Skip to content

Commit 13b8e00

Browse files
committed
Ensure we stop looking for file phpdoc block asap
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 #172
1 parent 85e9e0a commit 13b8e00

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
1010
- The `moodle.NamingConventions.ValidFunctionName` sniff will now ignore errors on methods employing the `#[\Override]` attribute.
1111
- The `moodle.Commenting.MissingDocblock` sniff no longer warns about missing docs on non-global anonymous classes, for example those written as an instance class in a unit test.
1212

13+
### Fixed
14+
- Fixed an edge case leading to the file phpdoc block being incorrectly detected by various sniffs.
15+
1316
## [v3.4.9] - 2024-06-19
1417
### Fixed
1518
- Fixed a recent regression by allowing to the `moodle.Files.BoilerplateComment` sniff to contain "extra" consecutive comment lines immediately after the official boilerplate ends.

moodle/Tests/Util/DocblocksTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@
3333
*/
3434
class DocblocksTest extends MoodleCSBaseTestCase
3535
{
36-
public function testgetDocBlockPointer(): void {
36+
public static function getNullDocBlockPointerProvider(): array {
37+
return [
38+
'global_scope_code' => ['none_global_scope.php'],
39+
'oop_scope_code' => ['none.php'],
40+
];
41+
}
42+
43+
/**
44+
* @dataProvider getNullDocBlockPointerProvider
45+
*/
46+
public function testGetNullDocBlockPointer(string $fixture): void {
3747
$phpcsConfig = new Config();
3848
$phpcsRuleset = new Ruleset($phpcsConfig);
3949
$phpcsFile = new \PHP_CodeSniffer\Files\LocalFile(
40-
__DIR__ . '/fixtures/docblocks/none.php',
50+
__DIR__ . '/fixtures/docblocks/' . $fixture,
4151
$phpcsRuleset,
4252
$phpcsConfig
4353
);

moodle/Tests/Util/fixtures/docblocks/none.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@
1717
namespace MoodleHQ\MoodleCS\moodle\Tests;
1818

1919
class example {
20+
public function get_something() {
21+
/** @var int $variable */
22+
$variable = 1;
23+
return $variable;
24+
}
2025
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Just an extreme case, that makes no sense within Moodle, but it's a valid PHP file.
18+
$variable = 1;

moodle/Util/Docblocks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ protected static function getDocTagFromOpenTag(
256256
];
257257

258258
while ($stackPtr = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true)) {
259+
// If we have arrived to a stop token, and haven't found the file docblock yet, then there isn't one.
260+
if (in_array($tokens[$stackPtr]['code'], $stopAtTypes)) {
261+
return null;
262+
}
263+
259264
if ($tokens[$stackPtr]['code'] === T_NAMESPACE || $tokens[$stackPtr]['code'] === T_USE) {
260265
$stackPtr = $phpcsFile->findNext(T_SEMICOLON, $stackPtr + 1);
261266
continue;

0 commit comments

Comments
 (0)