Skip to content

Commit

Permalink
Drop AttributeKey::SCOPE in ClassMethodReturnTypeOverrideGuard (#4144)
Browse files Browse the repository at this point in the history
* Drop AttributeKey::SCOPE in ClassMethodReturnTypeOverrideGuard

* cs
  • Loading branch information
staabm committed Jun 9, 2023
1 parent 7740446 commit ebfe96b
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 66 deletions.
Expand Up @@ -20,7 +20,6 @@
use Rector\Core\Reflection\ReflectionResolver;
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
Expand All @@ -47,7 +46,7 @@ public function __construct(
) {
}

public function shouldSkipClassMethod(ClassMethod $classMethod): bool
public function shouldSkipClassMethod(ClassMethod $classMethod, Scope $scope): bool
{
// 1. skip magic methods
if ($classMethod->isMagic()) {
Expand All @@ -72,7 +71,7 @@ public function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

if (! $this->isReturnTypeChangeAllowed($classMethod)) {
if (! $this->isReturnTypeChangeAllowed($classMethod, $scope)) {
return true;
}

Expand All @@ -92,7 +91,7 @@ public function shouldSkipClassMethod(ClassMethod $classMethod): bool
return $this->hasClassMethodExprReturn($classMethod);
}

private function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
private function isReturnTypeChangeAllowed(ClassMethod $classMethod, Scope $scope): bool
{
// make sure return type is not protected by parent contract
$parentClassMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($classMethod);
Expand All @@ -102,11 +101,6 @@ private function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
return true;
}

$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$parentClassMethodReflection,
$classMethod,
Expand Down
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
Expand Down Expand Up @@ -94,7 +93,10 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

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

Expand Down
8 changes: 6 additions & 2 deletions rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php
Expand Up @@ -143,8 +143,12 @@ private function isAssign(Node $parentNode): bool
/**
* @param string[] $checkedVariables
*/
private function shouldSkipVariable(Variable $variable, string $variableName, array &$checkedVariables, Node $parentNode): bool
{
private function shouldSkipVariable(
Variable $variable,
string $variableName,
array &$checkedVariables,
Node $parentNode
): bool {
if ($this->isAsCoalesceLeftOrAssignOpCoalesceVar($parentNode, $variable)) {
return true;
}
Expand Down
Expand Up @@ -9,9 +9,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down
Expand Up @@ -7,10 +7,11 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\TypeDeclaration\Guard\PhpDocNestedAnnotationGuard;
use Rector\TypeDeclaration\Helper\PhpDocNullableTypeHelper;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
Expand All @@ -20,7 +21,7 @@
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\ReturnAnnotationIncorrectNullableRectorTest
*/
final class ReturnAnnotationIncorrectNullableRector extends AbstractRector
final class ReturnAnnotationIncorrectNullableRector extends AbstractScopeAwareRector
{
public function __construct(
private readonly PhpDocTypeChanger $phpDocTypeChanger,
Expand Down Expand Up @@ -78,11 +79,14 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|Function_ $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$returnType = $node->getReturnType();

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

Expand Down
Expand Up @@ -14,8 +14,9 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\NeverType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeNestingScope\ValueObject\ControlStructure;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
Expand All @@ -28,7 +29,7 @@
*
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\ReturnNeverTypeRectorTest
*/
final class ReturnNeverTypeRector extends AbstractRector implements MinPhpVersionInterface
final class ReturnNeverTypeRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
Expand Down Expand Up @@ -74,9 +75,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;
}

Expand All @@ -90,7 +91,7 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::NEVER_TYPE;
}

private function shouldSkip(ClassMethod | Function_ | Closure $node): bool
private function shouldSkip(ClassMethod | Function_ | Closure $node, Scope $scope): bool
{
$hasReturn = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, Return_::class);
if ($node instanceof ClassMethod && $node->isMagic()) {
Expand Down Expand Up @@ -118,7 +119,8 @@ private function shouldSkip(ClassMethod | Function_ | Closure $node): bool
}

if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod(
$node
$node,
$scope
)) {
return true;
}
Expand Down
Expand Up @@ -12,7 +12,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
Expand All @@ -23,7 +24,7 @@
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector\ReturnTypeFromReturnDirectArrayRectorTest
*/
final class ReturnTypeFromReturnDirectArrayRector extends AbstractRector implements MinPhpVersionInterface
final class ReturnTypeFromReturnDirectArrayRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
Expand Down Expand Up @@ -70,13 +71,16 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|Function_|ArrowFunction $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($node->returnType !== null) {
return null;
}

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

Expand Down
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
Expand All @@ -21,7 +22,7 @@
use PHPStan\Type\UnionType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
Expand All @@ -37,7 +38,7 @@
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\ReturnTypeFromReturnNewRectorTest
*/
final class ReturnTypeFromReturnNewRector extends AbstractRector implements MinPhpVersionInterface
final class ReturnTypeFromReturnNewRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly TypeFactory $typeFactory,
Expand Down Expand Up @@ -87,13 +88,16 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|Function_|ArrowFunction $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($node->returnType !== null) {
return null;
}

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

Expand Down
Expand Up @@ -9,7 +9,8 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictBoolReturnTypeAnalyzer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
Expand All @@ -20,7 +21,7 @@
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector\ReturnTypeFromStrictBoolReturnExprRectorTest
*/
final class ReturnTypeFromStrictBoolReturnExprRector extends AbstractRector implements MinPhpVersionInterface
final class ReturnTypeFromStrictBoolReturnExprRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly StrictBoolReturnTypeAnalyzer $strictBoolReturnTypeAnalyzer,
Expand Down Expand Up @@ -67,13 +68,16 @@ 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 !== null) {
return null;
}

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

Expand Down
Expand Up @@ -6,8 +6,9 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\TypeAnalyzer\StrictReturnClassConstReturnTypeAnalyzer;
Expand All @@ -19,7 +20,7 @@
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictConstantReturnRector\ReturnTypeFromStrictConstantReturnRectorTest
*/
final class ReturnTypeFromStrictConstantReturnRector extends AbstractRector implements MinPhpVersionInterface
final class ReturnTypeFromStrictConstantReturnRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly StrictReturnClassConstReturnTypeAnalyzer $strictReturnClassConstReturnTypeAnalyzer,
Expand Down Expand Up @@ -69,13 +70,13 @@ public function getNodeTypes(): array
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($node->returnType instanceof Node) {
return null;
}

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

Expand Down
Expand Up @@ -8,8 +8,9 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\MixedType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
Expand All @@ -22,7 +23,7 @@
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector\ReturnTypeFromStrictNativeCallRectorTest
*/
final class ReturnTypeFromStrictNativeCallRector extends AbstractRector implements MinPhpVersionInterface
final class ReturnTypeFromStrictNativeCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly StrictNativeFunctionReturnTypeAnalyzer $strictNativeFunctionReturnTypeAnalyzer,
Expand Down Expand Up @@ -70,13 +71,16 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|Closure|Function_ $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($node->returnType !== null) {
return null;
}

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

Expand Down

0 comments on commit ebfe96b

Please sign in to comment.