| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4a37567 commit 91deb44
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,31 +117,10 @@ public function check_for_events($file) | |||
| 117 | 117 | $event_line = $i; | |
| 118 | 118 | $event_name = $this->get_trigger_event_name($file, $lines[$event_line]); | |
| 119 | 119 | ||
| 120 | - // Find $vars array | ||
| 120 | + // Find variables of the event | ||
| 121 | 121 | $arguments = $this->get_vars_from_array($file, $event_name, $lines, $event_line); | |
| 122 | - | ||
| 123 | - // Validate $vars array with @var | ||
| 124 | - $find_vars_line = 3; | ||
| 125 | - $doc_vars = array(); | ||
| 126 | - while (strpos(trim($lines[$event_line - $find_vars_line]), '*') === 0) | ||
| 127 | - { | ||
| 128 | - $var_line = trim($lines[$event_line - $find_vars_line]); | ||
| 129 | - $var_line = preg_replace('!\s+!', ' ', $var_line); | ||
| 130 | - if (strpos($var_line, '* @var ') === 0) | ||
| 131 | - { | ||
| 132 | - $doc_line = explode(' ', $var_line); | ||
| 133 | - if (isset($doc_line[3])) | ||
| 134 | - { | ||
| 135 | - $doc_vars[] = $doc_line[3]; | ||
| 136 | - } | ||
| 137 | - } | ||
| 138 | - $find_vars_line++; | ||
| 139 | - } | ||
| 140 | - | ||
| 141 | - if (sizeof($arguments) !== sizeof($doc_vars) && array_intersect($arguments, $doc_vars)) | ||
| 142 | - { | ||
| 143 | - throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); | ||
| 144 | - } | ||
| 122 | + $doc_vars = $this->get_vars_from_docblock($file, $event_name, $lines, $event_line); | ||
| 123 | + $this->validate_vars_docblock_array($file, $event_name, $arguments, $doc_vars); | ||
| 145 | 124 | } | |
| 146 | 125 | else | |
| 147 | 126 | { | |
@@ -277,9 +256,73 @@ public function get_vars_from_array($file, $event_name, $lines, $event_line) | |||
| 277 | 256 | } | |
| 278 | 257 | } | |
| 279 | 258 | ||
| 259 | + sort($vars_array); | ||
| 280 | 260 | return $vars_array; | |
| 281 | 261 | } | |
| 282 | 262 | ||
| 263 | + /** | ||
| 264 | + * Find the $vars array | ||
| 265 | + * | ||
| 266 | + * @param string $file | ||
| 267 | + * @param string $event_name | ||
| 268 | + * @param array $lines | ||
| 269 | + * @param int $event_line Index of the event call in $lines | ||
| 270 | + * @return array List of variables | ||
| 271 | + */ | ||
| 272 | + public function get_vars_from_docblock($file, $event_name, $lines, $event_line) | ||
| 273 | + { | ||
| 274 | + $doc_vars = array(); | ||
| 275 | + $current_doc_line = 1; | ||
| 276 | + $found_comment_end = false; | ||
| 277 | + while (ltrim($lines[$event_line - $current_doc_line], "\t") !== '/**') | ||
| 278 | + { | ||
| 279 | + if (ltrim($lines[$event_line - $current_doc_line], "\t") === '*/') | ||
| 280 | + { | ||
| 281 | + $found_comment_end = true; | ||
| 282 | + } | ||
| 283 | + | ||
| 284 | + if ($found_comment_end) | ||
| 285 | + { | ||
| 286 | + $var_line = trim($lines[$event_line - $current_doc_line]); | ||
| 287 | + $var_line = preg_replace('!\s+!', ' ', $var_line); | ||
| 288 | + if (strpos($var_line, '* @var ') === 0) | ||
| 289 | + { | ||
| 290 | + $doc_line = explode(' ', $var_line, 5); | ||
| 291 | + if (sizeof($doc_line) !== 5) | ||
| 292 | + { | ||
| 293 | + throw new LogicException('Found invalid line "' . $lines[$event_line - $current_doc_line] | ||
| 294 | + . '" for event "' . $event_name . '" in file "' . $file . '"', 1); | ||
| 295 | + } | ||
| 296 | + $doc_vars[] = $doc_line[3]; | ||
| 297 | + } | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + $current_doc_line++; | ||
| 301 | + if ($current_doc_line > $event_line) | ||
| 302 | + { | ||
| 303 | + // Reached the start of the file | ||
| 304 | + throw new LogicException('Can not find end of docblock for event "' . $event_name . '" in file "' . $file . '"', 2); | ||
| 305 | + } | ||
| 306 | + } | ||
| 307 | + | ||
| 308 | + if (empty($doc_vars)) | ||
| 309 | + { | ||
| 310 | + // Reached the start of the file | ||
| 311 | + throw new LogicException('Can not find @var lines for event "' . $event_name . '" in file "' . $file . '"', 3); | ||
| 312 | + } | ||
| 313 | + | ||
| 314 | + foreach ($doc_vars as $var) | ||
| 315 | + { | ||
| 316 | + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) | ||
| 317 | + { | ||
| 318 | + throw new LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $event_name . '" in file "' . $file . '"', 4); | ||
| 319 | + } | ||
| 320 | + } | ||
| 321 | + | ||
| 322 | + sort($doc_vars); | ||
| 323 | + return $doc_vars; | ||
| 324 | + } | ||
| 325 | + | ||
| 283 | 326 | /** | |
| 284 | 327 | * Find the "@since" Information line | |
| 285 | 328 | * | |
@@ -443,6 +486,27 @@ public function validate_event($file, $event_name, $line) | |||
| 443 | 486 | return $event; | |
| 444 | 487 | } | |
| 445 | 488 | ||
| 489 | + /** | ||
| 490 | + * Validates that two arrays contain the same strings | ||
| 491 | + * | ||
| 492 | + * @param string $file | ||
| 493 | + * @param string $event_name | ||
| 494 | + * @param array $vars_array Variables found in the array line | ||
| 495 | + * @param array $vars_docblock Variables found in the doc block | ||
| 496 | + * @return null | ||
| 497 | + */ | ||
| 498 | + public function validate_vars_docblock_array($file, $event_name, $vars_array, $vars_docblock) | ||
| 499 | + { | ||
| 500 | + $vars_array = array_unique($vars_array); | ||
| 501 | + $vars_docblock = array_unique($vars_docblock); | ||
| 502 | + $sizeof_vars_array = sizeof($vars_array); | ||
| 503 | + | ||
| 504 | + if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) | ||
| 505 | + { | ||
| 506 | + throw new LogicException('$vars array does not match the list of @var tags for event "' . $event_name . '" in file "' . $file . '"'); | ||
| 507 | + } | ||
| 508 | + } | ||
| 509 | + | ||
| 446 | 510 | /** | |
| 447 | 511 | * Returns a list of files in that directory | |
| 448 | 512 | * | |
| Back | FazBrowse Home | New Git URL |
0 commit comments