From 676a0c7a9ef003f517e53bdaf15b9cc91174f4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:17:28 +0100 Subject: [PATCH 01/12] Removed dead ignores in Psalm configuration --- psalm.xml | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/psalm.xml b/psalm.xml index ae23fd722..dfa0049de 100644 --- a/psalm.xml +++ b/psalm.xml @@ -15,41 +15,21 @@ - + - - - - - - - - - - - - - - - - - - - - From fbe4723d8687ee1be434fd6b7e599ad3b0930c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:18:57 +0100 Subject: [PATCH 02/12] Don't ignore MissingClosureReturnType in Psalm --- psalm.xml | 2 -- src/Reflection/ReflectionFunction.php | 2 +- src/Reflection/ReflectionMethod.php | 8 ++++---- src/Reflection/ReflectionProperty.php | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/psalm.xml b/psalm.xml index dfa0049de..eb112c356 100644 --- a/psalm.xml +++ b/psalm.xml @@ -17,8 +17,6 @@ - - diff --git a/src/Reflection/ReflectionFunction.php b/src/Reflection/ReflectionFunction.php index 0051283cb..45e834231 100644 --- a/src/Reflection/ReflectionFunction.php +++ b/src/Reflection/ReflectionFunction.php @@ -123,7 +123,7 @@ public function getClosure(): Closure $this->assertFunctionExist($functionName); - return static fn (...$args) => $functionName(...$args); + return static fn (...$args): mixed => $functionName(...$args); } /** diff --git a/src/Reflection/ReflectionMethod.php b/src/Reflection/ReflectionMethod.php index 2b38c3dea..bc1bb4678 100644 --- a/src/Reflection/ReflectionMethod.php +++ b/src/Reflection/ReflectionMethod.php @@ -298,12 +298,12 @@ public function getClosure(?object $object = null): Closure if ($this->isStatic()) { $this->assertClassExist($declaringClassName); - return fn (...$args) => $this->callStaticMethod($args); + return fn (...$args): mixed => $this->callStaticMethod($args); } $instance = $this->assertObject($object); - return fn (...$args) => $this->callObjectMethod($instance, $args); + return fn (...$args): mixed => $this->callObjectMethod($instance, $args); } /** @@ -344,7 +344,7 @@ private function callStaticMethod(array $args): mixed $implementingClassName = $this->getImplementingClass()->getName(); /** @psalm-suppress InvalidStringClass */ - $closure = Closure::bind(fn (string $implementingClassName, string $methodName, array $methodArgs) => $implementingClassName::{$methodName}(...$methodArgs), null, $implementingClassName); + $closure = Closure::bind(fn (string $implementingClassName, string $methodName, array $methodArgs): mixed => $implementingClassName::{$methodName}(...$methodArgs), null, $implementingClassName); assert($closure instanceof Closure); @@ -356,7 +356,7 @@ private function callStaticMethod(array $args): mixed */ private function callObjectMethod(object $object, array $args): mixed { - $closure = Closure::bind(fn ($object, string $methodName, array $methodArgs) => $object->{$methodName}(...$methodArgs), $object, $this->getImplementingClass()->getName()); + $closure = Closure::bind(fn ($object, string $methodName, array $methodArgs): mixed => $object->{$methodName}(...$methodArgs), $object, $this->getImplementingClass()->getName()); assert($closure instanceof Closure); diff --git a/src/Reflection/ReflectionProperty.php b/src/Reflection/ReflectionProperty.php index 27a41c106..129a91b21 100644 --- a/src/Reflection/ReflectionProperty.php +++ b/src/Reflection/ReflectionProperty.php @@ -400,7 +400,7 @@ public function getValue(?object $object = null): mixed if ($this->isStatic()) { $this->assertClassExist($implementingClassName); - $closure = Closure::bind(fn (string $implementingClassName, string $propertyName) => $implementingClassName::${$propertyName}, null, $implementingClassName); + $closure = Closure::bind(fn (string $implementingClassName, string $propertyName): mixed => $implementingClassName::${$propertyName}, null, $implementingClassName); assert($closure instanceof Closure); @@ -409,7 +409,7 @@ public function getValue(?object $object = null): mixed $instance = $this->assertObject($object); - $closure = Closure::bind(fn (object $instance, string $propertyName) => $instance->{$propertyName}, $instance, $implementingClassName); + $closure = Closure::bind(fn (object $instance, string $propertyName): mixed => $instance->{$propertyName}, $instance, $implementingClassName); assert($closure instanceof Closure); From a5633850be7550e6eb9a29b6c4fe2baa88699c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:27:47 +0100 Subject: [PATCH 03/12] Don't ignore MissingClosureParamType in Psalm --- psalm.xml | 1 - src/Reflection/ReflectionFunction.php | 2 +- src/Reflection/ReflectionMethod.php | 6 +++--- src/Reflection/ReflectionProperty.php | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/psalm.xml b/psalm.xml index eb112c356..4579f0022 100644 --- a/psalm.xml +++ b/psalm.xml @@ -18,7 +18,6 @@ - diff --git a/src/Reflection/ReflectionFunction.php b/src/Reflection/ReflectionFunction.php index 45e834231..5a7cc25b8 100644 --- a/src/Reflection/ReflectionFunction.php +++ b/src/Reflection/ReflectionFunction.php @@ -123,7 +123,7 @@ public function getClosure(): Closure $this->assertFunctionExist($functionName); - return static fn (...$args): mixed => $functionName(...$args); + return static fn (mixed ...$args): mixed => $functionName(...$args); } /** diff --git a/src/Reflection/ReflectionMethod.php b/src/Reflection/ReflectionMethod.php index bc1bb4678..7ee7fd0d6 100644 --- a/src/Reflection/ReflectionMethod.php +++ b/src/Reflection/ReflectionMethod.php @@ -298,12 +298,12 @@ public function getClosure(?object $object = null): Closure if ($this->isStatic()) { $this->assertClassExist($declaringClassName); - return fn (...$args): mixed => $this->callStaticMethod($args); + return fn (mixed ...$args): mixed => $this->callStaticMethod($args); } $instance = $this->assertObject($object); - return fn (...$args): mixed => $this->callObjectMethod($instance, $args); + return fn (mixed ...$args): mixed => $this->callObjectMethod($instance, $args); } /** @@ -356,7 +356,7 @@ private function callStaticMethod(array $args): mixed */ private function callObjectMethod(object $object, array $args): mixed { - $closure = Closure::bind(fn ($object, string $methodName, array $methodArgs): mixed => $object->{$methodName}(...$methodArgs), $object, $this->getImplementingClass()->getName()); + $closure = Closure::bind(fn (object $object, string $methodName, array $methodArgs): mixed => $object->{$methodName}(...$methodArgs), $object, $this->getImplementingClass()->getName()); assert($closure instanceof Closure); diff --git a/src/Reflection/ReflectionProperty.php b/src/Reflection/ReflectionProperty.php index 129a91b21..c7606f20f 100644 --- a/src/Reflection/ReflectionProperty.php +++ b/src/Reflection/ReflectionProperty.php @@ -429,7 +429,7 @@ public function setValue(mixed $object, mixed $value = null): void if ($this->isStatic()) { $this->assertClassExist($implementingClassName); - $closure = Closure::bind(function (string $implementingClassName, string $propertyName, $value): void { + $closure = Closure::bind(function (string $implementingClassName, string $propertyName, mixed $value): void { $implementingClassName::${$propertyName} = $value; }, null, $implementingClassName); @@ -442,7 +442,7 @@ public function setValue(mixed $object, mixed $value = null): void $instance = $this->assertObject($object); - $closure = Closure::bind(function ($instance, string $propertyName, $value): void { + $closure = Closure::bind(function (object $instance, string $propertyName, mixed $value): void { $instance->{$propertyName} = $value; }, $instance, $implementingClassName); From 695ba800b38ad4ff71ae421b9549178634b39621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:07:19 +0100 Subject: [PATCH 04/12] ReflectionParameter cleanup --- src/Reflection/ReflectionParameter.php | 36 +++++++++++--------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/Reflection/ReflectionParameter.php b/src/Reflection/ReflectionParameter.php index dc6ded722..e793cd34b 100644 --- a/src/Reflection/ReflectionParameter.php +++ b/src/Reflection/ReflectionParameter.php @@ -33,22 +33,18 @@ class ReflectionParameter { - private ParamNode $node; - - private ?Namespace_ $declaringNamespace; - - private ReflectionMethod|ReflectionFunction $function; - - private int $parameterIndex; - private bool $isOptional; private ?CompiledValue $compiledDefaultValue = null; - private Reflector $reflector; - - private function __construct() - { + private function __construct( + private Reflector $reflector, + private ParamNode $node, + private ?Namespace_ $declaringNamespace, + private ReflectionMethod|ReflectionFunction $function, + private int $parameterIndex, + ) { + $this->isOptional = $this->detectIsOptional(); } /** @@ -142,15 +138,13 @@ public static function createFromNode( ReflectionMethod|ReflectionFunction $function, int $parameterIndex, ): self { - $param = new self(); - $param->reflector = $reflector; - $param->node = $node; - $param->declaringNamespace = $declaringNamespace; - $param->function = $function; - $param->parameterIndex = $parameterIndex; - $param->isOptional = $param->detectIsOptional(); - - return $param; + return new self( + $reflector, + $node, + $declaringNamespace, + $function, + $parameterIndex, + ); } /** From d350054478805ae2b434c5e8a52abaa83f3e7ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:32:28 +0100 Subject: [PATCH 05/12] ReflectionEnumCase cleanup --- src/Reflection/ReflectionEnumCase.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Reflection/ReflectionEnumCase.php b/src/Reflection/ReflectionEnumCase.php index 90622280c..fd507cd9c 100644 --- a/src/Reflection/ReflectionEnumCase.php +++ b/src/Reflection/ReflectionEnumCase.php @@ -22,16 +22,13 @@ class ReflectionEnumCase { - private Reflector $reflector; - - private EnumCase $node; - - private ReflectionEnum $enum; - private ?CompiledValue $compiledValue = null; - private function __construct() - { + private function __construct( + private Reflector $reflector, + private EnumCase $node, + private ReflectionEnum $enum, + ) { } /** @@ -42,12 +39,7 @@ public static function createFromNode( EnumCase $node, ReflectionEnum $enum, ): self { - $reflection = new self(); - $reflection->reflector = $reflector; - $reflection->node = $node; - $reflection->enum = $enum; - - return $reflection; + return new self($reflector, $node, $enum); } public function getName(): string From 46cd667b8e96bc9122983b7bc81ae49ae311d769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:34:08 +0100 Subject: [PATCH 06/12] ReflectionClassConstant cleanup --- src/Reflection/ReflectionClassConstant.php | 32 ++++++++-------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/Reflection/ReflectionClassConstant.php b/src/Reflection/ReflectionClassConstant.php index 246028d75..5355acba1 100644 --- a/src/Reflection/ReflectionClassConstant.php +++ b/src/Reflection/ReflectionClassConstant.php @@ -22,25 +22,18 @@ class ReflectionClassConstant private ?CompiledValue $compiledValue = null; - private Reflector $reflector; - - /** @var ReflectionClass Constant owner */ - private ReflectionClass $owner; - - private ClassConst $node; - - private int $positionInNode; - - private function __construct() - { + private function __construct( + private Reflector $reflector, + private ClassConst $node, + private ReflectionClass $owner, + private int $positionInNode, + ) { } /** * Create a reflection of a class's constant by Const Node * * @internal - * - * @param ClassConst $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver */ public static function createFromNode( Reflector $reflector, @@ -48,13 +41,12 @@ public static function createFromNode( int $positionInNode, ReflectionClass $owner, ): self { - $ref = new self(); - $ref->node = $node; - $ref->positionInNode = $positionInNode; - $ref->owner = $owner; - $ref->reflector = $reflector; - - return $ref; + return new self( + $reflector, + $node, + $owner, + $positionInNode, + ); } /** From 8b9ecff7aa6355faf62ff0c01e58614b128d109a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:36:33 +0100 Subject: [PATCH 07/12] ReflectionConstant cleanup --- src/Reflection/ReflectionConstant.php | 45 +++++++++++---------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/Reflection/ReflectionConstant.php b/src/Reflection/ReflectionConstant.php index 84a8bd801..18736f791 100644 --- a/src/Reflection/ReflectionConstant.php +++ b/src/Reflection/ReflectionConstant.php @@ -29,20 +29,15 @@ class ReflectionConstant implements Reflection { - private Reflector $reflector; - - private Node\Stmt\Const_|Node\Expr\FuncCall $node; - - private LocatedSource $locatedSource; - - private ?NamespaceNode $declaringNamespace; - - private ?int $positionInNode; - private ?CompiledValue $compiledValue = null; - private function __construct() - { + private function __construct( + private Reflector $reflector, + private Node\Stmt\Const_|Node\Expr\FuncCall $node, + private LocatedSource $locatedSource, + private ?NamespaceNode $declaringNamespace = null, + private ?int $positionInNode = null, + ) { } /** @@ -81,14 +76,13 @@ private static function createFromConstKeyword( ?NamespaceNode $namespace, int $positionInNode, ): self { - $constant = new self(); - $constant->reflector = $reflector; - $constant->node = $node; - $constant->locatedSource = $locatedSource; - $constant->declaringNamespace = $namespace; - $constant->positionInNode = $positionInNode; - - return $constant; + return new self( + $reflector, + $node, + $locatedSource, + $namespace, + $positionInNode, + ); } /** @@ -101,12 +95,11 @@ private static function createFromDefineFunctionCall( ): self { ConstantNodeChecker::assertValidDefineFunctionCall($node); - $constant = new self(); - $constant->reflector = $reflector; - $constant->node = $node; - $constant->locatedSource = $locatedSource; - - return $constant; + return new self( + $reflector, + $node, + $locatedSource, + ); } /** From 57f948611606fd96e84b430d4e95bfc963a6497b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:41:21 +0100 Subject: [PATCH 08/12] ReflectionProperty cleanup --- src/Reflection/ReflectionClass.php | 4 -- src/Reflection/ReflectionObject.php | 1 - src/Reflection/ReflectionProperty.php | 51 +++++++------------ .../Reflection/ReflectionPropertyTest.php | 1 - 4 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index a560ce5b3..2e8c00bf2 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -731,7 +731,6 @@ public function getImmediateProperties(?int $filter = null): array $this->reflector, $propertiesNode, $propertyPositionInNode, - $this->declaringNamespace, $this, $this, false, @@ -765,7 +764,6 @@ public function getImmediateProperties(?int $filter = null): array $this->reflector, $propertyNode, 0, - $this->declaringNamespace, $this, $this, true, @@ -812,7 +810,6 @@ private function addEnumProperties(array $properties): array $this->reflector, $propertyNode, 0, - $this->declaringNamespace, $this, $this, false, @@ -864,7 +861,6 @@ function (ReflectionClass $trait) use ($filter) { $this->reflector, $property->getAst(), $property->getPositionInAst(), - $trait->declaringNamespace, $property->getDeclaringClass(), $this, $property->isPromoted(), diff --git a/src/Reflection/ReflectionObject.php b/src/Reflection/ReflectionObject.php index fb81eac71..90b051fa6 100644 --- a/src/Reflection/ReflectionObject.php +++ b/src/Reflection/ReflectionObject.php @@ -89,7 +89,6 @@ private function getRuntimeProperties(?int $filter = null): array $this->reflector, $this->createPropertyNodeFromReflection($property, $this->object), 0, - $this->reflectionClass->getDeclaringNamespaceAst(), $this, $this, false, diff --git a/src/Reflection/ReflectionProperty.php b/src/Reflection/ReflectionProperty.php index c7606f20f..2c8bf7ff6 100644 --- a/src/Reflection/ReflectionProperty.php +++ b/src/Reflection/ReflectionProperty.php @@ -11,7 +11,6 @@ use PhpParser\Node; use PhpParser\Node\NullableType; use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Property as PropertyNode; use ReflectionException; use ReflectionProperty as CoreReflectionProperty; @@ -42,26 +41,17 @@ class ReflectionProperty { - private ReflectionClass $declaringClass; - - private ReflectionClass $implementingClass; - - private PropertyNode $node; - - private int $positionInNode; - - private ?Namespace_ $declaringNamespace; - - private bool $isPromoted; - - private bool $declaredAtCompileTime = true; - - private Reflector $reflector; - private ?CompiledValue $compiledDefaultValue = null; - private function __construct() - { + private function __construct( + private Reflector $reflector, + private PropertyNode $node, + private int $positionInNode, + private ReflectionClass $declaringClass, + private ReflectionClass $implementingClass, + private bool $isPromoted, + private bool $declaredAtCompileTime = true, + ) { } /** @@ -97,23 +87,20 @@ public static function createFromNode( Reflector $reflector, PropertyNode $node, int $positionInNode, - ?Namespace_ $declaringNamespace, ReflectionClass $declaringClass, ReflectionClass $implementingClass, bool $isPromoted, bool $declaredAtCompileTime = true, ): self { - $prop = new self(); - $prop->reflector = $reflector; - $prop->node = $node; - $prop->positionInNode = $positionInNode; - $prop->declaringNamespace = $declaringNamespace; - $prop->declaringClass = $declaringClass; - $prop->implementingClass = $implementingClass; - $prop->isPromoted = $isPromoted; - $prop->declaredAtCompileTime = $declaredAtCompileTime; - - return $prop; + return new self( + $reflector, + $node, + $positionInNode, + $declaringClass, + $implementingClass, + $isPromoted, + $declaredAtCompileTime, + ); } /** @@ -261,7 +248,7 @@ public function getDocBlockTypeStrings(): array */ public function getDocBlockTypes(): array { - return (new FindPropertyType())->__invoke($this, $this->declaringNamespace); + return (new FindPropertyType())->__invoke($this, $this->declaringClass->getDeclaringNamespaceAst()); } public function getDeclaringClass(): ReflectionClass diff --git a/test/unit/Reflection/ReflectionPropertyTest.php b/test/unit/Reflection/ReflectionPropertyTest.php index f8424f76d..9e9c9446c 100644 --- a/test/unit/Reflection/ReflectionPropertyTest.php +++ b/test/unit/Reflection/ReflectionPropertyTest.php @@ -262,7 +262,6 @@ public function testIsDefaultWithRuntimeDeclaredProperty(): void $this->reflector, new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo')]), 0, - null, $classInfo, $classInfo, false, From e3a03cbf37dc587688b73a3dea3d1ed38f6b929c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 15:12:13 +0100 Subject: [PATCH 09/12] ReflectionMethod and ReflectionFunction cleanup --- src/Reflection/ReflectionFunction.php | 17 ++++++-- src/Reflection/ReflectionFunctionAbstract.php | 10 ----- src/Reflection/ReflectionMethod.php | 43 +++++++++++-------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/Reflection/ReflectionFunction.php b/src/Reflection/ReflectionFunction.php index 5a7cc25b8..ca5086b0a 100644 --- a/src/Reflection/ReflectionFunction.php +++ b/src/Reflection/ReflectionFunction.php @@ -18,6 +18,7 @@ use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; use Roave\BetterReflection\SourceLocator\Type\ClosureSourceLocator; +use function assert; use function function_exists; class ReflectionFunction implements Reflection @@ -28,6 +29,17 @@ class ReflectionFunction implements Reflection private Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $functionNode; + private function __construct( + private Reflector $reflector, + private Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node, + private LocatedSource $locatedSource, + private ?NamespaceNode $declaringNamespace = null, + ) { + assert($node instanceof Node\Stmt\Function_ || $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction); + + $this->functionNode = $node; + } + /** * @throws IdentifierNotFound */ @@ -63,10 +75,7 @@ public static function createFromNode( LocatedSource $locatedSource, ?NamespaceNode $namespaceNode = null, ): self { - $function = new self($reflector, $node, $locatedSource, $namespaceNode); - $function->functionNode = $node; - - return $function; + return new self($reflector, $node, $locatedSource, $namespaceNode); } public function getAst(): Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction diff --git a/src/Reflection/ReflectionFunctionAbstract.php b/src/Reflection/ReflectionFunctionAbstract.php index 0ff769fbf..4ba112c95 100644 --- a/src/Reflection/ReflectionFunctionAbstract.php +++ b/src/Reflection/ReflectionFunctionAbstract.php @@ -11,7 +11,6 @@ use PhpParser\Node\Expr\Yield_ as YieldNode; use PhpParser\Node\Expr\YieldFrom as YieldFromNode; use PhpParser\Node\Param as ParamNode; -use PhpParser\Node\Stmt\Namespace_ as NamespaceNode; use PhpParser\NodeTraverser; use PhpParser\Parser; use PhpParser\PrettyPrinter\Standard as StandardPrettyPrinter; @@ -24,7 +23,6 @@ use Roave\BetterReflection\Reflection\Attribute\ReflectionAttributeHelper; use Roave\BetterReflection\Reflection\Exception\InvalidArrowFunctionBodyNode; use Roave\BetterReflection\Reflection\Exception\Uncloneable; -use Roave\BetterReflection\Reflector\Reflector; use Roave\BetterReflection\SourceLocator\Ast\Exception\ParseToAstFailure; use Roave\BetterReflection\SourceLocator\Located\LocatedSource; use Roave\BetterReflection\SourceLocator\Type\ClosureSourceLocator; @@ -44,14 +42,6 @@ trait ReflectionFunctionAbstract { private static ?Parser $parser; - private function __construct( - private Reflector $reflector, - private Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node, - private LocatedSource $locatedSource, - private ?NamespaceNode $declaringNamespace = null, - ) { - } - abstract public function __toString(): string; abstract public function getShortName(): string; diff --git a/src/Reflection/ReflectionMethod.php b/src/Reflection/ReflectionMethod.php index 7ee7fd0d6..984d5eca5 100644 --- a/src/Reflection/ReflectionMethod.php +++ b/src/Reflection/ReflectionMethod.php @@ -6,8 +6,10 @@ use Closure; use OutOfBoundsException; +use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod as MethodNode; use PhpParser\Node\Stmt\Namespace_; +use PhpParser\Node\Stmt\Namespace_ as NamespaceNode; use ReflectionException; use ReflectionMethod as CoreReflectionMethod; use Roave\BetterReflection\Reflection\Exception\ClassDoesNotExist; @@ -28,20 +30,25 @@ class ReflectionMethod { use ReflectionFunctionAbstract; - private ReflectionClass $declaringClass; - - private ReflectionClass $implementingClass; - - private ReflectionClass $currentClass; - private MethodNode $methodNode; - private ?string $aliasName; + private function __construct( + private Reflector $reflector, + private MethodNode|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node, + private LocatedSource $locatedSource, + private ?NamespaceNode $declaringNamespace, + private ReflectionClass $declaringClass, + private ReflectionClass $implementingClass, + private ReflectionClass $currentClass, + private ?string $aliasName, + ) { + assert($node instanceof MethodNode); + + $this->methodNode = $node; + } /** * @internal - * - * @param MethodNode $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver */ public static function createFromNode( Reflector $reflector, @@ -53,14 +60,16 @@ public static function createFromNode( ReflectionClass $currentClass, ?string $aliasName = null, ): self { - $method = new self($reflector, $node, $locatedSource, $namespace); - $method->declaringClass = $declaringClass; - $method->implementingClass = $implementingClass; - $method->currentClass = $currentClass; - $method->methodNode = $node; - $method->aliasName = $aliasName; - - return $method; + return new self( + $reflector, + $node, + $locatedSource, + $namespace, + $declaringClass, + $implementingClass, + $currentClass, + $aliasName, + ); } /** From c23d3aea6d3f3a4c3eed6883b86a027ef5c4928e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:59:31 +0100 Subject: [PATCH 10/12] Don't ignore PropertyNotSetInConstructor in Psalm globally --- psalm.xml | 2 -- src/BetterReflection.php | 12 ++++++------ src/Reflection/Adapter/ReflectionClass.php | 3 +++ src/Reflection/Adapter/ReflectionEnum.php | 3 +++ src/SourceLocator/Type/AutoloadSourceLocator.php | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/psalm.xml b/psalm.xml index 4579f0022..33f50928b 100644 --- a/psalm.xml +++ b/psalm.xml @@ -17,8 +17,6 @@ - - diff --git a/src/BetterReflection.php b/src/BetterReflection.php index 2dc1be1e1..1b8b22d9f 100644 --- a/src/BetterReflection.php +++ b/src/BetterReflection.php @@ -25,17 +25,17 @@ final class BetterReflection { - private ?SourceLocator $sourceLocator; + private ?SourceLocator $sourceLocator = null; - private ?Reflector $reflector; + private ?Reflector $reflector = null; - private ?Parser $phpParser; + private ?Parser $phpParser = null; - private ?AstLocator $astLocator; + private ?AstLocator $astLocator = null; - private ?FindReflectionOnLine $findReflectionOnLine; + private ?FindReflectionOnLine $findReflectionOnLine = null; - private ?SourceStubber $sourceStubber; + private ?SourceStubber $sourceStubber = null; public function sourceLocator(): SourceLocator { diff --git a/src/Reflection/Adapter/ReflectionClass.php b/src/Reflection/Adapter/ReflectionClass.php index 7fc9b6b37..fba1fbd62 100644 --- a/src/Reflection/Adapter/ReflectionClass.php +++ b/src/Reflection/Adapter/ReflectionClass.php @@ -28,6 +28,9 @@ use function sprintf; use function strtolower; +/** + * @psalm-suppress PropertyNotSetInConstructor + */ final class ReflectionClass extends CoreReflectionClass { public function __construct(private BetterReflectionClass|BetterReflectionEnum $betterReflectionClass) diff --git a/src/Reflection/Adapter/ReflectionEnum.php b/src/Reflection/Adapter/ReflectionEnum.php index fd736ed75..912bdc097 100644 --- a/src/Reflection/Adapter/ReflectionEnum.php +++ b/src/Reflection/Adapter/ReflectionEnum.php @@ -28,6 +28,9 @@ use function sprintf; use function strtolower; +/** + * @psalm-suppress PropertyNotSetInConstructor + */ final class ReflectionEnum extends CoreReflectionEnum { public function __construct(private BetterReflectionEnum $betterReflectionEnum) diff --git a/src/SourceLocator/Type/AutoloadSourceLocator.php b/src/SourceLocator/Type/AutoloadSourceLocator.php index 8bd8798e2..9627cbc95 100644 --- a/src/SourceLocator/Type/AutoloadSourceLocator.php +++ b/src/SourceLocator/Type/AutoloadSourceLocator.php @@ -294,7 +294,7 @@ private function createConstantVisitor(): NodeVisitorAbstract { return new class () extends NodeVisitorAbstract { - private ?string $constantName; + private ?string $constantName = null; private Node\Stmt\Const_|Node\Expr\FuncCall|null $node = null; From b4832be0d77bd5c0ecb2bd3616a55deaea2f84d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 14:49:22 +0100 Subject: [PATCH 11/12] Don't ignore PropertyTypeCoercion in Psalm globally --- psalm.xml | 2 -- .../SourceStubber/PhpStormStubsSourceStubber.php | 7 +++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/psalm.xml b/psalm.xml index 33f50928b..910620072 100644 --- a/psalm.xml +++ b/psalm.xml @@ -24,7 +24,5 @@ - - diff --git a/src/SourceLocator/SourceStubber/PhpStormStubsSourceStubber.php b/src/SourceLocator/SourceStubber/PhpStormStubsSourceStubber.php index 794cfb9ff..3ecc5d67f 100644 --- a/src/SourceLocator/SourceStubber/PhpStormStubsSourceStubber.php +++ b/src/SourceLocator/SourceStubber/PhpStormStubsSourceStubber.php @@ -187,8 +187,11 @@ public function __construct(private Parser $phpParser, private ?int $phpVersion return; } - self::$classMap = array_change_key_case(PhpStormStubsMap::CLASSES); - self::$functionMap = array_change_key_case(PhpStormStubsMap::FUNCTIONS); + /** @psalm-suppress PropertyTypeCoercion */ + self::$classMap = array_change_key_case(PhpStormStubsMap::CLASSES); + /** @psalm-suppress PropertyTypeCoercion */ + self::$functionMap = array_change_key_case(PhpStormStubsMap::FUNCTIONS); + /** @psalm-suppress PropertyTypeCoercion */ self::$constantMap = array_change_key_case(PhpStormStubsMap::CONSTANTS); self::$mapsInitialized = true; } From 9fde8ea0f764391172906cc1d9c86a229bacbf2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 13 Nov 2021 18:46:48 +0100 Subject: [PATCH 12/12] Don't ignore MissingImmutableAnnotation in Psalm globally --- psalm.xml | 2 -- src/Reflection/Adapter/ReflectionNamedType.php | 3 +++ src/Reflection/Adapter/ReflectionParameter.php | 3 +++ src/Reflection/Adapter/ReflectionProperty.php | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/psalm.xml b/psalm.xml index 910620072..5bc71f75e 100644 --- a/psalm.xml +++ b/psalm.xml @@ -15,8 +15,6 @@ - - diff --git a/src/Reflection/Adapter/ReflectionNamedType.php b/src/Reflection/Adapter/ReflectionNamedType.php index 7bf56f301..313f87262 100644 --- a/src/Reflection/Adapter/ReflectionNamedType.php +++ b/src/Reflection/Adapter/ReflectionNamedType.php @@ -7,6 +7,9 @@ use ReflectionNamedType as CoreReflectionNamedType; use Roave\BetterReflection\Reflection\ReflectionNamedType as BetterReflectionNamedType; +/** + * @psalm-suppress MissingImmutableAnnotation + */ final class ReflectionNamedType extends CoreReflectionNamedType { public function __construct(private BetterReflectionNamedType $betterReflectionType) diff --git a/src/Reflection/Adapter/ReflectionParameter.php b/src/Reflection/Adapter/ReflectionParameter.php index e0ed1e5e5..2ec9de6d4 100644 --- a/src/Reflection/Adapter/ReflectionParameter.php +++ b/src/Reflection/Adapter/ReflectionParameter.php @@ -14,6 +14,9 @@ use function array_map; +/** + * @psalm-suppress MissingImmutableAnnotation + */ final class ReflectionParameter extends CoreReflectionParameter { public function __construct(private BetterReflectionParameter $betterReflectionParameter) diff --git a/src/Reflection/Adapter/ReflectionProperty.php b/src/Reflection/Adapter/ReflectionProperty.php index 4f1babbf1..d0f763ab4 100644 --- a/src/Reflection/Adapter/ReflectionProperty.php +++ b/src/Reflection/Adapter/ReflectionProperty.php @@ -75,8 +75,12 @@ public function hasType(): bool return $this->betterReflectionProperty->hasType(); } + /** + * @psalm-mutation-free + */ public function getType(): ReflectionUnionType|ReflectionNamedType|ReflectionIntersectionType|null { + /** @psalm-suppress ImpureMethodCall */ return ReflectionType::fromTypeOrNull($this->betterReflectionProperty->getType()); }