Skip to content

Commit

Permalink
[TypeDeclaration] Using ClassMethodReturnTypeOverrideGuard on ReturnT…
Browse files Browse the repository at this point in the history
…ypeFromStrictParamRector (#4878)

* [TypeDeclaration] Using ClassMethodReturnTypeOverrideGuard on ReturnTypeFromStrictParamRector

* tweak of parent not found

* Fix

* add parent already has return on ReturnTypeFromStrictParamRector

* fixture fix
  • Loading branch information
samsonasik committed Aug 29, 2023
1 parent ea2252a commit 6424bae
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function isReturnTypeChangeAllowed(ClassMethod $classMethod, Scope $scop

// nothing to check
if (! $parentClassMethodReflection instanceof MethodReflection) {
return true;
return ! $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod);
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Source\ParentHasReturn;

class FromParentHasReturn extends ParentHasReturn {
public function doFoo(int $param): int {
return $param;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Source\ParentOverrideBase;

class ParentOverridden extends ParentOverrideBase {
public function doFoo(ParentOverrideBase $param) {
return $param;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Source\ParentOverrideBase;

class ParentOverridden extends ParentOverrideBase {
public function doFoo(ParentOverrideBase $param): ParentOverrideBase {
return $param;
}
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Source;

class ParentHasReturn {
public function doFoo(int $param): int {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Source;

class ParentOverrideBase {
public function doFoo(ParentOverrideBase $param) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -36,7 +36,7 @@
final class ReturnTypeFromStrictParamRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
private readonly ReturnTypeInferer $returnTypeInferer
) {
}
Expand Down Expand Up @@ -91,7 +91,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

if ($this->shouldSkipNode($node)) {
if ($this->shouldSkipNode($node, $scope)) {
return null;
}

Expand Down Expand Up @@ -195,20 +195,14 @@ private function shouldSkipParam(Param $param, array $stmts): bool
return $isParamModified;
}

private function shouldSkipNode(ClassMethod|Function_|Closure $node): bool
private function shouldSkipNode(ClassMethod|Function_|Closure $node, Scope $scope): bool
{
if ($node->returnType !== null) {
return true;
}

if ($node instanceof ClassMethod) {
if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($node)) {
return true;
}

if ($node->isMagic()) {
return true;
}
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
return true;
}

$returnType = $this->returnTypeInferer->inferFunctionLike($node);
Expand Down

0 comments on commit 6424bae

Please sign in to comment.