From b831e96aafc4b8c2bc883dd47c4f2452f329e5b2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 02:08:23 +0200 Subject: [ticket/12273] Use class properties instead of parameters PHPBB3-12273 --- phpBB/develop/event_exporter.php | 237 +++++++++++++++++++++------------------ 1 file changed, 128 insertions(+), 109 deletions(-) (limited to 'phpBB/develop') diff --git a/phpBB/develop/event_exporter.php b/phpBB/develop/event_exporter.php index c17e714764..a84b18db26 100644 --- a/phpBB/develop/event_exporter.php +++ b/phpBB/develop/event_exporter.php @@ -12,12 +12,30 @@ class event_exporter /** @var string */ protected $root_path; + /** @var string */ + protected $current_file; + + /** @var string */ + protected $current_event; + + /** @var int */ + protected $current_event_line; + + /** @var array */ + protected $events; + + /** @var array */ + protected $file_lines; + /** * @param string $phpbb_root_path */ public function __construct($phpbb_root_path) { $this->root_path = $phpbb_root_path; + $this->events = $this->file_lines = array(); + $this->current_file = $this->current_event = ''; + $this->current_event_line = 0; } function export_from_eventsmd($filter) @@ -82,53 +100,70 @@ class event_exporter function export_from_php() { $files = $this->get_file_list($this->root_path); - $events = array(); + $this->events = array(); foreach ($files as $file) { - $file_events = $this->check_for_events($file); - if (!empty($file_events)) - { - $events = array_merge($events, $file_events); - } + $this->check_for_events($file); } - ksort($events); + ksort($this->events); - foreach ($events as $event) + foreach ($this->events as $event) { echo '|- id="' . $event['event'] . '"' . "\n"; echo '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; } } + public function get_events() + { + return $this->events; + } + + public function set_current_event($name, $line) + { + $this->current_event = $name; + $this->current_event_line = $line; + } + + public function set_content($content) + { + $this->file_lines = $content; + } + + /** + * @param $file + * @throws LogicException + */ public function check_for_events($file) { - $events = array(); - $content = file_get_contents($this->root_path . $file); + $this->current_file = $file; + $this->file_lines = array(); + $content = file_get_contents($this->root_path . $this->current_file); if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) { - $lines = explode("\n", $content); - for ($i = 0, $num_lines = sizeof($lines); $i < $num_lines; $i++) + $this->set_content(explode("\n", $content)); + for ($i = 0, $num_lines = sizeof($this->file_lines); $i < $num_lines; $i++) { $event_line = false; - $found_trigger_event = strpos($lines[$i], "dispatcher->trigger_event('"); + $found_trigger_event = strpos($this->file_lines[$i], "dispatcher->trigger_event('"); if ($found_trigger_event !== false) { $event_line = $i; - $event_name = $this->get_trigger_event_name($file, $lines[$event_line]); + $this->set_current_event($this->get_trigger_event_name($this->file_lines[$event_line]), $event_line); // Find variables of the event - $arguments = $this->get_vars_from_array($file, $event_name, $lines, $event_line); - $doc_vars = $this->get_vars_from_docblock($file, $event_name, $lines, $event_line); - $this->validate_vars_docblock_array($file, $event_name, $arguments, $doc_vars); + $arguments = $this->get_vars_from_array(); + $doc_vars = $this->get_vars_from_docblock(); + $this->validate_vars_docblock_array($arguments, $doc_vars); } else { - $found_dispatch = strpos($lines[$i], "dispatcher->dispatch('"); + $found_dispatch = strpos($this->file_lines[$i], "dispatcher->dispatch('"); if ($found_dispatch !== false) { $event_line = $i; - $event_name = $this->get_dispatch_name($file, $lines[$event_line]); + $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); $arguments = array(); } } @@ -136,20 +171,26 @@ class event_exporter if ($event_line) { // Validate @event - $event_line_num = $this->find_event($file, $event_name, $lines, $event_line); - $this->validate_event($file, $event_name, $lines[$event_line_num]); + $event_line_num = $this->find_event(); + $this->validate_event($this->current_event, $this->file_lines[$event_line_num]); // Validate @since - $since_line_num = $this->find_since($file, $event_name, $lines, $event_line); - $since = $this->validate_since($file, $event_name, $lines[$since_line_num]); + $since_line_num = $this->find_since(); + $since = $this->validate_since($this->file_lines[$since_line_num]); // Find event description line - $description_line_num = $this->find_description($file, $event_name, $lines, $event_line); - $description = substr(trim($lines[$description_line_num]), strlen('* ')); + $description_line_num = $this->find_description(); + $description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); - $events[$event_name] = array( - 'event' => $event_name, - 'file' => $file, + if (isset($this->events[$this->current_event])) + { + throw new LogicException('The event "' . $this->current_event . '" from file "' . $this->current_file + . '" already exists in file "'. $this->events[$this->current_event]['file'] . '"', 10); + } + + $this->events[$this->current_event] = array( + 'event' => $this->current_event, + 'file' => $this->current_file, 'arguments' => $arguments, 'since' => $since, 'description' => $description, @@ -157,18 +198,16 @@ class event_exporter } } } - - return $events; } /** * Find the name of the event inside the dispatch() line * - * @param string $file * @param string $event_line * @return int Absolute line number + * @throws LogicException */ - public function get_dispatch_name($file, $event_line) + public function get_dispatch_name($event_line) { $event_line = ltrim($event_line, "\t"); @@ -181,7 +220,7 @@ class event_exporter preg_match($regex, $event_line, $match); if (!isset($match[2])) { - throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); } return $match[2]; @@ -190,11 +229,11 @@ class event_exporter /** * Find the name of the event inside the trigger_event() line * - * @param string $file * @param string $event_line * @return int Absolute line number + * @throws LogicException */ - public function get_trigger_event_name($file, $event_line) + public function get_trigger_event_name($event_line) { $event_line = ltrim($event_line, "\t"); @@ -207,7 +246,7 @@ class event_exporter preg_match($regex, $event_line, $match); if (!isset($match[2])) { - throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); } return $match[2]; @@ -226,24 +265,21 @@ class event_exporter /** * Find the $vars array * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return array List of variables + * @throws LogicException */ - public function get_vars_from_array($file, $event_name, $lines, $event_line) + public function get_vars_from_array() { - $vars_line = ltrim($lines[$event_line - 1], "\t"); + $vars_line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); if (strpos($vars_line, "\$vars = array('") !== 0 || substr($vars_line, -3) !== '\');') { - throw new LogicException('Can not find "$vars = array();"-line for event "' . $event_name . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); } $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); if ($vars_array === '') { - throw new LogicException('Found empty $vars array for event "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } $vars_array = explode("', '", $vars_array); @@ -252,7 +288,7 @@ class event_exporter { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new LogicException('Found invalid var "' . $var . '" in array for event "' . $event_name . '" in file "' . $file . '"', 3); + throw new LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); } } @@ -263,59 +299,56 @@ class event_exporter /** * Find the $vars array * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return array List of variables + * @throws LogicException */ - public function get_vars_from_docblock($file, $event_name, $lines, $event_line) + public function get_vars_from_docblock() { $doc_vars = array(); $current_doc_line = 1; $found_comment_end = false; - while (ltrim($lines[$event_line - $current_doc_line], "\t") !== '/**') + while (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") !== '/**') { - if (ltrim($lines[$event_line - $current_doc_line], "\t") === '*/') + if (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") === '*/') { $found_comment_end = true; } if ($found_comment_end) { - $var_line = trim($lines[$event_line - $current_doc_line]); + $var_line = trim($this->file_lines[$this->current_event_line - $current_doc_line]); $var_line = preg_replace('!\s+!', ' ', $var_line); if (strpos($var_line, '* @var ') === 0) { $doc_line = explode(' ', $var_line, 5); if (sizeof($doc_line) !== 5) { - throw new LogicException('Found invalid line "' . $lines[$event_line - $current_doc_line] - . '" for event "' . $event_name . '" in file "' . $file . '"', 1); + throw new LogicException('Found invalid line "' . $this->file_lines[$this->current_event_line - $current_doc_line] + . '" for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); } $doc_vars[] = $doc_line[3]; } } $current_doc_line++; - if ($current_doc_line > $event_line) + if ($current_doc_line > $this->current_event_line) { // Reached the start of the file - throw new LogicException('Can not find end of docblock for event "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Can not find end of docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } } if (empty($doc_vars)) { // Reached the start of the file - throw new LogicException('Can not find @var lines for event "' . $event_name . '" in file "' . $file . '"', 3); + throw new LogicException('Can not find @var lines for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); } foreach ($doc_vars as $var) { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $event_name . '" in file "' . $file . '"', 4); + throw new LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); } } @@ -326,109 +359,96 @@ class event_exporter /** * Find the "@since" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return int Absolute line number + * @throws LogicException */ - public function find_since($file, $event_name, $lines, $event_line) + public function find_since() { - return $this->find_tag($file, $event_name, $lines, $event_line, 'since', array('event', 'var')); + return $this->find_tag('since', array('event', 'var')); } /** * Find the "@event" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return int Absolute line number */ - public function find_event($file, $event_name, $lines, $event_line) + public function find_event() { - return $this->find_tag($file, $event_name, $lines, $event_line, 'event', array()); + return $this->find_tag('event', array()); } /** * Find a "@*" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @param string $find_tag Name of the tag we are trying to find * @param array $disallowed_tags List of tags that must not appear between * the tag and the actual event * @return int Absolute line number + * @throws LogicException */ - public function find_tag($file, $event_name, $lines, $event_line, $find_tag, $disallowed_tags) + public function find_tag($find_tag, $disallowed_tags) { $find_tag_line = 0; $found_comment_end = false; - while (strpos(ltrim($lines[$event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) + while (strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) { - if ($found_comment_end && ltrim($lines[$event_line - $find_tag_line], "\t") === '/**') + if ($found_comment_end && ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '/**') { // Reached the start of this doc block - throw new LogicException('Can not find @' . $find_tag . ' information for event "' . $event_name . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); } foreach ($disallowed_tags as $disallowed_tag) { - if ($found_comment_end && strpos(ltrim($lines[$event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) + if ($found_comment_end && strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) { // Found @var after the @since - throw new LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $event_name . '" in file "' . $file . '"', 3); + throw new LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); } } - if (ltrim($lines[$event_line - $find_tag_line], "\t") === '*/') + if (ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '*/') { $found_comment_end = true; } $find_tag_line++; - if ($find_tag_line >= $event_line) + if ($find_tag_line >= $this->current_event_line) { // Reached the start of the file - throw new LogicException('Can not find @' . $find_tag . ' information for event "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } } - return $event_line - $find_tag_line; + return $this->current_event_line - $find_tag_line; } /** * Find a "@*" Information line * - * @param string $file - * @param string $event_name - * @param array $lines - * @param int $event_line Index of the event call in $lines * @return int Absolute line number + * @throws LogicException */ - public function find_description($file, $event_name, $lines, $event_line) + public function find_description() { $find_desc_line = 0; - while (ltrim($lines[$event_line - $find_desc_line], "\t") !== '/**') + while (ltrim($this->file_lines[$this->current_event_line - $find_desc_line], "\t") !== '/**') { $find_desc_line++; - if ($find_desc_line > $event_line) + if ($find_desc_line > $this->current_event_line) { // Reached the start of the file - throw new LogicException('Can not find a description for event "' . $event_name . '" in file "' . $file . '"', 1); + throw new LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); } } - $find_desc_line = $event_line - $find_desc_line + 1; + $find_desc_line = $this->current_event_line - $find_desc_line + 1; - $desc = trim($lines[$find_desc_line]); + $desc = trim($this->file_lines[$find_desc_line]); if (strpos($desc, '* @') === 0 || $desc[0] !== '*' || substr($desc, 1) == '') { // First line of the doc block is a @-line, empty or only contains "*" - throw new LogicException('Can not find a description for event "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } return $find_desc_line; @@ -437,25 +457,24 @@ class event_exporter /** * Validate "@since" Information * - * @param string $file - * @param string $event_name * @param string $line * @return string + * @throws LogicException */ - public function validate_since($file, $event_name, $line) + public function validate_since($line) { $since = substr(ltrim($line, "\t"), strlen('* @since ')); if ($since !== trim($since)) { - throw new LogicException('Invalid @since information for event "' . $event_name . '" in file "' . $file . '"', 1); + throw new LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); } $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) { - throw new LogicException('Invalid @since information for event "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); } return $since; @@ -464,23 +483,23 @@ class event_exporter /** * Validate "@event" Information * - * @param string $file * @param string $event_name * @param string $line * @return string + * @throws LogicException */ - public function validate_event($file, $event_name, $line) + public function validate_event($event_name, $line) { $event = substr(ltrim($line, "\t"), strlen('* @event ')); if ($event !== trim($event)) { - throw new LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $file . '"', 1); + throw new LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $this->current_file . '"', 1); } if ($event !== $event_name) { - throw new LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $file . '"', 2); + throw new LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); } return $event; @@ -489,13 +508,12 @@ class event_exporter /** * Validates that two arrays contain the same strings * - * @param string $file - * @param string $event_name * @param array $vars_array Variables found in the array line * @param array $vars_docblock Variables found in the doc block * @return null + * @throws LogicException */ - public function validate_vars_docblock_array($file, $event_name, $vars_array, $vars_docblock) + public function validate_vars_docblock_array($vars_array, $vars_docblock) { $vars_array = array_unique($vars_array); $vars_docblock = array_unique($vars_docblock); @@ -503,7 +521,7 @@ class event_exporter if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) { - throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); + throw new LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); } } @@ -513,6 +531,7 @@ class event_exporter * Works recursive with any depth * * @param string $dir Directory to go through + * @param string $path Path from root to $dir * @return array List of files (including directories from within $dir */ function get_file_list($dir, $path = '') -- cgit v1.2.1