Skip to content

Commit

Permalink
[CodeQuality] Skip child has typed on ReturnTypeFromStrictScalarRetur…
Browse files Browse the repository at this point in the history
…nExprRector (#2806)

Co-authored-by: Dominik Peters <kuhlesdominik@gmx.de>
  • Loading branch information
samsonasik and Dominik Peters committed Aug 19, 2022
1 parent 1cc3914 commit 6b5a537
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

class SkipIfChildClassHasTyped
{
public function test()
{
return 0;
}
}

class SomeChild extends SkipIfChildClassHasTyped
{
public function test(): string
{
$a = '1';

return $a;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\Core\ValueObject\PhpVersion;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictScalarReturnTypeAnalyzer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -23,7 +24,8 @@
final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly StrictScalarReturnTypeAnalyzer $strictScalarReturnTypeAnalyzer
private readonly StrictScalarReturnTypeAnalyzer $strictScalarReturnTypeAnalyzer,
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard
) {
}

Expand Down Expand Up @@ -90,6 +92,10 @@ public function refactor(Node $node): ?Node
return null;
}

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

$node->returnType = $returnTypeNode;
return $node;
}
Expand Down

0 comments on commit 6b5a537

Please sign in to comment.