Skip to content

Commit

Permalink
Drop AttributeKey::SCOPE in Rector classes (#3791)
Browse files Browse the repository at this point in the history
* Drop AttributeKey::SCOPE in ParamTypeFromStrictTypedPropertyRector

* typo?

* Update ParamTypeFromStrictTypedPropertyRector.php

* cs

* Update rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

* Update rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php

---------

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
staabm and samsonasik committed May 13, 2023
1 parent 56e1346 commit 6dea82d
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -31,7 +32,7 @@
/**
* @see \Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\ParamTypeFromStrictTypedPropertyRectorTest
*/
final class ParamTypeFromStrictTypedPropertyRector extends AbstractRector implements MinPhpVersionInterface
final class ParamTypeFromStrictTypedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ReflectionResolver $reflectionResolver,
Expand Down Expand Up @@ -82,22 +83,22 @@ public function getNodeTypes(): array
/**
* @param Param $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof ClassMethod) {
return null;
}

return $this->decorateParamWithType($parentNode, $node);
return $this->decorateParamWithType($parentNode, $node, $scope);
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}

private function decorateParamWithType(ClassMethod $classMethod, Param $param): ?Param
private function decorateParamWithType(ClassMethod $classMethod, Param $param, Scope $scope): ?Param
{
if ($param->type !== null) {
return null;
Expand All @@ -111,7 +112,7 @@ private function decorateParamWithType(ClassMethod $classMethod, Param $param):
return null;
}

$originalParamType = $this->resolveParamOriginalType($param);
$originalParamType = $this->resolveParamOriginalType($param, $scope);

$paramName = $this->getName($param);

Expand Down Expand Up @@ -175,21 +176,16 @@ private function hasTypeChangedBeforeAssign(Assign $assign, string $paramName, T
return false;
}

if (! $scope->hasVariableType($paramName)->yes()) {
if (!$scope->hasVariableType($paramName)->yes()) {
return false;
}

$currentParamType = $scope->getVariableType($paramName);
return ! $currentParamType->equals($originalType);
}

private function resolveParamOriginalType(Param $param): Type
private function resolveParamOriginalType(Param $param, Scope $scope): Type
{
$scope = $param->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return new MixedType();
}

$paramName = $this->getName($param);
if (! $scope->hasVariableType($paramName)->yes()) {
return new MixedType();
Expand Down

0 comments on commit 6dea82d

Please sign in to comment.