Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.10.x' into 1.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 8, 2023
2 parents 5028e74 + 4b2ffb8 commit 365b54a
Show file tree
Hide file tree
Showing 20 changed files with 227 additions and 75 deletions.
5 changes: 5 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isFinalByKeyword(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function isFinal(): TrinaryLogic
return $this->reflection->isFinal();
}

public function isFinalByKeyword(): TrinaryLogic
{
return $this->reflection->isFinalByKeyword();
}

public function isInternal(): TrinaryLogic
{
return $this->reflection->isInternal();
Expand Down
33 changes: 29 additions & 4 deletions src/Reflection/Dummy/DummyConstructorReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace PHPStan\Reflection\Dummy;

use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;

class DummyConstructorReflection implements MethodReflection
class DummyConstructorReflection implements ExtendedMethodReflection
{

public function __construct(private ClassReflection $declaringClass)
Expand Down Expand Up @@ -51,12 +53,15 @@ public function getPrototype(): ClassMemberReflection
public function getVariants(): array
{
return [
new FunctionVariant(
new FunctionVariantWithPhpDocs(
TemplateTypeMap::createEmpty(),
null,
[],
false,
new VoidType(),
new MixedType(),
new MixedType(),
null,
),
];
}
Expand Down Expand Up @@ -96,4 +101,24 @@ public function getDocComment(): ?string
return null;
}

public function getAsserts(): Assertions
{
return Assertions::createEmpty();
}

public function getSelfOutType(): ?Type
{
return null;
}

public function returnsByReference(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isFinalByKeyword(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isFinalByKeyword(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/ExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public function getSelfOutType(): ?Type;

public function returnsByReference(): TrinaryLogic;

public function isFinalByKeyword(): TrinaryLogic;

}
5 changes: 5 additions & 0 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::createFromBoolean($this->reflection->isFinal());
}

public function isFinalByKeyword(): TrinaryLogic
{
return $this->isFinal();
}

public function getThrowType(): ?Type
{
return $this->throwType;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/ClosureCallMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public function isFinal(): TrinaryLogic
return $this->nativeMethodReflection->isFinal();
}

public function isFinalByKeyword(): TrinaryLogic
{
return $this->nativeMethodReflection->isFinalByKeyword();
}

public function isInternal(): TrinaryLogic
{
return $this->nativeMethodReflection->isInternal();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/EnumCasesMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function isFinalByKeyword(): TrinaryLogic
{
return TrinaryLogic::createYes();
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
9 changes: 9 additions & 0 deletions src/Reflection/Php/PhpFunctionFromParserNodeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::createFromBoolean($finalMethod || $this->isFinal);
}

public function isFinalByKeyword(): TrinaryLogic
{
$finalMethod = false;
if ($this->functionLike instanceof ClassMethod) {
$finalMethod = $this->functionLike->isFinal();
}
return TrinaryLogic::createFromBoolean($finalMethod);
}

public function getThrowType(): ?Type
{
return $this->throwType;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::createFromBoolean($this->isFinal || $this->reflection->isFinal());
}

public function isFinalByKeyword(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->reflection->isFinal());
}

public function isAbstract(): bool
{
return $this->reflection->isAbstract();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/ResolvedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public function isFinal(): TrinaryLogic
return $this->reflection->isFinal();
}

public function isFinalByKeyword(): TrinaryLogic
{
return $this->reflection->isFinalByKeyword();
}

public function isInternal(): TrinaryLogic
{
return $this->reflection->isInternal();
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Type/IntersectionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::lazyMaxMin($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->isFinal());
}

public function isFinalByKeyword(): TrinaryLogic
{
return TrinaryLogic::lazyMaxMin($this->methods, static fn (ExtendedMethodReflection $method): TrinaryLogic => $method->isFinalByKeyword());
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::lazyMaxMin($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->isInternal());
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Type/UnionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public function isFinal(): TrinaryLogic
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->isFinal());
}

public function isFinalByKeyword(): TrinaryLogic
{
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (ExtendedMethodReflection $method): TrinaryLogic => $method->isFinalByKeyword());
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->isInternal());
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/WrappedExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function isFinal(): TrinaryLogic
return $this->method->isFinal();
}

