diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/AddReturnDocblockFromMethodCallDocblockRectorTest.php b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/AddReturnDocblockFromMethodCallDocblockRectorTest.php deleted file mode 100644 index c57fcf989d0..00000000000 --- a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/AddReturnDocblockFromMethodCallDocblockRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/handle_static_call.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/handle_static_call.php.inc deleted file mode 100644 index 9e079d809b4..00000000000 --- a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/handle_static_call.php.inc +++ /dev/null @@ -1,38 +0,0 @@ - ------ - diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_fetch_first_column.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_fetch_first_column.php.inc deleted file mode 100644 index c5c35022334..00000000000 --- a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_fetch_first_column.php.inc +++ /dev/null @@ -1,22 +0,0 @@ -connection = $connection; - } - - public function getAll(): array - { - return $this->connection->fetchFirstColumn(); - } -} diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_missing_array_declaration.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_missing_array_declaration.php.inc deleted file mode 100644 index 87710eeb5be..00000000000 --- a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_missing_array_declaration.php.inc +++ /dev/null @@ -1,22 +0,0 @@ -someRepository = $someRepository; - } - - public function getAll() - { - return $this->someRepository->findAll(); - } -} diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_missing_array_on_call.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_missing_array_on_call.php.inc deleted file mode 100644 index f3523603717..00000000000 --- a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/skip_missing_array_on_call.php.inc +++ /dev/null @@ -1,22 +0,0 @@ -someRepository = $someRepository; - } - - public function getAll(): array - { - return $this->someRepository->findAllWithoutArray(); - } -} diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/some_class.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/some_class.php.inc deleted file mode 100644 index d2322cc4fa3..00000000000 --- a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,52 +0,0 @@ -someRepository = $someRepository; - } - - public function getAll(): array - { - return $this->someRepository->findAll(); - } -} - -?> ------ -someRepository = $someRepository; - } - - /** - * @return \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockFromMethodCallDocblockRector\Source\SomeEntity[] - */ - public function getAll(): array - { - return $this->someRepository->findAll(); - } -} - -?> diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Source/SomeEntity.php b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Source/SomeEntity.php deleted file mode 100644 index 175c7266b1f..00000000000 --- a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Source/SomeEntity.php +++ /dev/null @@ -1,8 +0,0 @@ -withRules([AddReturnDocblockFromMethodCallDocblockRector::class]); diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector.php index 99764e3de3b..db2163ce180 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector.php @@ -5,38 +5,18 @@ namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod; use PhpParser\Node; -use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Return_; -use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; -use PHPStan\Type\MixedType; -use PHPStan\Type\ObjectType; -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; -use Rector\Doctrine\Enum\DoctrineClass; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; -use Rector\Reflection\ReflectionResolver; -use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder; -use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockFromMethodCallDocblockRector\AddReturnDocblockFromMethodCallDocblockRectorTest + * @deprecated This rule is deprecated, as it copies docblock from another method call. The docblock can be incorrect or outdated, and spreads the error further. */ -final class AddReturnDocblockFromMethodCallDocblockRector extends AbstractRector +final class AddReturnDocblockFromMethodCallDocblockRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly PhpDocInfoFactory $phpDocInfoFactory, - private readonly ReturnNodeFinder $returnNodeFinder, - private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer, - private readonly ReflectionResolver $reflectionResolver, - private readonly PhpDocTypeChanger $phpDocTypeChanger, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -106,51 +86,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - // definitely not an array return - if (! $node->returnType instanceof Node || ! $this->isName($node->returnType, 'array')) { - return null; - } - - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($phpDocInfo->getReturnTagValue())) { - return null; - } - - $onlyReturnWithExpr = $this->returnNodeFinder->findOnlyReturnWithExpr($node); - if (! $onlyReturnWithExpr instanceof Return_ || (! $onlyReturnWithExpr->expr instanceof MethodCall && ! $onlyReturnWithExpr->expr instanceof StaticCall)) { - return null; - } - - $returnedMethodCall = $onlyReturnWithExpr->expr; - - // skip doctrine connection calls, as to generic and not helpful - $callerType = $this->getType( - $returnedMethodCall instanceof MethodCall ? $returnedMethodCall->var : $returnedMethodCall->class - ); - if ($callerType instanceof ObjectType && $callerType->isInstanceOf(DoctrineClass::CONNECTION)->yes()) { - return null; - } - - $calledMethodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($returnedMethodCall); - if (! $calledMethodReflection instanceof MethodReflection) { - return null; - } - - $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($calledMethodReflection->getVariants()); - - // native return type must be a plain array - if (! $extendedParametersAcceptor->getNativeReturnType()->isArray()->yes()) { - return null; - } - - // docblock must carry a more specific array value type, e.g. SomeEntity[] - $calledReturnType = $extendedParametersAcceptor->getReturnType(); - if ($calledReturnType->getIterableValueType() instanceof MixedType) { - return null; - } - - $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $calledReturnType); - - return $node; + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated, as it copies docblock from another method call that can be incorrect or outdated', + self::class + )); } } diff --git a/src/Config/Level/TypeDeclarationDocblocksLevel.php b/src/Config/Level/TypeDeclarationDocblocksLevel.php index f03ccfd4d94..08d540cb241 100644 --- a/src/Config/Level/TypeDeclarationDocblocksLevel.php +++ b/src/Config/Level/TypeDeclarationDocblocksLevel.php @@ -61,8 +61,5 @@ final class TypeDeclarationDocblocksLevel // run latter after other rules, as more generic AddReturnDocblockForDimFetchArrayFromAssignsRector::class, - - // @todo test first, 2026-01 - // AddReturnDocblockFromMethodCallDocblockRector::class, ]; }