Skip to content

Commit

Permalink
Re-use name scope (#2171)
Browse files Browse the repository at this point in the history
* re-use name scope

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Apr 26, 2022
1 parent 1928480 commit ba29e3f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down
20 changes: 17 additions & 3 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

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;
Expand Down Expand Up @@ -49,15 +51,27 @@ public function getRuleDefinition(): RuleDefinition
*/
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;
Expand Down
7 changes: 6 additions & 1 deletion rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -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
) {
}

Expand All @@ -43,6 +45,9 @@ public function narrowToFullyQualifiedOrAliasedObjectType(
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)) {
Expand Down

0 comments on commit ba29e3f

Please sign in to comment.