public function isFinalByKeyword(): TrinaryLogic
{
return $this->isFinal();
}

public function isInternal(): TrinaryLogic
{
return $this->method->isInternal();
Expand Down
45 changes: 1 addition & 44 deletions src/Rules/Methods/ConsistentConstructorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
use PHPStan\Reflection\MethodPrototypeReflection;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Rules\Rule;
use function strtolower;

Expand Down Expand Up @@ -43,54 +40,14 @@ public function processNode(Node $node, Scope $scope): array
if ($parent->hasConstructor()) {
$parentConstructor = $parent->getConstructor();
} else {
$parentConstructor = $this->getEmptyConstructor($parent);
}

if (! $parentConstructor instanceof PhpMethodReflection && ! $parentConstructor instanceof MethodPrototypeReflection) {
return [];
$parentConstructor = new DummyConstructorReflection($parent);
}

if (! $parentConstructor->getDeclaringClass()->hasConsistentConstructor()) {
return [];
}

if (! $parentConstructor instanceof MethodPrototypeReflection) {
$parentConstructor = $this->getMethodPrototypeReflection($parentConstructor, $parent);
}

return $this->methodParameterComparisonHelper->compare($parentConstructor, $method, true);
}

private function getMethodPrototypeReflection(PhpMethodReflection $methodReflection, ClassReflection $classReflection): MethodPrototypeReflection
{
return new MethodPrototypeReflection(
$methodReflection->getName(),
$classReflection,
$methodReflection->isStatic(),
$methodReflection->isPrivate(),
$methodReflection->isPublic(),
$methodReflection->isAbstract(),
$methodReflection->isFinal()->yes(),
$classReflection->getNativeMethod($methodReflection->getName())->getVariants(),
null,
);
}

private function getEmptyConstructor(ClassReflection $classReflection): MethodPrototypeReflection
{
$emptyConstructor = new DummyConstructorReflection($classReflection);

return new MethodPrototypeReflection(
$emptyConstructor->getName(),
$classReflection,
$emptyConstructor->isStatic(),
$emptyConstructor->isPrivate(),
$emptyConstructor->isPublic(),
false,
$emptyConstructor->isFinal()->yes(),
$emptyConstructor->getVariants(),
null,
);
}

}
12 changes: 2 additions & 10 deletions src/Rules/Methods/MethodParameterComparisonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace PHPStan\Rules\Methods;

use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\MethodPrototypeReflection;
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\IdentifierRuleError;
Expand All @@ -31,7 +30,7 @@ public function __construct(private PhpVersion $phpVersion, private bool $generi
/**
* @return list<IdentifierRuleError>
*/
public function compare(MethodPrototypeReflection $prototype, PhpMethodFromParserNodeReflection $method, bool $ignorable = false): array
public function compare(ExtendedMethodReflection $prototype, PhpMethodFromParserNodeReflection $method, bool $ignorable = false): array
{
/** @var list<IdentifierRuleError> $messages */
$messages = [];
Expand Down Expand Up @@ -164,9 +163,6 @@ public function compare(MethodPrototypeReflection $prototype, PhpMethodFromParse
if ($this->phpVersion->supportsLessOverridenParametersWithVariadic()) {
$remainingPrototypeParameters = array_slice($prototypeVariant->getParameters(), $i);
foreach ($remainingPrototypeParameters as $j => $remainingPrototypeParameter) {
if (!$remainingPrototypeParameter instanceof ParameterReflectionWithPhpDocs) {
continue;
}
if ($methodParameter->getNativeType()->isSuperTypeOf($remainingPrototypeParameter->getNativeType())->yes()) {
continue;
}
Expand Down Expand Up @@ -236,10 +232,6 @@ public function compare(MethodPrototypeReflection $prototype, PhpMethodFromParse

$methodParameterType = $methodParameter->getNativeType();

if (!$prototypeParameter instanceof ParameterReflectionWithPhpDocs) {
continue;
}

$prototypeParameterType = $prototypeParameter->getNativeType();
if (!$this->phpVersion->supportsParameterTypeWidening()) {
if (!$methodParameterType->equals($prototypeParameterType)) {
Expand Down
Loading

0 comments on commit 365b54a

Please sign in to comment.