Skip to content

Commit

Permalink
Ensure we stop looking for file phpdoc block asap
Browse files Browse the repository at this point in the history
Previously, we were allowing the getDocTagFromOpenTag()
method to advance too much when looking for file phpdoc
blocks. Now we stop as soon as any of the stop tokens is found.

Note that this case is very edge one, only reproducible when
there are lots of missing class/function phpdoc block, causing
some other phpdoc block to be picked.

In any case, I think that it's perfectly ok to shortcut the
search that way, it can save us some precious iterations.

Fixes moodlehq#172
  • Loading branch information
stronk7 committed Jun 27, 2024
1 parent 85e9e0a commit 4d9f6f3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions moodle/Tests/Util/fixtures/docblocks/none.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@
namespace MoodleHQ\MoodleCS\moodle\Tests;

class example {

public function get_something() {
/** @var int $variable */
$variable = 1;
return $variable;
}
5 changes: 5 additions & 0 deletions moodle/Util/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ protected static function getDocTagFromOpenTag(
];

while ($stackPtr = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true)) {
// If we have arrived to a stop token, and haven't found the file docblock yet, then there isn't one.
if (in_array($tokens[$stackPtr]['code'], $stopAtTypes)) {
return null;
}

if ($tokens[$stackPtr]['code'] === T_NAMESPACE || $tokens[$stackPtr]['code'] === T_USE) {
$stackPtr = $phpcsFile->findNext(T_SEMICOLON, $stackPtr + 1);
continue;
Expand Down

0 comments on commit 4d9f6f3

Please sign in to comment.