Skip to content

Commit

Permalink
New parameter checkMissingOverrideMethodAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 14, 2023
1 parent 5a19a9c commit ac4d3de
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ services:
checkPhpDocMethodSignatures: %checkPhpDocMethodSignatures%
genericPrototypeMessage: %featureToggles.genericPrototypeMessage%
finalByPhpDoc: %featureToggles.finalByPhpDoc%
checkMissingOverrideMethodAttribute: %checkMissingOverrideMethodAttribute%
tags:
- phpstan.rules.rule

Expand Down
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ parameters:
reportMaybesInPropertyPhpDocTypes: false
reportStaticMethodSignatures: false
reportWrongPhpDocTypeInVarTag: false
checkMissingOverrideMethodAttribute: false
mixinExcludeClasses: []
scanFiles: []
scanDirectories: []
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ parametersSchema:
reportMaybesInPropertyPhpDocTypes: bool()
reportStaticMethodSignatures: bool()
reportWrongPhpDocTypeInVarTag: bool()
checkMissingOverrideMethodAttribute: bool()
parallel: structure([
jobSize: int(),
processTimeout: float(),
Expand Down
2 changes: 1 addition & 1 deletion src/PhpDoc/StubValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private function getRuleRegistry(Container $container): RuleRegistry
new ExistingClassesInTypehintsRule($functionDefinitionCheck),
new \PHPStan\Rules\Functions\ExistingClassesInTypehintsRule($functionDefinitionCheck),
new ExistingClassesInPropertiesRule($reflectionProvider, $classCaseSensitivityCheck, $unresolvableTypeHelper, $phpVersion, true, false),
new OverridingMethodRule($phpVersion, new MethodSignatureRule(true, true, $container->getParameter('featureToggles')['abstractTraitMethod']), true, new MethodParameterComparisonHelper($phpVersion, $container->getParameter('featureToggles')['genericPrototypeMessage']), $container->getParameter('featureToggles')['genericPrototypeMessage'], $container->getParameter('featureToggles')['finalByPhpDoc']),
new OverridingMethodRule($phpVersion, new MethodSignatureRule(true, true, $container->getParameter('featureToggles')['abstractTraitMethod']), true, new MethodParameterComparisonHelper($phpVersion, $container->getParameter('featureToggles')['genericPrototypeMessage']), $container->getParameter('featureToggles')['genericPrototypeMessage'], $container->getParameter('featureToggles')['finalByPhpDoc'], $container->getParameter('checkMissingOverrideMethodAttribute')),
new DuplicateDeclarationRule(),
new LocalTypeAliasesRule($localTypeAliasesCheck),
new LocalTypeTraitAliasesRule($localTypeAliasesCheck, $reflectionProvider),
Expand Down
14 changes: 14 additions & 0 deletions src/Rules/Methods/OverridingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __construct(
private MethodParameterComparisonHelper $methodParameterComparisonHelper,
private bool $genericPrototypeMessage,
private bool $finalByPhpDoc,
private bool $checkMissingOverrideMethodAttribute,
)
{
}
Expand Down Expand Up @@ -96,6 +97,19 @@ public function processNode(Node $node, Scope $scope): array
[$prototype, $checkVisibility] = $prototypeData;

$messages = [];
if (
$this->phpVersion->supportsOverrideAttribute()
&& $this->checkMissingOverrideMethodAttribute
&& !$this->hasOverrideAttribute($node->getOriginalNode())
) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Method %s::%s() overrides method %s::%s() but is missing the #[Override] attribute.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$prototype->getDeclaringClass()->getDisplayName($this->genericPrototypeMessage),
$prototype->getName(),
))->build();
}
if ($prototype->isFinalByKeyword()->yes()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Method %s::%s() overrides final method %s::%s().',
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function getRule(): Rule
new MethodParameterComparisonHelper($phpVersion, true),
true,
true,
false,
);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class OverridingMethodRuleTest extends RuleTestCase

private int $phpVersionId;

private bool $checkMissingOverrideMethodAttribute = false;

protected function getRule(): Rule
{
$phpVersion = new PhpVersion($this->phpVersionId);
Expand All @@ -28,6 +30,7 @@ protected function getRule(): Rule
new MethodParameterComparisonHelper($phpVersion, true),
true,
true,
$this->checkMissingOverrideMethodAttribute,
);
}

Expand Down Expand Up @@ -751,4 +754,28 @@ public function testOverrideAttribute(): void
]);
}

public function dataCheckMissingOverrideAttribute(): iterable
{
yield [false, 80000, []];
yield [true, 80000, []];
yield [false, 80300, []];
yield [true, 80300, [
[
'Method CheckMissingOverrideAttr\Bar::doFoo() overrides method CheckMissingOverrideAttr\Foo::doFoo() but is missing the #[Override] attribute.',
18,
],
]];
}

/**
* @dataProvider dataCheckMissingOverrideAttribute
* @param list<array{0: string, 1: int, 2?: string}> $errors
*/
public function testCheckMissingOverrideAttribute(bool $checkMissingOverrideMethodAttribute, int $phpVersionId, array $errors): void
{
$this->checkMissingOverrideMethodAttribute = $checkMissingOverrideMethodAttribute;
$this->phpVersionId = $phpVersionId;
$this->analyse([__DIR__ . '/data/check-missing-override-attr.php'], $errors);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Methods/data/check-missing-override-attr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CheckMissingOverrideAttr;

class Foo
{

public function doFoo(): void
{

}

}

class Bar extends Foo
{

public function doFoo(): void
{

}

}

0 comments on commit ac4d3de

Please sign in to comment.