[ticket/12273] Remove duplicated code

PHPBB3-12273
This commit is contained in:
Joas Schilling 2014-04-20 14:58:24 +02:00
parent 6849e8b36f
commit 08cce5fba5
2 changed files with 24 additions and 37 deletions

View file

@ -185,7 +185,7 @@ class php_exporter
if ($found_trigger_event !== false)
{
$event_line = $i;
$this->set_current_event($this->get_trigger_event_name($event_line), $event_line);
$this->set_current_event($this->get_event_name($event_line, false), $event_line);
// Find variables of the event
$arguments = $this->get_vars_from_array();
@ -198,7 +198,7 @@ class php_exporter
if ($found_dispatch !== false)
{
$event_line = $i;
$this->set_current_event($this->get_dispatch_name($event_line), $event_line);
$this->set_current_event($this->get_event_name($event_line, true), $event_line);
}
}
@ -241,46 +241,29 @@ class php_exporter
* Find the name of the event inside the dispatch() line
*
* @param int $event_line
* @param bool $is_dispatch Do we look for dispatch() or trigger_event() ?
* @return int Absolute line number
* @throws \LogicException
*/
public function get_dispatch_name($event_line)
public function get_event_name($event_line, $is_dispatch)
{
$event_text_line = $this->file_lines[$event_line];
$event_text_line = ltrim($event_text_line, "\t");
$regex = '#\$([a-z](?:[a-z0-9_]|->)*)';
$regex .= '->dispatch\(';
$regex .= '\'' . $this->preg_match_event_name() . '\'';
$regex .= '\);#';
$match = array();
preg_match($regex, $event_text_line, $match);
if (!isset($match[2]))
if ($is_dispatch)
{
throw new \LogicException("Can not find event name in line '{$event_text_line}' "
. "in file '{$this->current_file}:{$event_line}'", 1);
$regex = '#\$([a-z](?:[a-z0-9_]|->)*)';
$regex .= '->dispatch\(';
$regex .= '\'' . $this->preg_match_event_name() . '\'';
$regex .= '\);#';
}
else
{
$regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)';
$regex .= '->trigger_event\(';
$regex .= '\'' . $this->preg_match_event_name() . '\'';
$regex .= ', compact\(\$vars\)\)\);#';
}
return $match[2];
}
/**
* Find the name of the event inside the trigger_event() line
*
* @param int $event_line
* @return int Absolute line number
* @throws \LogicException
*/
public function get_trigger_event_name($event_line)
{
$event_text_line = $this->file_lines[$event_line];
$event_text_line = ltrim($event_text_line, "\t");
$regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)';
$regex .= '->trigger_event\(';
$regex .= '\'' . $this->preg_match_event_name() . '\'';
$regex .= ', compact\(\$vars\)\)\);#';
$match = array();
preg_match($regex, $event_text_line, $match);

View file

@ -233,7 +233,8 @@ class phpbb_event_php_exporter_test extends phpbb_test_case
*/
public function test_get_dispatch_name($event_line, $expected)
{
$this->assertEquals($expected, $this->exporter->get_dispatch_name($event_line));
$this->exporter->set_content(array($event_line));
$this->assertEquals($expected, $this->exporter->get_event_name(0, true));
}
static public function get_dispatch_name_throws_data()
@ -252,7 +253,8 @@ class phpbb_event_php_exporter_test extends phpbb_test_case
*/
public function test_get_dispatch_name_throws($event_line)
{
$this->exporter->get_dispatch_name($event_line);
$this->exporter->set_content(array($event_line));
$this->exporter->get_event_name(0, true);
}
static public function get_trigger_event_name_data()
@ -270,7 +272,8 @@ class phpbb_event_php_exporter_test extends phpbb_test_case
*/
public function test_get_trigger_event_name($event_line, $expected)
{
$this->assertEquals($expected, $this->exporter->get_trigger_event_name($event_line));
$this->exporter->set_content(array($event_line));
$this->assertEquals($expected, $this->exporter->get_event_name(0, false));
}
static public function get_trigger_event_name_throws_data()
@ -293,7 +296,8 @@ class phpbb_event_php_exporter_test extends phpbb_test_case
*/
public function test_get_trigger_event_name_throws($event_line)
{
$this->exporter->get_trigger_event_name($event_line);
$this->exporter->set_content(array($event_line));
$this->exporter->get_event_name(0, false);
}
static public function get_vars_from_array_data()