Skip to content

Commit

Permalink
Skip __construct() in BoolReturnTypeFromStrictScalarReturnsRector (#…
Browse files Browse the repository at this point in the history
…5133)

* Create skip_construct.php.inc

* fix

* use ClassMethodReturnTypeOverrideGuard
  • Loading branch information
staabm committed Oct 6, 2023
1 parent 8179073 commit caf08c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
@@ -0,0 +1,11 @@
<?php

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

final class SkipConstructor
{
public function __construct()
{
return true;
}
}
Expand Up @@ -27,23 +27,27 @@
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;

/**
* @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,
) {
}

Expand Down Expand Up @@ -94,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 ($node->returnType instanceof Node) {
if ($this->shouldSkip($node, $scope)) {
return null;
}

Expand All @@ -110,6 +114,23 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param ClassMethod|Function_|Closure $node
*/
private function shouldSkip(Node $node, Scope $scope): bool {
if ($node->returnType instanceof Node) {
return true;
}

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

return false;

}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::SCALAR_TYPES;
Expand Down

0 comments on commit caf08c4

Please sign in to comment.