mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 22:08:00 +01:00
Updated Rector to commit 5f43d6b712538efcbcb976418c2e856de0468e3a
5f43d6b712
Make RemoveUnusedPrivateClassConstantRector use of static reflection (#384)
This commit is contained in:
parent
7d9a7d6d64
commit
31b96d8276
@ -9264,15 +9264,15 @@ Remove specific traits from code
|
||||
- class: [`Rector\Removing\Rector\Class_\RemoveTraitRector`](../rules/Removing/Rector/Class_/RemoveTraitRector.php)
|
||||
|
||||
```php
|
||||
use Rector\Removing\Rector\Class_\RemoveTraitRector;
|
||||
use Rector\Removing\Rector\Class_\RemoveTraitUseRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(RemoveTraitRector::class)
|
||||
$services->set(RemoveTraitUseRector::class)
|
||||
->call('configure', [[
|
||||
RemoveTraitRector::TRAITS_TO_REMOVE => ['TraitNameToRemove'],
|
||||
RemoveTraitUseRector::TRAITS_TO_REMOVE => ['TraitNameToRemove'],
|
||||
]]);
|
||||
};
|
||||
```
|
||||
|
@ -4,12 +4,10 @@ declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\ClassConst;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\NodeAnalyzer\EnumAnalyzer;
|
||||
use Rector\Core\NodeManipulator\ClassConstManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
@ -24,9 +22,14 @@ final class RemoveUnusedPrivateClassConstantRector extends \Rector\Core\Rector\A
|
||||
* @var \Rector\Core\NodeManipulator\ClassConstManipulator
|
||||
*/
|
||||
private $classConstManipulator;
|
||||
public function __construct(\Rector\Core\NodeManipulator\ClassConstManipulator $classConstManipulator)
|
||||
/**
|
||||
* @var \Rector\Core\NodeAnalyzer\EnumAnalyzer
|
||||
*/
|
||||
private $enumAnalyzer;
|
||||
public function __construct(\Rector\Core\NodeManipulator\ClassConstManipulator $classConstManipulator, \Rector\Core\NodeAnalyzer\EnumAnalyzer $enumAnalyzer)
|
||||
{
|
||||
$this->classConstManipulator = $classConstManipulator;
|
||||
$this->enumAnalyzer = $enumAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
@ -62,7 +65,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor($node) : ?\PhpParser\Node
|
||||
{
|
||||
if ($this->shouldSkip($node)) {
|
||||
if ($this->shouldSkipClassConst($node)) {
|
||||
return null;
|
||||
}
|
||||
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
||||
@ -73,30 +76,13 @@ CODE_SAMPLE
|
||||
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
$classLike = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
||||
if (!$classLike instanceof \PhpParser\Node\Stmt\ClassLike) {
|
||||
if ($this->classConstManipulator->hasClassConstFetch($node, $classReflection)) {
|
||||
return null;
|
||||
}
|
||||
$classObjectType = new \PHPStan\Type\ObjectType($classReflection->getName());
|
||||
/** @var ClassConstFetch[] $classConstFetches */
|
||||
$classConstFetches = $this->betterNodeFinder->findInstanceOf($classLike->stmts, \PhpParser\Node\Expr\ClassConstFetch::class);
|
||||
foreach ($classConstFetches as $classConstFetch) {
|
||||
if (!$this->nodeNameResolver->areNamesEqual($classConstFetch->name, $node->consts[0]->name)) {
|
||||
continue;
|
||||
}
|
||||
$constFetchClassType = $this->nodeTypeResolver->resolve($classConstFetch->class);
|
||||
// constant is used!
|
||||
if ($constFetchClassType->isSuperTypeOf($classObjectType)->yes()) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
$this->removeNode($node);
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassConst $classConst
|
||||
*/
|
||||
private function shouldSkip($classConst) : bool
|
||||
private function shouldSkipClassConst(\PhpParser\Node\Stmt\ClassConst $classConst) : bool
|
||||
{
|
||||
if (!$classConst->isPrivate()) {
|
||||
return \true;
|
||||
@ -104,21 +90,6 @@ CODE_SAMPLE
|
||||
if (\count($classConst->consts) !== 1) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->classConstManipulator->isEnum($classConst)) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->classConstManipulator->hasClassConstFetch($classConst)) {
|
||||
return \true;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classConst);
|
||||
if ($phpDocInfo->hasByName('api')) {
|
||||
return \true;
|
||||
}
|
||||
$classLike = $classConst->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
||||
if ($classLike instanceof \PhpParser\Node\Stmt\ClassLike) {
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classLike);
|
||||
return $phpDocInfo->hasByName('api');
|
||||
}
|
||||
return \false;
|
||||
return $this->enumAnalyzer->isEnumClassConst($classConst);
|
||||
}
|
||||
}
|
||||
|
@ -12,9 +12,10 @@ use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\ThisType;
|
||||
use PHPStan\Type\TypeWithClassName;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
@ -75,14 +76,14 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
$type = $scope->getType($node->var);
|
||||
if ($type instanceof \PHPStan\Type\ThisType) {
|
||||
$type = $type->getStaticObjectType();
|
||||
}
|
||||
if (!$type instanceof \PHPStan\Type\ObjectType) {
|
||||
if (!$type instanceof \PHPStan\Type\TypeWithClassName) {
|
||||
return null;
|
||||
}
|
||||
$class = $this->reflectionAstResolver->resolveClassFromObjectType($type);
|
||||
if ($this->shouldSkipClassMethod($class, $node)) {
|
||||
$classLike = $this->reflectionAstResolver->resolveClassFromObjectType($type);
|
||||
if ($classLike === null) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipClassMethod($classLike, $node)) {
|
||||
return null;
|
||||
}
|
||||
// if->cond cannot removed, it has to be replaced with false, see https://3v4l.org/U9S9i
|
||||
@ -100,18 +101,18 @@ CODE_SAMPLE
|
||||
return $node;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\Class_|null $class
|
||||
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\Interface_ $classLike
|
||||
*/
|
||||
private function shouldSkipClassMethod($class, \PhpParser\Node\Expr\MethodCall $methodCall) : bool
|
||||
private function shouldSkipClassMethod($classLike, \PhpParser\Node\Expr\MethodCall $methodCall) : bool
|
||||
{
|
||||
if (!$class instanceof \PhpParser\Node\Stmt\Class_) {
|
||||
if (!$classLike instanceof \PhpParser\Node\Stmt\Class_) {
|
||||
return \true;
|
||||
}
|
||||
$methodName = $this->getName($methodCall->name);
|
||||
if ($methodName === null) {
|
||||
return \true;
|
||||
}
|
||||
$classMethod = $class->getMethod($methodName);
|
||||
$classMethod = $classLike->getMethod($methodName);
|
||||
if (!$classMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
|
||||
return \true;
|
||||
}
|
||||
|
@ -4,19 +4,17 @@ declare (strict_types=1);
|
||||
namespace Rector\Removing\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\NodeManipulator\ClassManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Removing\Rector\Class_\RemoveTraitRector\RemoveTraitRectorTest
|
||||
* @see \Rector\Tests\Removing\Rector\Class_\RemoveTraitUseRector\RemoveTraitUseRectorTest
|
||||
*/
|
||||
final class RemoveTraitRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
||||
final class RemoveTraitUseRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
@ -30,14 +28,6 @@ final class RemoveTraitRector extends \Rector\Core\Rector\AbstractRector impleme
|
||||
* @var string[]
|
||||
*/
|
||||
private $traitsToRemove = [];
|
||||
/**
|
||||
* @var \Rector\Core\NodeManipulator\ClassManipulator
|
||||
*/
|
||||
private $classManipulator;
|
||||
public function __construct(\Rector\Core\NodeManipulator\ClassManipulator $classManipulator)
|
||||
{
|
||||
$this->classManipulator = $classManipulator;
|
||||
}
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove specific traits from code', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
||||
@ -65,12 +55,16 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor($node) : ?\PhpParser\Node
|
||||
{
|
||||
$usedTraits = $this->classManipulator->getUsedTraits($node);
|
||||
if ($usedTraits === []) {
|
||||
return null;
|
||||
}
|
||||
$this->classHasChanged = \false;
|
||||
$this->removeTraits($usedTraits);
|
||||
foreach ($node->getTraitUses() as $traitUse) {
|
||||
foreach ($traitUse->traits as $trait) {
|
||||
if (!$this->isNames($trait, $this->traitsToRemove)) {
|
||||
continue;
|
||||
}
|
||||
$this->removeNode($traitUse);
|
||||
$this->classHasChanged = \true;
|
||||
}
|
||||
}
|
||||
// invoke re-print
|
||||
if ($this->classHasChanged) {
|
||||
$node->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE, null);
|
||||
@ -85,20 +79,4 @@ CODE_SAMPLE
|
||||
{
|
||||
$this->traitsToRemove = $configuration[self::TRAITS_TO_REMOVE] ?? [];
|
||||
}
|
||||
/**
|
||||
* @param Name[] $usedTraits
|
||||
*/
|
||||
private function removeTraits(array $usedTraits) : void
|
||||
{
|
||||
foreach ($usedTraits as $usedTrait) {
|
||||
foreach ($this->traitsToRemove as $traitToRemove) {
|
||||
if (!$this->isName($usedTrait, $traitToRemove)) {
|
||||
continue;
|
||||
}
|
||||
$this->removeNode($usedTrait);
|
||||
$this->classHasChanged = \true;
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -16,11 +16,11 @@ final class VersionResolver
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '90b213c51914957326ef4d4f942982e772abbce4';
|
||||
public const PACKAGE_VERSION = '5f43d6b712538efcbcb976418c2e856de0468e3a';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2021-07-05 10:57:28';
|
||||
public const RELEASE_DATE = '2021-07-05 09:39:26';
|
||||
public static function resolvePackageVersion() : string
|
||||
{
|
||||
$process = new \RectorPrefix20210705\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
|
||||
|
34
src/NodeAnalyzer/EnumAnalyzer.php
Normal file
34
src/NodeAnalyzer/EnumAnalyzer.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Core\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
final class EnumAnalyzer
|
||||
{
|
||||
/**
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
}
|
||||
/**
|
||||
* @see https://github.com/myclabs/php-enum#declaration
|
||||
*/
|
||||
public function isEnumClassConst(\PhpParser\Node\Stmt\ClassConst $classConst) : bool
|
||||
{
|
||||
$classLike = $classConst->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
||||
if (!$classLike instanceof \PhpParser\Node\Stmt\Class_) {
|
||||
return \false;
|
||||
}
|
||||
if ($classLike->extends === null) {
|
||||
return \false;
|
||||
}
|
||||
return $this->nodeNameResolver->isName($classLike->extends, '*Enum');
|
||||
}
|
||||
}
|
@ -7,10 +7,10 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeCollector\NodeCollector\NodeRepository;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
final class ClassConstManipulator
|
||||
@ -19,70 +19,44 @@ final class ClassConstManipulator
|
||||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
/**
|
||||
* @var \Rector\Core\NodeManipulator\ClassManipulator
|
||||
*/
|
||||
private $classManipulator;
|
||||
/**
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @var \Rector\NodeCollector\NodeCollector\NodeRepository
|
||||
* @var \Rector\Core\PhpParser\AstResolver
|
||||
*/
|
||||
private $nodeRepository;
|
||||
/**
|
||||
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
||||
*/
|
||||
private $nodeComparator;
|
||||
public function __construct(\Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\Core\NodeManipulator\ClassManipulator $classManipulator, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\NodeCollector\NodeCollector\NodeRepository $nodeRepository, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator)
|
||||
private $astResolver;
|
||||
public function __construct(\Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\AstResolver $astResolver)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->classManipulator = $classManipulator;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->nodeRepository = $nodeRepository;
|
||||
$this->nodeComparator = $nodeComparator;
|
||||
$this->astResolver = $astResolver;
|
||||
}
|
||||
public function hasClassConstFetch(\PhpParser\Node\Stmt\ClassConst $classConst) : bool
|
||||
public function hasClassConstFetch(\PhpParser\Node\Stmt\ClassConst $classConst, \PHPStan\Reflection\ClassReflection $classReflection) : bool
|
||||
{
|
||||
$classLike = $classConst->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
||||
if (!$classLike instanceof \PhpParser\Node\Stmt\Class_) {
|
||||
return \false;
|
||||
}
|
||||
$searchInNodes = [$classLike];
|
||||
$usedTraitNames = $this->classManipulator->getUsedTraits($classLike);
|
||||
foreach ($usedTraitNames as $usedTraitName) {
|
||||
$usedTraitName = $this->nodeRepository->findTrait((string) $usedTraitName);
|
||||
if (!$usedTraitName instanceof \PhpParser\Node\Stmt\Trait_) {
|
||||
foreach ($classReflection->getAncestors() as $ancestorClassReflection) {
|
||||
$ancestorClass = $this->astResolver->resolveClassFromClassReflection($ancestorClassReflection, $ancestorClassReflection->getName());
|
||||
if (!$ancestorClass instanceof \PhpParser\Node\Stmt\ClassLike) {
|
||||
continue;
|
||||
}
|
||||
$searchInNodes[] = $usedTraitName;
|
||||
}
|
||||
return (bool) $this->betterNodeFinder->find($searchInNodes, function (\PhpParser\Node $node) use($classConst) : bool {
|
||||
// itself
|
||||
if ($this->nodeComparator->areNodesEqual($node, $classConst)) {
|
||||
return \false;
|
||||
// has in class?
|
||||
$isClassConstFetchFound = (bool) $this->betterNodeFinder->find($ancestorClass, function (\PhpParser\Node $node) use($classConst) : bool {
|
||||
// property + static fetch
|
||||
if (!$node instanceof \PhpParser\Node\Expr\ClassConstFetch) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isNameMatch($node, $classConst);
|
||||
});
|
||||
if ($isClassConstFetchFound) {
|
||||
return \true;
|
||||
}
|
||||
// property + static fetch
|
||||
if (!$node instanceof \PhpParser\Node\Expr\ClassConstFetch) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isNameMatch($node, $classConst);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @see https://github.com/myclabs/php-enum#declaration
|
||||
*/
|
||||
public function isEnum(\PhpParser\Node\Stmt\ClassConst $classConst) : bool
|
||||
{
|
||||
$classLike = $classConst->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
||||
if (!$classLike instanceof \PhpParser\Node\Stmt\Class_) {
|
||||
return \false;
|
||||
}
|
||||
if ($classLike->extends === null) {
|
||||
return \false;
|
||||
}
|
||||
return $this->nodeNameResolver->isName($classLike->extends, '*Enum');
|
||||
return \false;
|
||||
}
|
||||
private function isNameMatch(\PhpParser\Node\Expr\ClassConstFetch $classConstFetch, \PhpParser\Node\Stmt\ClassConst $classConst) : bool
|
||||
{
|
||||
|
@ -3,12 +3,10 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Core\NodeManipulator;
|
||||
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
@ -33,23 +31,6 @@ final class ClassManipulator
|
||||
$this->nodesToRemoveCollector = $nodesToRemoveCollector;
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
}
|
||||
/**
|
||||
* @deprecated
|
||||
* @return array<string, Name>
|
||||
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_ $classLike
|
||||
*/
|
||||
public function getUsedTraits($classLike) : array
|
||||
{
|
||||
$usedTraits = [];
|
||||
foreach ($classLike->getTraitUses() as $traitUse) {
|
||||
foreach ($traitUse->traits as $trait) {
|
||||
/** @var string $traitName */
|
||||
$traitName = $this->nodeNameResolver->getName($trait);
|
||||
$usedTraits[$traitName] = $trait;
|
||||
}
|
||||
}
|
||||
return $usedTraits;
|
||||
}
|
||||
public function hasParentMethodOrInterface(\PHPStan\Type\ObjectType $objectType, string $methodName) : bool
|
||||
{
|
||||
if (!$this->reflectionProvider->hasClass($objectType->getClassName())) {
|
||||
|
@ -7,15 +7,17 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PhpParser\NodeFinder;
|
||||
use PhpParser\Parser;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\Php\PhpFunctionReflection;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\TypeWithClassName;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
@ -49,9 +51,9 @@ final class AstResolver
|
||||
* Parsing files is very heavy performance, so this will help to leverage it
|
||||
* The value can be also null, as the method might not exist in the class.
|
||||
*
|
||||
* @var array<class-string, Class_|null>
|
||||
* @var array<class-string, Class_|Trait_|Interface_|null>
|
||||
*/
|
||||
private $classesByName = [];
|
||||
private $classLikesByName = [];
|
||||
/**
|
||||
* @var \PhpParser\Parser
|
||||
*/
|
||||
@ -100,13 +102,16 @@ final class AstResolver
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
$this->nodeTypeResolver = $nodeTypeResolver;
|
||||
}
|
||||
public function resolveClassFromObjectType(\PHPStan\Type\ObjectType $objectType) : ?\PhpParser\Node\Stmt\Class_
|
||||
/**
|
||||
* @return \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\Interface_|null
|
||||
*/
|
||||
public function resolveClassFromObjectType(\PHPStan\Type\TypeWithClassName $typeWithClassName)
|
||||
{
|
||||
if (!$this->reflectionProvider->hasClass($objectType->getClassName())) {
|
||||
if (!$this->reflectionProvider->hasClass($typeWithClassName->getClassName())) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $this->reflectionProvider->getClass($objectType->getClassName());
|
||||
return $this->resolveClassFromClassReflection($classReflection, $objectType->getClassName());
|
||||
$classReflection = $this->reflectionProvider->getClass($typeWithClassName->getClassName());
|
||||
return $this->resolveClassFromClassReflection($classReflection, $typeWithClassName->getClassName());
|
||||
}
|
||||
public function resolveClassMethodFromMethodReflection(\PHPStan\Reflection\MethodReflection $methodReflection) : ?\PhpParser\Node\Stmt\ClassMethod
|
||||
{
|
||||
@ -206,39 +211,42 @@ final class AstResolver
|
||||
}
|
||||
return $this->resolveClassMethod($callerStaticType->getClassName(), $methodName);
|
||||
}
|
||||
public function resolveClassFromClassReflection(\PHPStan\Reflection\ClassReflection $classReflection, string $className) : ?\PhpParser\Node\Stmt\Class_
|
||||
/**
|
||||
* @return \PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|null
|
||||
*/
|
||||
public function resolveClassFromClassReflection(\PHPStan\Reflection\ClassReflection $classReflection, string $className)
|
||||
{
|
||||
if ($classReflection->isBuiltin()) {
|
||||
return null;
|
||||
}
|
||||
if (isset($this->classesByName[$classReflection->getName()])) {
|
||||
return $this->classesByName[$classReflection->getName()];
|
||||
if (isset($this->classLikesByName[$classReflection->getName()])) {
|
||||
return $this->classLikesByName[$classReflection->getName()];
|
||||
}
|
||||
$fileName = $classReflection->getFileName();
|
||||
// probably internal class
|
||||
if ($fileName === \false) {
|
||||
// avoid parsing falsy-file again
|
||||
$this->classesByName[$classReflection->getName()] = null;
|
||||
$this->classLikesByName[$classReflection->getName()] = null;
|
||||
return null;
|
||||
}
|
||||
$fileContent = $this->smartFileSystem->readFile($fileName);
|
||||
$nodes = $this->parser->parse($fileContent);
|
||||
if ($nodes === null) {
|
||||
// avoid parsing falsy-file again
|
||||
$this->classesByName[$classReflection->getName()] = null;
|
||||
$this->classLikesByName[$classReflection->getName()] = null;
|
||||
return null;
|
||||
}
|
||||
/** @var Class_[] $classes */
|
||||
$classes = $this->betterNodeFinder->findInstanceOf($nodes, \PhpParser\Node\Stmt\Class_::class);
|
||||
/** @var array<Class_|Trait_|Interface_> $classLikes */
|
||||
$classLikes = $this->betterNodeFinder->findInstanceOf($nodes, \PhpParser\Node\Stmt\ClassLike::class);
|
||||
$reflectionClassName = $classReflection->getName();
|
||||
foreach ($classes as $class) {
|
||||
foreach ($classLikes as $classLike) {
|
||||
if ($reflectionClassName !== $className) {
|
||||
continue;
|
||||
}
|
||||
$this->classesByName[$classReflection->getName()] = $class;
|
||||
return $class;
|
||||
$this->classLikesByName[$classReflection->getName()] = $classLike;
|
||||
return $classLike;
|
||||
}
|
||||
$this->classesByName[$classReflection->getName()] = null;
|
||||
$this->classLikesByName[$classReflection->getName()] = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -4,4 +4,4 @@
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3::getLoader();
|
||||
return ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7::getLoader();
|
||||
|
3
vendor/composer/autoload_classmap.php
vendored
3
vendor/composer/autoload_classmap.php
vendored
@ -1837,6 +1837,7 @@ return array(
|
||||
'Rector\\Core\\NodeAnalyzer\\ClassAnalyzer' => $baseDir . '/src/NodeAnalyzer/ClassAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\CompactFuncCallAnalyzer' => $baseDir . '/src/NodeAnalyzer/CompactFuncCallAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ConstFetchAnalyzer' => $baseDir . '/src/NodeAnalyzer/ConstFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\EnumAnalyzer' => $baseDir . '/src/NodeAnalyzer/EnumAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ParamAnalyzer' => $baseDir . '/src/NodeAnalyzer/ParamAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PromotedPropertyParamCleaner' => $baseDir . '/src/NodeAnalyzer/PromotedPropertyParamCleaner.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PropertyFetchAnalyzer' => $baseDir . '/src/NodeAnalyzer/PropertyFetchAnalyzer.php',
|
||||
@ -2859,7 +2860,7 @@ return array(
|
||||
'Rector\\Removing\\Rector\\ClassMethod\\ArgumentRemoverRector' => $baseDir . '/rules/Removing/Rector/ClassMethod/ArgumentRemoverRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveInterfacesRector' => $baseDir . '/rules/Removing/Rector/Class_/RemoveInterfacesRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveParentRector' => $baseDir . '/rules/Removing/Rector/Class_/RemoveParentRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveTraitRector' => $baseDir . '/rules/Removing/Rector/Class_/RemoveTraitRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveTraitUseRector' => $baseDir . '/rules/Removing/Rector/Class_/RemoveTraitUseRector.php',
|
||||
'Rector\\Removing\\Rector\\FuncCall\\RemoveFuncCallArgRector' => $baseDir . '/rules/Removing/Rector/FuncCall/RemoveFuncCallArgRector.php',
|
||||
'Rector\\Removing\\Rector\\FuncCall\\RemoveFuncCallRector' => $baseDir . '/rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php',
|
||||
'Rector\\Removing\\ValueObject\\ArgumentRemover' => $baseDir . '/rules/Removing/ValueObject/ArgumentRemover.php',
|
||||
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3
|
||||
class ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,15 +22,15 @@ class ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7', 'loadClassLoader'));
|
||||
|
||||
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
||||
if ($useStaticLoader) {
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit41b2d08b01395424e71534000216a8d3::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit72b2647cd4535e261f0ad028fa7a67f7::getInitializer($loader));
|
||||
} else {
|
||||
$classMap = require __DIR__ . '/autoload_classmap.php';
|
||||
if ($classMap) {
|
||||
@ -42,19 +42,19 @@ class ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3
|
||||
$loader->register(true);
|
||||
|
||||
if ($useStaticLoader) {
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit41b2d08b01395424e71534000216a8d3::$files;
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit72b2647cd4535e261f0ad028fa7a67f7::$files;
|
||||
} else {
|
||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||
}
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire41b2d08b01395424e71534000216a8d3($fileIdentifier, $file);
|
||||
composerRequire72b2647cd4535e261f0ad028fa7a67f7($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
function composerRequire41b2d08b01395424e71534000216a8d3($fileIdentifier, $file)
|
||||
function composerRequire72b2647cd4535e261f0ad028fa7a67f7($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
require $file;
|
||||
|
11
vendor/composer/autoload_static.php
vendored
11
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit41b2d08b01395424e71534000216a8d3
|
||||
class ComposerStaticInit72b2647cd4535e261f0ad028fa7a67f7
|
||||
{
|
||||
public static $files = array (
|
||||
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
|
||||
@ -2192,6 +2192,7 @@ class ComposerStaticInit41b2d08b01395424e71534000216a8d3
|
||||
'Rector\\Core\\NodeAnalyzer\\ClassAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ClassAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\CompactFuncCallAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/CompactFuncCallAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ConstFetchAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ConstFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\EnumAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/EnumAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ParamAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ParamAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PromotedPropertyParamCleaner' => __DIR__ . '/../..' . '/src/NodeAnalyzer/PromotedPropertyParamCleaner.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PropertyFetchAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/PropertyFetchAnalyzer.php',
|
||||
@ -3214,7 +3215,7 @@ class ComposerStaticInit41b2d08b01395424e71534000216a8d3
|
||||
'Rector\\Removing\\Rector\\ClassMethod\\ArgumentRemoverRector' => __DIR__ . '/../..' . '/rules/Removing/Rector/ClassMethod/ArgumentRemoverRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveInterfacesRector' => __DIR__ . '/../..' . '/rules/Removing/Rector/Class_/RemoveInterfacesRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveParentRector' => __DIR__ . '/../..' . '/rules/Removing/Rector/Class_/RemoveParentRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveTraitRector' => __DIR__ . '/../..' . '/rules/Removing/Rector/Class_/RemoveTraitRector.php',
|
||||
'Rector\\Removing\\Rector\\Class_\\RemoveTraitUseRector' => __DIR__ . '/../..' . '/rules/Removing/Rector/Class_/RemoveTraitUseRector.php',
|
||||
'Rector\\Removing\\Rector\\FuncCall\\RemoveFuncCallArgRector' => __DIR__ . '/../..' . '/rules/Removing/Rector/FuncCall/RemoveFuncCallArgRector.php',
|
||||
'Rector\\Removing\\Rector\\FuncCall\\RemoveFuncCallRector' => __DIR__ . '/../..' . '/rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php',
|
||||
'Rector\\Removing\\ValueObject\\ArgumentRemover' => __DIR__ . '/../..' . '/rules/Removing/ValueObject/ArgumentRemover.php',
|
||||
@ -3844,9 +3845,9 @@ class ComposerStaticInit41b2d08b01395424e71534000216a8d3
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit41b2d08b01395424e71534000216a8d3::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit41b2d08b01395424e71534000216a8d3::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit41b2d08b01395424e71534000216a8d3::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit72b2647cd4535e261f0ad028fa7a67f7::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit72b2647cd4535e261f0ad028fa7a67f7::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit72b2647cd4535e261f0ad028fa7a67f7::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
|
||||
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
||||
spl_autoload_call('RectorPrefix20210705\AutoloadIncluder');
|
||||
}
|
||||
if (!class_exists('ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3', false) && !interface_exists('ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3', false) && !trait_exists('ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3', false)) {
|
||||
spl_autoload_call('RectorPrefix20210705\ComposerAutoloaderInit41b2d08b01395424e71534000216a8d3');
|
||||
if (!class_exists('ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7', false) && !interface_exists('ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7', false) && !trait_exists('ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7', false)) {
|
||||
spl_autoload_call('RectorPrefix20210705\ComposerAutoloaderInit72b2647cd4535e261f0ad028fa7a67f7');
|
||||
}
|
||||
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
|
||||
spl_autoload_call('RectorPrefix20210705\Doctrine\Inflector\Inflector');
|
||||
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
|
||||
return \RectorPrefix20210705\print_node(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('composerRequire41b2d08b01395424e71534000216a8d3')) {
|
||||
function composerRequire41b2d08b01395424e71534000216a8d3() {
|
||||
return \RectorPrefix20210705\composerRequire41b2d08b01395424e71534000216a8d3(...func_get_args());
|
||||
if (!function_exists('composerRequire72b2647cd4535e261f0ad028fa7a67f7')) {
|
||||
function composerRequire72b2647cd4535e261f0ad028fa7a67f7() {
|
||||
return \RectorPrefix20210705\composerRequire72b2647cd4535e261f0ad028fa7a67f7(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('parseArgs')) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user