Skip to content

Commit

Permalink
Merge pull request moodlehq#171 from andrewnicols/inlineAnonymousClasses
Browse files Browse the repository at this point in the history
Ignore anonymous class docblocks if they have a parent
  • Loading branch information
stronk7 authored Jun 24, 2024
2 parents e92f166 + db84372 commit 85e9e0a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt
## [Unreleased]
### Changed
- The `moodle.NamingConventions.ValidFunctionName` sniff will now ignore errors on methods employing the `#[\Override]` attribute.
- 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.

## [v3.4.9] - 2024-06-19
### Fixed
Expand Down
5 changes: 5 additions & 0 deletions moodle/Sniffs/Commenting/MissingDocblockSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ protected function processScopes(File $phpcsFile, int $stackPtr): void {
// Skip methods of classes, traits and interfaces.
continue;
}
if ($token['code'] === T_ANON_CLASS && !empty($token['conditions'])) {
// Skip anonymous classes.
continue;
}

$artifactCount++;

if ($token['code'] === T_FUNCTION) {
Expand Down
25 changes: 25 additions & 0 deletions moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@ public static function docblockCorrectnessProvider(): array {
'warnings' => [
],
],
'Anonymous class as only class in file (documented)' => [
'fixture' => 'entire_anonymous_class_documented',
'fixtureFilename' => null,
'errors' => [
],
'warnings' => [
],
],
'Anonymous class as only class in file (undocumented)' => [
'fixture' => 'entire_anonymous_class',
'fixtureFilename' => null,
'errors' => [
5 => 'Missing docblock for class anonymous class',
],
'warnings' => [
],
],
'Anonymous class as member of method' => [
'fixture' => 'nested_anonymous_class',
'fixtureFilename' => null,
'errors' => [
],
'warnings' => [
],
],
];

if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

return new class() extends \stdClass {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

/**
* Class level docblock.
*/
return new class() extends \stdClass {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

/**
* Class level docblock.
*/
class class_with_anonymous_class_in_method {
/**
* Documented method.
*/
public function test(): string {
return new class() extends \stdClass {};
}
}

0 comments on commit 85e9e0a

Please sign in to comment.