Skip to content

Commit

Permalink
Check @no-named-arguments in MethodSignatureRule
Browse files Browse the repository at this point in the history
Add a check preventing a child method from declaring `@no-named-arguments` when parent method does not
  • Loading branch information
jrmajor committed Jun 6, 2022
1 parent 64b0907 commit 826f763
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Expand Up @@ -110,6 +110,11 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function acceptsNamedArguments(): bool
{
return true;
}

public function getDocComment(): ?string
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/ChangedTypeMethodReflection.php
Expand Up @@ -89,4 +89,9 @@ public function hasSideEffects(): TrinaryLogic
return $this->reflection->hasSideEffects();
}

public function acceptsNamedArguments(): bool
{
return $this->reflection->acceptsNamedArguments();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyConstructorReflection.php
Expand Up @@ -91,6 +91,11 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function acceptsNamedArguments(): bool
{
return true;
}

public function getDocComment(): ?string
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyMethodReflection.php
Expand Up @@ -91,6 +91,11 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function acceptsNamedArguments(): bool
{
return true;
}

public function getDocComment(): ?string
{
return null;
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/MethodReflection.php
Expand Up @@ -30,4 +30,6 @@ public function getThrowType(): ?Type;

public function hasSideEffects(): TrinaryLogic;

public function acceptsNamedArguments(): bool;

}
5 changes: 5 additions & 0 deletions src/Reflection/Mixin/MixinMethodReflection.php
Expand Up @@ -85,4 +85,9 @@ public function hasSideEffects(): TrinaryLogic
return $this->reflection->hasSideEffects();
}

public function acceptsNamedArguments(): bool
{
return $this->reflection->acceptsNamedArguments();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Native/NativeMethodReflection.php
Expand Up @@ -137,6 +137,11 @@ public function hasSideEffects(): TrinaryLogic
return $this->hasSideEffects;
}

public function acceptsNamedArguments(): bool
{
return true;
}

private function isVoid(): bool
{
foreach ($this->variants as $variant) {
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/ClosureCallMethodReflection.php
Expand Up @@ -118,4 +118,9 @@ public function hasSideEffects(): TrinaryLogic
return $this->nativeMethodReflection->hasSideEffects();
}

public function acceptsNamedArguments(): bool
{
return $this->nativeMethodReflection->acceptsNamedArguments();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Php/EnumCasesMethodReflection.php
Expand Up @@ -105,4 +105,9 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function acceptsNamedArguments(): bool
{
return true;
}

}
6 changes: 6 additions & 0 deletions src/Reflection/Php/PhpMethodReflection.php
Expand Up @@ -78,6 +78,7 @@ public function __construct(
private bool $isInternal,
private bool $isFinal,
private ?bool $isPure,
private bool $acceptsNamedArguments = true,
)
{
}
Expand Down Expand Up @@ -419,4 +420,9 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function acceptsNamedArguments(): bool
{
return $this->acceptsNamedArguments;
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Php/Soap/SoapClientMethodReflection.php
Expand Up @@ -97,4 +97,9 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function acceptsNamedArguments(): bool
{
return true;
}

}
5 changes: 5 additions & 0 deletions src/Reflection/ResolvedMethodReflection.php
Expand Up @@ -115,4 +115,9 @@ public function hasSideEffects(): TrinaryLogic
return $this->reflection->hasSideEffects();
}

public function acceptsNamedArguments(): bool
{
return $this->reflection->acceptsNamedArguments();
}

}
11 changes: 11 additions & 0 deletions src/Reflection/Type/IntersectionTypeMethodReflection.php
Expand Up @@ -147,6 +147,17 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::maxMin(...array_map(static fn (MethodReflection $method): TrinaryLogic => $method->hasSideEffects(), $this->methods));
}

public function acceptsNamedArguments(): bool
{
foreach ($this->methods as $method) {
if (!$method->acceptsNamedArguments()) {
return false;
}
}

return true;
}

public function getDocComment(): ?string
{
return null;
Expand Down
11 changes: 11 additions & 0 deletions src/Reflection/Type/UnionTypeMethodReflection.php
Expand Up @@ -148,6 +148,17 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::extremeIdentity(...array_map(static fn (MethodReflection $method): TrinaryLogic => $method->hasSideEffects(), $this->methods));
}

public function acceptsNamedArguments(): bool
{
foreach ($this->methods as $method) {
if (!$method->acceptsNamedArguments()) {
return false;
}
}

return true;
}

public function getDocComment(): ?string
{
return null;
Expand Down
10 changes: 10 additions & 0 deletions src/Rules/Methods/MethodSignatureRule.php
Expand Up @@ -67,6 +67,16 @@ public function processNode(Node $node, Scope $scope): array
$errors = [];
$declaringClass = $method->getDeclaringClass();
foreach ($this->collectParentMethods($methodName, $method->getDeclaringClass()) as $parentMethod) {
if ($parentMethod->acceptsNamedArguments() && ! $method->acceptsNamedArguments()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Method %s::%s() should accept named arguments as method %s::%s() does',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$parentMethod->getDeclaringClass()->getDisplayName(),
$parentMethod->getName(),
))->build();
}

$parentVariants = $parentMethod->getVariants();
if (count($parentVariants) !== 1) {
continue;
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Expand Up @@ -374,4 +374,20 @@ public function testOverridenMethodWithConditionalReturnType(): void
]);
}

public function testOverridenMethodWithNoNamedArguments(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/no-named-arguments.php'], [
[
'Method NoNamedArguments\Baz::namedArgumentsInParent() should accept named arguments as method NoNamedArguments\Foo::namedArgumentsInParent() does',
10,
],
[
'Method NoNamedArguments\Baz::namedArgumentsInInterface() should accept named arguments as method NoNamedArguments\Bar::namedArgumentsInInterface() does',
15,
],
]);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Methods/data/no-named-arguments.php
@@ -0,0 +1,26 @@
<?php

namespace NoNamedArguments;

class Baz extends Foo implements Bar
{
/**
* @no-named-arguments
*/
public function namedArgumentsInParent(float ...$args) {}

/**
* @no-named-arguments
*/
public function namedArgumentsInInterface(float ...$args) {}
}

abstract class Foo
{
abstract public function namedArgumentsInParent(float ...$args);
}

interface Bar
{
public function namedArgumentsInInterface();
}

0 comments on commit 826f763

Please sign in to comment.