Re-use name scope (#2171)

* re-use name scope

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tomas Votruba 2022-04-26 14:55:06 +02:00 committed by GitHub
parent 19284802ae
commit ba29e3fd5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -582,7 +582,7 @@ parameters:
- '#Access to an undefined property PhpParser\\Node\\FunctionLike\|PhpParser\\Node\\Stmt\\Else_\|PhpParser\\Node\\Stmt\\Foreach_\|PhpParser\\Node\\Stmt\\If_\:\:\$stmts#'
- '#Access to an undefined property PhpParser\\Node\\FunctionLike\|PhpParser\\Node\\Stmt\\Foreach_\|PhpParser\\Node\\Stmt\\If_\|PhpParser\\Node\\Stmt\\Namespace_\|Rector\\Core\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace\:\:\$stmts#'
# stmts refactoring
- '#Cognitive complexity for "Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector\:\:refactor\(\)" is 13, keep it under 10#'
- '#Cognitive complexity for "Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector\:\:refactor\(\)" is \d+, keep it under 10#'
# todo add custom stmts aware interface
-

View File

@ -6,12 +6,14 @@ namespace Rector\DeadCode\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
@ -49,15 +51,27 @@ CODE_SAMPLE
*/
public function getNodeTypes(): array
{
return [Foreach_::class, FileWithoutNamespace::class, FunctionLike::class, If_::class, Namespace_::class];
return [
Foreach_::class,
FileWithoutNamespace::class,
ClassMethod::class,
Function_::class,
Closure::class,
If_::class,
Namespace_::class,
];
}
/**
* @param Foreach_|FileWithoutNamespace|FunctionLike|If_|Namespace_ $node
* @param Foreach_|FileWithoutNamespace|If_|Namespace_|ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node): ?Node
{
$stmts = $node->stmts;
if ($stmts === null) {
return null;
}
$hasChanged = false;
$previousStmt = null;

View File

@ -19,6 +19,7 @@ use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\Naming\NameScopeFactory;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;
@ -34,7 +35,8 @@ final class ObjectTypeSpecifier
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly UseImportsResolver $useImportsResolver,
private readonly array $typeWithClassTypeSpecifiers
private readonly array $typeWithClassTypeSpecifiers,
private readonly NameScopeFactory $nameScopeFactory
) {
}
@ -43,6 +45,9 @@ final class ObjectTypeSpecifier
ObjectType $objectType,
Scope|null $scope
): TypeWithClassName | NonExistingObjectType | UnionType | MixedType {
$this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($node);
// @todo reuse name scope
if ($scope instanceof Scope) {
foreach ($this->typeWithClassTypeSpecifiers as $typeWithClassTypeSpecifier) {
if ($typeWithClassTypeSpecifier->match($objectType, $scope)) {