Skip to content

Commit

Permalink
[TypeDeclaration] Utilize ClassMethodReturnTypeOverrideGuard on Numer…
Browse files Browse the repository at this point in the history
…icReturnTypeFromStrictScalarReturnsRector (#5155)

* [TypeDeclaration] Utilize ClassMethodReturnTypeOverrideGuard on NumericReturnTypeFromStrictScalarReturnsRector

* Fix phpstan
  • Loading branch information
samsonasik committed Oct 11, 2023
1 parent 0eb6000 commit 668da1d
Showing 1 changed file with 13 additions and 3 deletions.
Expand Up @@ -24,19 +24,25 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector\NumericReturnTypeFromStrictScalarReturnsRectorTest
*/
final class NumericReturnTypeFromStrictScalarReturnsRector extends AbstractRector implements MinPhpVersionInterface
final class NumericReturnTypeFromStrictScalarReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change numeric return type based on strict returns type operations', [
Expand Down Expand Up @@ -76,7 +82,7 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($node->returnType instanceof Node) {
return null;
Expand All @@ -86,6 +92,10 @@ public function refactor(Node $node): ?Node
return null;
}

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

$return = $this->matchRootReturnWithExpr($node);
if (! $return instanceof Return_) {
return null;
Expand Down

0 comments on commit 668da1d

Please sign in to comment.