From 40fbe10f105f92d3c895750d1c37c977be951e82 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 16 Jul 2021 00:55:31 +0200 Subject: [PATCH] [Generics] Remove GenericsPHPStormMethodAnnotationRector, now native in PHPStorm (#444) --- composer.json | 1 - .../Fixture/array_like.php.inc | 38 ----- .../Fixture/array_like_nullable.php.inc | 38 ----- .../Fixture/child_repository.php.inc | 38 ----- .../Fixture/direct_pass_repository.php.inc | 36 ----- .../Fixture/nullable_get.php.inc | 38 ----- .../Fixture/real_get_by_id.php.inc | 38 ----- .../Fixture/skip_already_method.php.inc | 17 --- .../Fixture/skip_implemented_method.php.inc | 20 --- ...ricsPHPStormMethodAnnotationRectorTest.php | 33 ----- .../Source/AbstractGenericArrayRepository.php | 18 --- .../AbstractGenericMaybeArrayRepository.php | 18 --- .../Source/AbstractGenericRepository.php | 18 --- .../Source/AbstractMaybeGenericRepository.php | 18 --- .../Source/AbstractRealRepository.php | 20 --- .../Source/ChildRealObject.php | 9 -- .../Source/RealObject.php | 10 -- .../config/configured_rule.php | 11 -- .../NodeAnalyzer/ExprUsedInNodeAnalyzer.php | 1 - .../UnnededMethodTagValueNodeFilter.php | 89 ----------- .../ExtendsTemplateTypeMapFallbackFactory.php | 108 -------------- .../NodeType/GenericTypeSpecifier.php | 71 --------- ...GenericsPHPStormMethodAnnotationRector.php | 140 ------------------ .../Reflection/ClassGenericMethodResolver.php | 83 ----------- .../Reflection/ClassMethodAnalyzer.php | 26 ---- .../GenericClassReflectionAnalyzer.php | 69 --------- .../MethodTagValueNodeFactory.php | 133 ----------------- .../MethodTagValueParameterNodeFactory.php | 42 ------ .../ChildParentClassReflections.php | 38 ----- 29 files changed, 1219 deletions(-) delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like_nullable.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/child_repository.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/direct_pass_repository.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/nullable_get.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/real_get_by_id.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_already_method.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_implemented_method.php.inc delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/GenericsPHPStormMethodAnnotationRectorTest.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericArrayRepository.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericMaybeArrayRepository.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericRepository.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractMaybeGenericRepository.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractRealRepository.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/ChildRealObject.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/RealObject.php delete mode 100644 rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/config/configured_rule.php delete mode 100644 rules/Generics/Filter/UnnededMethodTagValueNodeFilter.php delete mode 100644 rules/Generics/NodeType/ExtendsTemplateTypeMapFallbackFactory.php delete mode 100644 rules/Generics/NodeType/GenericTypeSpecifier.php delete mode 100644 rules/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector.php delete mode 100644 rules/Generics/Reflection/ClassGenericMethodResolver.php delete mode 100644 rules/Generics/Reflection/ClassMethodAnalyzer.php delete mode 100644 rules/Generics/Reflection/GenericClassReflectionAnalyzer.php delete mode 100644 rules/Generics/TagValueNodeFactory/MethodTagValueNodeFactory.php delete mode 100644 rules/Generics/TagValueNodeFactory/MethodTagValueParameterNodeFactory.php delete mode 100644 rules/Generics/ValueObject/ChildParentClassReflections.php diff --git a/composer.json b/composer.json index f22db84bf506..ea87454a8a64 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,6 @@ "phpunit/phpunit": "^9.5", "rector/phpstan-rules": "^0.3.3", "rector/rector-generator": "^0.3", - "symplify/coding-standard": "^9.4.14", "symplify/easy-ci": "^9.4.14", "symplify/easy-coding-standard": "^9.4.7", diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like.php.inc deleted file mode 100644 index 046300acea95..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like.php.inc +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class ArrayLike extends AbstractGenericArrayRepository -{ -} - -?> ------ - - * @method \Rector\Tests\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\RealObject[] findAll() - */ -final class ArrayLike extends AbstractGenericArrayRepository -{ -} - -?> diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like_nullable.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like_nullable.php.inc deleted file mode 100644 index 15e572cfebaa..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/array_like_nullable.php.inc +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class ArrayLikeNullable extends AbstractGenericMaybeArrayRepository -{ -} - -?> ------ - - * @method \Rector\Tests\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\RealObject[]|null findAllMaybe(string $some, int $type, $unknown) - */ -final class ArrayLikeNullable extends AbstractGenericMaybeArrayRepository -{ -} - -?> diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/child_repository.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/child_repository.php.inc deleted file mode 100644 index 106572229ca0..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/child_repository.php.inc +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class ChildRepository extends AbstractGenericRepository -{ -} - -?> ------ - - * @method \Rector\Tests\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\RealObject find($id) - */ -final class ChildRepository extends AbstractGenericRepository -{ -} - -?> diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/direct_pass_repository.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/direct_pass_repository.php.inc deleted file mode 100644 index 0db71e0e1fed..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/direct_pass_repository.php.inc +++ /dev/null @@ -1,36 +0,0 @@ - - */ -final class DirectPassRepository extends AbstractGenericRepository -{ -} - -?> ------ - - * @method \Rector\Tests\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\RealObject find($id) - */ -final class DirectPassRepository extends AbstractGenericRepository -{ -} - -?> diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/nullable_get.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/nullable_get.php.inc deleted file mode 100644 index be6aaecfbf8e..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/nullable_get.php.inc +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class NullableGet extends AbstractMaybeGenericRepository -{ -} - -?> ------ - - * @method \Rector\Tests\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\RealObject|null find($id) - */ -final class NullableGet extends AbstractMaybeGenericRepository -{ -} - -?> diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/real_get_by_id.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/real_get_by_id.php.inc deleted file mode 100644 index edd5de3da20d..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/real_get_by_id.php.inc +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class RealGetById extends AbstractRealRepository -{ -} - -?> ------ - - * @method \Rector\Tests\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\ChildRealObject|null getById($id) - */ -final class RealGetById extends AbstractRealRepository -{ -} - -?> diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_already_method.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_already_method.php.inc deleted file mode 100644 index b64e17460215..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_already_method.php.inc +++ /dev/null @@ -1,17 +0,0 @@ - - * @method \Rector\Tests\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector\Source\RealObject find($id) - */ -final class SkipAlreadyMethod extends AbstractGenericRepository -{ -} diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_implemented_method.php.inc b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_implemented_method.php.inc deleted file mode 100644 index dac112094c3c..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Fixture/skip_implemented_method.php.inc +++ /dev/null @@ -1,20 +0,0 @@ - - */ -final class SkipImplementedMethod extends AbstractGenericRepository -{ - public function find($inDifferentWay) - { - } -} diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/GenericsPHPStormMethodAnnotationRectorTest.php b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/GenericsPHPStormMethodAnnotationRectorTest.php deleted file mode 100644 index 2438e574768b..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/GenericsPHPStormMethodAnnotationRectorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericArrayRepository.php b/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericArrayRepository.php deleted file mode 100644 index 80fd312d687d..000000000000 --- a/rules-tests/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector/Source/AbstractGenericArrayRepository.php +++ /dev/null @@ -1,18 +0,0 @@ -services(); - $services->set(GenericsPHPStormMethodAnnotationRector::class); -}; diff --git a/rules/DeadCode/NodeAnalyzer/ExprUsedInNodeAnalyzer.php b/rules/DeadCode/NodeAnalyzer/ExprUsedInNodeAnalyzer.php index 54eb65d86706..d2b10f79a865 100644 --- a/rules/DeadCode/NodeAnalyzer/ExprUsedInNodeAnalyzer.php +++ b/rules/DeadCode/NodeAnalyzer/ExprUsedInNodeAnalyzer.php @@ -11,7 +11,6 @@ use PhpParser\Node\Expr\Variable; use Rector\Core\NodeAnalyzer\CompactFuncCallAnalyzer; use Rector\Core\PhpParser\Comparing\NodeComparator; -use Rector\Core\PhpParser\Node\BetterNodeFinder; final class ExprUsedInNodeAnalyzer { diff --git a/rules/Generics/Filter/UnnededMethodTagValueNodeFilter.php b/rules/Generics/Filter/UnnededMethodTagValueNodeFilter.php deleted file mode 100644 index 0f963bf1d7df..000000000000 --- a/rules/Generics/Filter/UnnededMethodTagValueNodeFilter.php +++ /dev/null @@ -1,89 +0,0 @@ -filterOutExistingMethodTagValuesNodes($methodTagValueNodes, $phpDocInfo); - - return $this->filterOutImplementedClassMethods( - $methodTagValueNodes, - $genericChildParentClassReflections->getChildClassReflection(), - $scope - ); - } - - /** - * @param MethodTagValueNode[] $methodTagValueNodes - * @return MethodTagValueNode[] - */ - private function filterOutExistingMethodTagValuesNodes( - array $methodTagValueNodes, - PhpDocInfo $phpDocInfo - ): array { - $methodTagNames = $phpDocInfo->getMethodTagNames(); - if ($methodTagNames === []) { - return $methodTagValueNodes; - } - - $filteredMethodTagValueNodes = []; - foreach ($methodTagValueNodes as $methodTagValueNode) { - if (in_array($methodTagValueNode->methodName, $methodTagNames, true)) { - continue; - } - - $filteredMethodTagValueNodes[] = $methodTagValueNode; - } - - return $filteredMethodTagValueNodes; - } - - /** - * @param MethodTagValueNode[] $methodTagValueNodes - * @return MethodTagValueNode[] - */ - private function filterOutImplementedClassMethods( - array $methodTagValueNodes, - ClassReflection $classReflection, - Scope $scope - ): array { - $filteredMethodTagValueNodes = []; - foreach ($methodTagValueNodes as $methodTagValueNode) { - if ($this->classMethodAnalyzer->hasClassMethodDirectly( - $classReflection, - $methodTagValueNode->methodName, - $scope - )) { - continue; - } - - $filteredMethodTagValueNodes[] = $methodTagValueNode; - } - - return $filteredMethodTagValueNodes; - } -} diff --git a/rules/Generics/NodeType/ExtendsTemplateTypeMapFallbackFactory.php b/rules/Generics/NodeType/ExtendsTemplateTypeMapFallbackFactory.php deleted file mode 100644 index 573884317c9a..000000000000 --- a/rules/Generics/NodeType/ExtendsTemplateTypeMapFallbackFactory.php +++ /dev/null @@ -1,108 +0,0 @@ -resolveExtendsTags($classReflection); - if ($extendsTags === []) { - return null; - } - - $parentGenericTypeNames = $this->resolveParentGenericTypeNames($classReflection); - - foreach ($extendsTags as $extendTag) { - $extendsTagType = $extendTag->getType(); - if (! $extendsTagType instanceof GenericObjectType) { - continue; - } - - $templateTypeMap = []; - foreach ($extendsTagType->getTypes() as $key => $genericExtendType) { - if (! isset($parentGenericTypeNames[$key])) { - continue; - } - - $parentGenericTypeName = $parentGenericTypeNames[$key]; - - if ($genericExtendType instanceof TypeWithClassName) { - // this plac ewill need more work - $templateTypeScope = TemplateTypeScope::createWithClass($classReflection->getName()); - $genericExtendType = $this->createTemplateObjectType( - $templateTypeScope, - $parentGenericTypeName, - $genericExtendType - ); - } - - $templateTypeMap[$parentGenericTypeName] = $genericExtendType; - } - - return new TemplateTypeMap($templateTypeMap); - } - - return null; - } - - /** - * @return ExtendsTag[] - */ - private function resolveExtendsTags(ClassReflection $classReflection): array - { - $parentClassReflection = $classReflection->getParentClass(); - if (! $parentClassReflection instanceof ClassReflection) { - return []; - } - - $resolvedPhpDocBlock = $classReflection->getResolvedPhpDoc(); - if (! $resolvedPhpDocBlock instanceof ResolvedPhpDocBlock) { - return []; - } - - return $resolvedPhpDocBlock->getExtendsTags(); - } - - /** - * @return string[] - */ - private function resolveParentGenericTypeNames(ClassReflection $classReflection): array - { - $parentClassReflection = $classReflection->getParentClass(); - if (! $parentClassReflection instanceof ClassReflection) { - return []; - } - - $templateTypeMap = $parentClassReflection->getTemplateTypeMap(); - return array_keys($templateTypeMap->getTypes()); - } - - private function createTemplateObjectType( - TemplateTypeScope $templateTypeScope, - string $parentGenericTypeName, - TypeWithClassName $typeWithClassName - ): TemplateObjectType { - return new TemplateObjectType( - $templateTypeScope, - new TemplateTypeParameterStrategy(), - TemplateTypeVariance::createInvariant(), - $parentGenericTypeName, - new ObjectType($typeWithClassName->getClassName()) - ); - } -} diff --git a/rules/Generics/NodeType/GenericTypeSpecifier.php b/rules/Generics/NodeType/GenericTypeSpecifier.php deleted file mode 100644 index 3e062ac85276..000000000000 --- a/rules/Generics/NodeType/GenericTypeSpecifier.php +++ /dev/null @@ -1,71 +0,0 @@ -resolveAvailableTemplateTypeMap($classReflection); - foreach ($methodTagValueNodes as $methodTagValueNode) { - if ($methodTagValueNode->returnType === null) { - continue; - } - - $returnType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanTypeWithTemplateTypeMap( - $methodTagValueNode->returnType, - $node, - $templateTypeMap - ); - - $resolvedType = TemplateTypeHelper::resolveTemplateTypes($returnType, $templateTypeMap); - $resolvedTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode( - $resolvedType, - TypeKind::RETURN() - ); - - $methodTagValueNode->returnType = $resolvedTypeNode; - } - } - - private function resolveAvailableTemplateTypeMap(ClassReflection $classReflection): TemplateTypeMap - { - $templateTypeMap = $classReflection->getTemplateTypeMap(); - - // add template map from extends - if ($templateTypeMap->getTypes() !== []) { - return $templateTypeMap; - } - $fallbackTemplateTypeMap = $this->extendsTemplateTypeMapFallbackFactory->createFromClassReflection( - $classReflection - ); - - if ($fallbackTemplateTypeMap instanceof TemplateTypeMap) { - return $fallbackTemplateTypeMap; - } - - return $templateTypeMap; - } -} diff --git a/rules/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector.php b/rules/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector.php deleted file mode 100644 index 334cd191fdd2..000000000000 --- a/rules/Generics/Rector/Class_/GenericsPHPStormMethodAnnotationRector.php +++ /dev/null @@ -1,140 +0,0 @@ - - */ -final class AndroidDeviceRepository extends AbstractRepository -{ -} -CODE_SAMPLE - - , - <<<'CODE_SAMPLE' -/** - * @template TEntity as object - */ -abstract class AbstractRepository -{ - /** - * @return TEntity - */ - public function find($id) - { - } -} - -/** - * @template TEntity as SomeObject - * @extends AbstractRepository - * @method SomeObject find($id) - */ -final class AndroidDeviceRepository extends AbstractRepository -{ -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [Class_::class]; - } - - /** - * @param Class_ $node - */ - public function refactor(Node $node): ?Node - { - $scope = $node->getAttribute(AttributeKey::SCOPE); - if (! $scope instanceof Scope) { - return null; - } - - $childParentClassReflections = $this->genericClassReflectionAnalyzer->resolveChildParent($node); - if (! $childParentClassReflections instanceof ChildParentClassReflections) { - return null; - } - - // resolve generic method from parent - $methodTagValueNodes = $this->classGenericMethodResolver->resolveFromClass($childParentClassReflections); - - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - - $methodTagValueNodes = $this->unnededMethodTagValueNodeFilter->filter( - $methodTagValueNodes, - $phpDocInfo, - $childParentClassReflections, - $scope - ); - - $this->genericTypeSpecifier->replaceGenericTypesWithSpecificTypes( - $methodTagValueNodes, - $node, - $childParentClassReflections->getChildClassReflection() - ); - - foreach ($methodTagValueNodes as $methodTagValueNode) { - $phpDocInfo->addTagValueNode($methodTagValueNode); - } - - return $node; - } -} diff --git a/rules/Generics/Reflection/ClassGenericMethodResolver.php b/rules/Generics/Reflection/ClassGenericMethodResolver.php deleted file mode 100644 index 1dcf9107990a..000000000000 --- a/rules/Generics/Reflection/ClassGenericMethodResolver.php +++ /dev/null @@ -1,83 +0,0 @@ -getParentClassReflection(); - $templateNames = array_keys($classReflection->getTemplateTags()); - - foreach ($classReflection->getNativeMethods() as $methodReflection) { - $parentMethodDocComment = $methodReflection->getDocComment(); - if ($parentMethodDocComment === null) { - continue; - } - - // how to parse? - $parentMethodSimplePhpDocNode = $this->simplePhpDocParser->parseDocBlock($parentMethodDocComment); - $methodTagValueNode = $this->resolveMethodTagValueNode( - $parentMethodSimplePhpDocNode, - $templateNames, - $methodReflection, - $genericChildParentClassReflections - ); - if (! $methodTagValueNode instanceof MethodTagValueNode) { - continue; - } - - $methodTagValueNodes[] = $methodTagValueNode; - } - - return $methodTagValueNodes; - } - - /** - * @param string[] $templateNames - */ - private function resolveMethodTagValueNode( - SimplePhpDocNode $simplePhpDocNode, - array $templateNames, - MethodReflection $methodReflection, - ChildParentClassReflections $genericChildParentClassReflections - ): ?MethodTagValueNode { - foreach ($simplePhpDocNode->getReturnTagValues() as $returnTagValueNode) { - foreach ($templateNames as $templateName) { - $typeAsString = (string) $returnTagValueNode->type; - if (! Strings::match($typeAsString, '#\b' . preg_quote($templateName, '#') . '\b#')) { - continue; - } - - return $this->methodTagValueNodeFactory->createFromMethodReflectionAndReturnTagValueNode( - $methodReflection, - $returnTagValueNode, - $genericChildParentClassReflections - ); - } - } - - return null; - } -} diff --git a/rules/Generics/Reflection/ClassMethodAnalyzer.php b/rules/Generics/Reflection/ClassMethodAnalyzer.php deleted file mode 100644 index 8c678c2e4f4a..000000000000 --- a/rules/Generics/Reflection/ClassMethodAnalyzer.php +++ /dev/null @@ -1,26 +0,0 @@ -hasMethod($methodName)) { - return false; - } - - $classMethodReflection = $classReflection->getMethod($methodName, $scope); - $declaringClassReflection = $classMethodReflection->getDeclaringClass(); - - return $declaringClassReflection->getName() === $classReflection->getName(); - } -} diff --git a/rules/Generics/Reflection/GenericClassReflectionAnalyzer.php b/rules/Generics/Reflection/GenericClassReflectionAnalyzer.php deleted file mode 100644 index 16583f6fa07b..000000000000 --- a/rules/Generics/Reflection/GenericClassReflectionAnalyzer.php +++ /dev/null @@ -1,69 +0,0 @@ -extends === null) { - return null; - } - - $scope = $class->getAttribute(AttributeKey::SCOPE); - if (! $scope instanceof Scope) { - return null; - } - - $classReflection = $scope->getClassReflection(); - if (! $classReflection instanceof ClassReflection) { - return null; - } - - if (! $this->isGeneric($classReflection)) { - return null; - } - - $parentClassReflection = $classReflection->getParentClass(); - if (! $parentClassReflection instanceof ClassReflection) { - return null; - } - - if (! $this->isGeneric($parentClassReflection)) { - return null; - } - - return new ChildParentClassReflections($classReflection, $parentClassReflection); - } - - /** - * Solve isGeneric() ignores extends and similar tags, - * so it has to be extended with "@extends" and "@implements" - */ - private function isGeneric(ClassReflection $classReflection): bool - { - if ($classReflection->isGeneric()) { - return true; - } - - $resolvedPhpDocBlock = $classReflection->getResolvedPhpDoc(); - if (! $resolvedPhpDocBlock instanceof ResolvedPhpDocBlock) { - return false; - } - - if ($resolvedPhpDocBlock->getExtendsTags() !== []) { - return true; - } - - return $resolvedPhpDocBlock->getImplementsTags() !== []; - } -} diff --git a/rules/Generics/TagValueNodeFactory/MethodTagValueNodeFactory.php b/rules/Generics/TagValueNodeFactory/MethodTagValueNodeFactory.php deleted file mode 100644 index 0ef540e829e0..000000000000 --- a/rules/Generics/TagValueNodeFactory/MethodTagValueNodeFactory.php +++ /dev/null @@ -1,133 +0,0 @@ -getTemplateTypeMap(); - $returnTagTypeNode = $this->resolveReturnTagTypeNode($returnTagValueNode, $templateTypeMap); - - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); - $parameterReflections = $parametersAcceptor->getParameters(); - - $stringParameters = $this->resolveStringParameters($parameterReflections); - - return new MethodTagValueNode( - false, - $returnTagTypeNode, - $methodReflection->getName(), - $stringParameters, - '' - ); - } - - /** - * @param ParameterReflection[] $parameterReflections - * @return MethodTagValueParameterNode[] - */ - private function resolveStringParameters(array $parameterReflections): array - { - $stringParameters = []; - - foreach ($parameterReflections as $parameterReflection) { - $stringParameters[] = $this->methodTagValueParameterNodeFactory->createFromParamReflection( - $parameterReflection - ); - } - - return $stringParameters; - } - - private function resolveReturnTagTypeNode( - ReturnTagValueNode $returnTagValueNode, - TemplateTypeMap $templateTypeMap - ): TypeNode { - $returnTagTypeNode = $returnTagValueNode->type; - if ($returnTagValueNode->type instanceof UnionTypeNode) { - return $this->resolveUnionTypeNode($returnTagValueNode->type, $templateTypeMap); - } - - if ($returnTagValueNode->type instanceof IdentifierTypeNode) { - return $this->resolveIdentifierTypeNode( - $returnTagValueNode->type, - $templateTypeMap, - $returnTagTypeNode - ); - } - - return $returnTagTypeNode; - } - - private function resolveUnionTypeNode(UnionTypeNode $unionTypeNode, TemplateTypeMap $templateTypeMap): UnionTypeNode - { - $resolvedTypes = []; - foreach ($unionTypeNode->types as $unionedTypeNode) { - if ($unionedTypeNode instanceof ArrayTypeNode) { - if (! $unionedTypeNode->type instanceof IdentifierTypeNode) { - throw new ShouldNotHappenException(); - } - - $resolvedType = $this->resolveIdentifierTypeNode( - $unionedTypeNode->type, - $templateTypeMap, - $unionedTypeNode - ); - - $resolvedTypes[] = new ArrayTypeNode($resolvedType); - } elseif ($unionedTypeNode instanceof IdentifierTypeNode) { - $resolvedTypes[] = $this->resolveIdentifierTypeNode( - $unionedTypeNode, - $templateTypeMap, - $unionedTypeNode - ); - } - } - - return new UnionTypeNode($resolvedTypes); - } - - private function resolveIdentifierTypeNode( - IdentifierTypeNode $identifierTypeNode, - TemplateTypeMap $templateTypeMap, - TypeNode $fallbackTypeNode - ): TypeNode { - $typeName = $identifierTypeNode->name; - $genericType = $templateTypeMap->getType($typeName); - - if ($genericType instanceof Type) { - return $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($genericType, TypeKind::RETURN()); - } - - return $fallbackTypeNode; - } -} diff --git a/rules/Generics/TagValueNodeFactory/MethodTagValueParameterNodeFactory.php b/rules/Generics/TagValueNodeFactory/MethodTagValueParameterNodeFactory.php deleted file mode 100644 index 1df02bdd8dd0..000000000000 --- a/rules/Generics/TagValueNodeFactory/MethodTagValueParameterNodeFactory.php +++ /dev/null @@ -1,42 +0,0 @@ -getType(); - if ($parameterType instanceof MixedType && ! $parameterType->isExplicitMixed()) { - $parameterTypeNode = null; - } else { - $parameterTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode( - $parameterType, - TypeKind::PARAM() - ); - } - - return new MethodTagValueParameterNode( - $parameterTypeNode, - $parameterReflection->passedByReference() - ->yes(), - $parameterReflection->isVariadic(), - '$' . $parameterReflection->getName(), - // @todo resolve - null - ); - } -} diff --git a/rules/Generics/ValueObject/ChildParentClassReflections.php b/rules/Generics/ValueObject/ChildParentClassReflections.php deleted file mode 100644 index 7e5ec56107d7..000000000000 --- a/rules/Generics/ValueObject/ChildParentClassReflections.php +++ /dev/null @@ -1,38 +0,0 @@ -childClassReflection; - } - - public function getParentClassReflection(): ClassReflection - { - return $this->parentClassReflection; - } - - /** - * Child class has priority with template map - */ - public function getTemplateTypeMap(): TemplateTypeMap - { - $parentClassTemplateTypeMap = $this->parentClassReflection->getTemplateTypeMap(); - $childClassTemplateTypeMap = $this->childClassReflection->getTemplateTypeMap(); - - return $childClassTemplateTypeMap->intersect($parentClassTemplateTypeMap); - } -}