Skip to content

Commit

Permalink
MethodSignatureRule - look at abstract trait method in Bleeding Edge
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 13, 2023
1 parent 2df14af commit 5fd8cee
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parameters:
notAnalysedTrait: true
curlSetOptTypes: true
listType: true
abstractTraitMethod: true
missingMagicSerializationRule: true
nullContextForVoidReturningFunctions: true
unescapeStrings: true
Expand Down
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parameters:
notAnalysedTrait: false
curlSetOptTypes: false
listType: false
abstractTraitMethod: false
missingMagicSerializationRule: false
nullContextForVoidReturningFunctions: false
unescapeStrings: false
Expand Down Expand Up @@ -916,6 +917,7 @@ services:
arguments:
reportMaybes: %reportMaybesInMethodSignatures%
reportStatic: %reportStaticMethodSignatures%
abstractTraitMethod: %featureToggles.abstractTraitMethod%

-
class: PHPStan\Rules\Methods\MethodParameterComparisonHelper
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ parametersSchema:
notAnalysedTrait: bool()
curlSetOptTypes: bool()
listType: bool()
abstractTraitMethod: bool()
missingMagicSerializationRule: bool()
nullContextForVoidReturningFunctions: bool()
unescapeStrings: bool()
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), 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']),
new DuplicateDeclarationRule(),
new LocalTypeAliasesRule($localTypeAliasesCheck),
new LocalTypeTraitAliasesRule($localTypeAliasesCheck, $reflectionProvider),
Expand Down
20 changes: 20 additions & 0 deletions src/Rules/Methods/MethodSignatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\VerbosityLevel;
use function count;
use function is_bool;
use function min;
use function sprintf;

Expand All @@ -34,6 +35,7 @@ class MethodSignatureRule implements Rule
public function __construct(
private bool $reportMaybes,
private bool $reportStatic,
private bool $abstractTraitMethod,
)
{
}
Expand Down Expand Up @@ -132,6 +134,24 @@ private function collectParentMethods(string $methodName, ClassReflection $class
$parentMethods[] = $interface->getNativeMethod($methodName);
}

if ($this->abstractTraitMethod) {
foreach ($class->getTraits(true) as $trait) {
if (!$trait->hasNativeMethod($methodName)) {
continue;
}

$method = $trait->getNativeMethod($methodName);
$isAbstract = $method->isAbstract();
if (is_bool($isAbstract)) {
if ($isAbstract) {
$parentMethods[] = $method;
}
} elseif ($isAbstract->yes()) {
$parentMethods[] = $method;
}
}
}

return $parentMethods;
}

Expand Down
15 changes: 14 additions & 1 deletion tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function getRule(): Rule

return new OverridingMethodRule(
$phpVersion,
new MethodSignatureRule($this->reportMaybes, $this->reportStatic),
new MethodSignatureRule($this->reportMaybes, $this->reportStatic, true),
true,
new MethodParameterComparisonHelper($phpVersion, true),
true,
Expand Down Expand Up @@ -423,4 +423,17 @@ public function testBug9905(): void
$this->analyse([__DIR__ . '/data/bug-9905.php'], []);
}

public function testTraits(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;

$this->analyse([__DIR__ . '/data/overriding-trait-methods-phpdoc.php'], [
[
'Parameter #1 $i (non-empty-string) of method OverridingTraitMethodsPhpDoc\Bar::doBar() should be contravariant with parameter $i (string) of method OverridingTraitMethodsPhpDoc\Foo::doBar()',
33,
]
]);
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function getRule(): Rule

return new OverridingMethodRule(
$phpVersion,
new MethodSignatureRule(true, true),
new MethodSignatureRule(true, true, true),
false,
new MethodParameterComparisonHelper($phpVersion, true),
true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php // lint >= 8.0

namespace OverridingTraitMethodsPhpDoc;

trait Foo
{

public function doFoo(int $i): int
{

}

abstract public function doBar(string $i): int;

}

class Bar
{

use Foo;

/**
* @param positive-int $i
*/
public function doFoo(int $i): string
{
// ok, trait method not abstract
}

/**
* @param non-empty-string $i
*/
public function doBar(string $i): int
{
// error
}

}

0 comments on commit 5fd8cee

Please sign in to comment.