From 4ff603d58abd156432d0db47bd1281e0dd5e9dc2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 6 Oct 2023 11:11:16 +0200 Subject: [PATCH 1/3] Create skip_construct.php.inc --- .../Fixture/skip_construct.php.inc | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector/Fixture/skip_construct.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector/Fixture/skip_construct.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector/Fixture/skip_construct.php.inc new file mode 100644 index 00000000000..2bcfbb2d36e --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector/Fixture/skip_construct.php.inc @@ -0,0 +1,11 @@ + Date: Fri, 6 Oct 2023 11:17:47 +0200 Subject: [PATCH 2/3] fix --- ...eturnTypeFromStrictScalarReturnsRector.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php index 7f2cf0d63ed..6da7958f9f0 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php @@ -28,6 +28,7 @@ use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\Value\ValueResolver; use Rector\Core\Rector\AbstractRector; +use Rector\Core\ValueObject\MethodName; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -96,7 +97,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->returnType instanceof Node) { + if ($this->shouldSkip($node)) { return null; } @@ -110,6 +111,24 @@ public function refactor(Node $node): ?Node return $node; } + /** + * @param ClassMethod|Function_|Closure $node + */ + private function shouldSkip(Node $node): bool { + if ($node->returnType instanceof Node) { + return true; + } + + if ($node instanceof ClassMethod) { + if ($this->isName($node, MethodName::CONSTRUCT)) { + return true; + } + } + + return false; + + } + public function provideMinPhpVersion(): int { return PhpVersionFeature::SCALAR_TYPES; From d544ac6ca6ac06d5450776cb887e3359fc0471c7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 6 Oct 2023 11:26:45 +0200 Subject: [PATCH 3/3] use ClassMethodReturnTypeOverrideGuard --- ...eturnTypeFromStrictScalarReturnsRector.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php index 6da7958f9f0..2fcefe1634e 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php @@ -27,10 +27,12 @@ use PHPStan\Type\BooleanType; use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\Value\ValueResolver; -use Rector\Core\Rector\AbstractRector; +use PHPStan\Analyser\Scope; +use Rector\Core\Rector\AbstractScopeAwareRector; use Rector\Core\ValueObject\MethodName; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; +use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -38,13 +40,14 @@ /** * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector\BoolReturnTypeFromStrictScalarReturnsRectorTest */ -final class BoolReturnTypeFromStrictScalarReturnsRector extends AbstractRector implements MinPhpVersionInterface +final class BoolReturnTypeFromStrictScalarReturnsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface { public function __construct( private readonly ReturnAnalyzer $returnAnalyzer, private readonly ReflectionProvider $reflectionProvider, private readonly ValueResolver $valueResolver, private readonly BetterNodeFinder $betterNodeFinder, + private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ) { } @@ -95,9 +98,9 @@ public function getNodeTypes(): array /** * @param ClassMethod|Function_|Closure $node */ - public function refactor(Node $node): ?Node + public function refactorWithScope(Node $node, Scope $scope): ?Node { - if ($this->shouldSkip($node)) { + if ($this->shouldSkip($node, $scope)) { return null; } @@ -114,15 +117,14 @@ public function refactor(Node $node): ?Node /** * @param ClassMethod|Function_|Closure $node */ - private function shouldSkip(Node $node): bool { + private function shouldSkip(Node $node, Scope $scope): bool { if ($node->returnType instanceof Node) { return true; } - if ($node instanceof ClassMethod) { - if ($this->isName($node, MethodName::CONSTRUCT)) { - return true; - } + if ($node instanceof ClassMethod + && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { + return true; } return false;