mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-19 17:42:23 +00:00
include standalone traits in downgrade too
This commit is contained in:
parent
8f4ff2a20a
commit
150da023db
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp72\PHPStan;
|
||||
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PHPStan\Collector\TraitNodeScopeCollector;
|
||||
|
||||
final class ClassLikeScopeResolver
|
||||
{
|
||||
public function __construct(
|
||||
private TraitNodeScopeCollector $traitNodeScopeCollector,
|
||||
private NodeNameResolver $nodeNameResolver,
|
||||
) {
|
||||
}
|
||||
|
||||
public function resolveScope(ClassMethod $classMethod): ?Scope
|
||||
{
|
||||
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
|
||||
if ($scope instanceof Scope) {
|
||||
return $scope;
|
||||
}
|
||||
|
||||
// fallback to a trait method
|
||||
$classLike = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if ($classLike instanceof Trait_) {
|
||||
$traitName = $this->nodeNameResolver->getName($classLike);
|
||||
return $this->traitNodeScopeCollector->getScopeForTrait($traitName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -7,12 +7,12 @@ namespace Rector\DowngradePhp72\Rector\ClassMethod;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DowngradePhp72\PhpDoc\NativeParamToPhpDocDecorator;
|
||||
use Rector\DowngradePhp72\PHPStan\ClassLikeScopeResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
@ -37,7 +37,7 @@ final class DowngradeParameterTypeWideningRector extends AbstractRector implemen
|
|||
|
||||
public function __construct(
|
||||
private NativeParamToPhpDocDecorator $nativeParamToPhpDocDecorator,
|
||||
private ClassLikeScopeResolver $classLikeScopeResolver
|
||||
private ReflectionProvider $reflectionProvider
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -96,16 +96,21 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$scope = $this->classLikeScopeResolver->resolveScope($node);
|
||||
if (! $scope instanceof Scope) {
|
||||
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if ($classLike === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (! $classReflection instanceof ClassReflection) {
|
||||
$className = $this->nodeNameResolver->getName($classLike);
|
||||
if ($className === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->reflectionProvider->hasClass($className)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$classReflection = $this->reflectionProvider->getClass($className);
|
||||
if ($this->isSealedClass($classReflection)) {
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user