Skip to content

Commit 85e9e0a

Browse files
authored
Merge pull request #171 from andrewnicols/inlineAnonymousClasses
Ignore anonymous class docblocks if they have a parent
2 parents e92f166 + db84372 commit 85e9e0a

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
88
## [Unreleased]
99
### Changed
1010
- The `moodle.NamingConventions.ValidFunctionName` sniff will now ignore errors on methods employing the `#[\Override]` attribute.
11+
- 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.
1112

1213
## [v3.4.9] - 2024-06-19
1314
### Fixed

moodle/Sniffs/Commenting/MissingDocblockSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ protected function processScopes(File $phpcsFile, int $stackPtr): void {
8383
// Skip methods of classes, traits and interfaces.
8484
continue;
8585
}
86+
if ($token['code'] === T_ANON_CLASS && !empty($token['conditions'])) {
87+
// Skip anonymous classes.
88+
continue;
89+
}
90+
8691
$artifactCount++;
8792

8893
if ($token['code'] === T_FUNCTION) {

moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,31 @@ public static function docblockCorrectnessProvider(): array {
199199
'warnings' => [
200200
],
201201
],
202+
'Anonymous class as only class in file (documented)' => [
203+
'fixture' => 'entire_anonymous_class_documented',
204+
'fixtureFilename' => null,
205+
'errors' => [
206+
],
207+
'warnings' => [
208+
],
209+
],
210+
'Anonymous class as only class in file (undocumented)' => [
211+
'fixture' => 'entire_anonymous_class',
212+
'fixtureFilename' => null,
213+
'errors' => [
214+
5 => 'Missing docblock for class anonymous class',
215+
],
216+
'warnings' => [
217+
],
218+
],
219+
'Anonymous class as member of method' => [
220+
'fixture' => 'nested_anonymous_class',
221+
'fixtureFilename' => null,
222+
'errors' => [
223+
],
224+
'warnings' => [
225+
],
226+
],
202227
];
203228

204229
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;
4+
5+
return new class() extends \stdClass {};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;
4+
5+
/**
6+
* Class level docblock.
7+
*/
8+
return new class() extends \stdClass {};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;
4+
5+
/**
6+
* Class level docblock.
7+
*/
8+
class class_with_anonymous_class_in_method {
9+
/**
10+
* Documented method.
11+
*/
12+
public function test(): string {
13+
return new class() extends \stdClass {};
14+
}
15+
}

0 commit comments

Comments
 (0)