Skip to content

Commit

Permalink
Ignore anonymous class docblocks if they have a parent
Browse files Browse the repository at this point in the history
There's usually little need to document an anonymous class when it is
part of another method.
  • Loading branch information
andrewnicols committed Jun 20, 2024
1 parent e92f166 commit 2c94596
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
The format of this change log follows the advice given at [Keep a CHANGELOG](https://keepachangelog.com).

## [Unreleased]
### Fixed
- The `moodle.Commenting.MissingDocblock` sniff no longer warns about anonymous classes which have a parent object such as a unit test method.

### Changed
- The `moodle.NamingConventions.ValidFunctionName` sniff will now ignore errors on methods employing the `#[\Override]` attribute.

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 but incorrect whitespace.
*/
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 but incorrect whitespace.
*/
class class_with_anonymous_class_in_method {
/**
* Documented method.
*/
public function test(): string {
return new class() extends \stdClass {};
}
}

0 comments on commit 2c94596

Please sign in to comment.