diff --git a/config/extensions.neon b/config/extensions.neon index 4ac8673..3c0e332 100644 --- a/config/extensions.neon +++ b/config/extensions.neon @@ -4,8 +4,3 @@ services: - class: Rector\PHPStanRules\ReturnTypeExtension\FindInstanceOfReturnTypeExtension tags: [phpstan.broker.dynamicMethodReturnTypeExtension] - - # $node->getAttribute($1) => Type|null by $1 - - - class: Rector\PHPStanRules\ReturnTypeExtension\GetAttributeReturnTypeExtension - tags: [phpstan.broker.dynamicMethodReturnTypeExtension] diff --git a/src/ReturnTypeExtension/GetAttributeReturnTypeExtension.php b/src/ReturnTypeExtension/GetAttributeReturnTypeExtension.php deleted file mode 100644 index c316c5f..0000000 --- a/src/ReturnTypeExtension/GetAttributeReturnTypeExtension.php +++ /dev/null @@ -1,82 +0,0 @@ - - */ - private const ARGUMENT_KEY_TO_RETURN_TYPE = [ - 'Rector\NodeTypeResolver\Node\AttributeKey::SCOPE' => Scope::class, - # Node - 'Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE' => Node::class, - ]; - - public function getClass(): string - { - return Node::class; - } - - public function isMethodSupported(MethodReflection $methodReflection): bool - { - return $methodReflection->getName() === 'getAttribute'; - } - - public function getTypeFromMethodCall( - MethodReflection $methodReflection, - MethodCall $methodCall, - Scope $scope - ): Type { - $returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); - - $argumentValue = $this->resolveArgumentValue($methodCall->getArgs()[0]->value); - if ($argumentValue === null) { - return $returnType; - } - - if (! isset(self::ARGUMENT_KEY_TO_RETURN_TYPE[$argumentValue])) { - return $returnType; - } - - $knownReturnType = self::ARGUMENT_KEY_TO_RETURN_TYPE[$argumentValue]; - return new UnionType([new ObjectType($knownReturnType), new NullType()]); - } - - private function resolveArgumentValue(Expr $expr): ?string - { - if ($expr instanceof ClassConstFetch) { - if (! $expr->class instanceof Name) { - return null; - } - - $className = $expr->class->toString(); - - if (! $expr->name instanceof Identifier) { - return null; - } - - $constName = $expr->name->toString(); - - return $className . '::' . $constName; - } - return null; - } -} diff --git a/tests/ReturnTypeExtension/GetAttributeReturnTypeExtension/GetAttributeReturnTypeExtensionTest.php b/tests/ReturnTypeExtension/GetAttributeReturnTypeExtension/GetAttributeReturnTypeExtensionTest.php deleted file mode 100644 index f7443f6..0000000 --- a/tests/ReturnTypeExtension/GetAttributeReturnTypeExtension/GetAttributeReturnTypeExtensionTest.php +++ /dev/null @@ -1,34 +0,0 @@ -assertFileAsserts($assertType, $file, ...$args); - } - - public static function dataAsserts(): Iterator - { - yield from self::gatherAssertTypes(__DIR__ . '/data/get_parent_node.php.inc'); - } - - /** - * @return string[] - */ - public static function getAdditionalConfigFiles(): array - { - return [__DIR__ . '/../../../config/extensions.neon']; - } -} diff --git a/tests/ReturnTypeExtension/GetAttributeReturnTypeExtension/data/get_parent_node.php.inc b/tests/ReturnTypeExtension/GetAttributeReturnTypeExtension/data/get_parent_node.php.inc deleted file mode 100644 index 4adb58e..0000000 --- a/tests/ReturnTypeExtension/GetAttributeReturnTypeExtension/data/get_parent_node.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -getAttribute(AttributeKey::SCOPE); -\PHPStan\Testing\assertType('PHPStan\Analyser\Scope|null', $scope);