Skip to content

feat: add rule to prevent comments after attributes #4101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PhpDoc;

use LogicException;
use Override;
use PhpParser\Node;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\VirtualNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function array_map;
use function assert;
use function max;
use function min;
use function property_exists;

/** @implements Rule<Node> */
#[RegisteredRule(level: 0)]
final class NoCommentsAfterAttributesRule implements Rule
{

#[Override]
public function getNodeType(): string
{
return Node::class;
}

#[Override]
public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof VirtualNode) {
return [];
}

if ($node->getDocComment() !== null) {
return [];
}

if (! property_exists($node, 'attrGroups')) {
return [];
}

$attrGroups = $node->attrGroups;

if ($attrGroups === []) {
return [];
}

$attrGroupEndLine = max(array_map(static fn (AttributeGroup $g) => $g->getEndLine(), $attrGroups));

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 55 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #1 ...$arg1 of function max expects non-empty-array, array<-1|int<1, max>> given.

if (property_exists($node, 'name')) {
$name = $node->name;
assert($name instanceof Node);
$startLine = $name->getStartLine();
} elseif ($node instanceof ClassConst) {
$startLine = min(array_map(static fn ($c) => $c->getStartLine(), $node->consts));

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 62 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.
} elseif ($node instanceof Property) {
$startLine = min(array_map(static fn ($c) => $c->getStartLine(), $node->props));

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.

Check failure on line 64 in src/Rules/PhpDoc/NoCommentsAfterAttributesRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #1 ...$arg1 of function min expects non-empty-array, array<-1|int<1, max>> given.
} elseif ($node instanceof Param) {
$startLine = $node->var->getStartLine();
} else {
throw new LogicException('Unexpected node type: ' . $node::class);
}

if ($startLine - $attrGroupEndLine <= 1) {
return [];
}

return [
RuleErrorBuilder::message('No comments after attributes.')
->identifier('node.noCommentsAfterAttributes')
->line($attrGroupEndLine + 1)
->nonIgnorable()
->build(),
];
}

}
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/NoCommentsAfterAttributesRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PhpDoc;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\CoversNothing;

/** @extends RuleTestCase<NoCommentsAfterAttributesRule> */
#[CoversNothing]

Check failure on line 10 in tests/PHPStan/Rules/PhpDoc/NoCommentsAfterAttributesRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Attribute class PHPUnit\Framework\Attributes\CoversNothing does not exist.

Check failure on line 10 in tests/PHPStan/Rules/PhpDoc/NoCommentsAfterAttributesRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Attribute class PHPUnit\Framework\Attributes\CoversNothing does not exist.

Check failure on line 10 in tests/PHPStan/Rules/PhpDoc/NoCommentsAfterAttributesRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Attribute class PHPUnit\Framework\Attributes\CoversNothing does not exist.

Check failure on line 10 in tests/PHPStan/Rules/PhpDoc/NoCommentsAfterAttributesRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Attribute class PHPUnit\Framework\Attributes\CoversNothing does not exist.
class NoCommentsAfterAttributesRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new NoCommentsAfterAttributesRule();
}

public function testRule(): void
{
$message = 'No comments after attributes.';

$this->analyse([__DIR__ . '/data/no-comments-after-attributes.php'], [
[$message, 37],
[$message, 41],
[$message, 45],
[$message, 50],
[$message, 53],
[$message, 58],
[$message, 62],
[$message, 71],
[$message, 81],
]);
}

}
83 changes: 83 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/no-comments-after-attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace NoCommentsAfterAttributes;

/** This is a doc comment. */
#[Good]
class Good
{
/** @var class-string*/
#[Good]
public const FOO = 'Foo';

/** @var array<int, string> */
#[Good]
private array $foo = [];

public function __construct(
/** @var array<int, string> */
#[Good]
private array $bar,
/** @var array<int, string> */
#[Good]
array $baz,
#[Good] string $qux,
) {}

// This is a comment.
#[Good]
public function foo(): void {}

/** This is a doc comment. */
#[Good]
public function bar(): void {}
}

#[Bad]
/** This is a doc comment. */
class Bad
{
#[Bad]
/** @var class-string */
public const BAR = 'Bar';

#[Bad]
/** @var array<int, string> */
private array $foo = [];

public function __construct(
#[Bad]
/** @var array<int, string> */
private array $bar,
#[Bad]
/** @var array<int, string> */
array $baz,
) {}

#[Bad]
// This is a comment after attributes.
public function foo(): void {}

#[Bad]
/** This is a doc comment after attributes. */
public function bar(): void {}
}

/** This is a doc comment. */
#[Good]
function foo(): void {}

#[Bad]
/** This is a doc comment. */
function bar(): void {}

enum Foo
{
/** This is a doc comment before attributes. */
#[Good]
case Foo;

#[Bad]
/** This is a doc comment after attributes. */
case Baz;
}
Loading