Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Skip __construct in inner class on TypedPropertyFromStrictConstructorRector #2010

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

class SkipInnerClass
{
/**
* @var string
*/
private $name;

public function __construct(string $name)
{
new class {
public function __construct(string $name)
{
$this->name = $name;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

class SkipInnerClass2
{
/**
* @var string
*/
private $name;

public function __construct()
{
new class {
public function __construct()
{
$this->name = 'value';
}
};
}
}
18 changes: 15 additions & 3 deletions src/NodeManipulator/ClassMethodPropertyFetchManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;

final class ClassMethodPropertyFetchManipulator
{
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly NodeNameResolver $nodeNameResolver
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder
) {
}

Expand All @@ -38,7 +40,7 @@ public function findParamAssignToPropertyName(ClassMethod $classMethod, string $

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
function (Node $node) use ($propertyName, &$assignedParamName): ?int {
function (Node $node) use ($propertyName, &$assignedParamName, $classMethod): ?int {
if (! $node instanceof Assign) {
return null;
}
Expand All @@ -51,6 +53,11 @@ function (Node $node) use ($propertyName, &$assignedParamName): ?int {
return null;
}

$parentClassMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
if ($parentClassMethod !== $classMethod) {
return null;
}

$assignedParamName = $this->nodeNameResolver->getName($node->expr);

return NodeTraverser::STOP_TRAVERSAL;
Expand Down Expand Up @@ -90,7 +97,7 @@ public function findAssignsToPropertyName(ClassMethod $classMethod, string $prop

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
function (Node $node) use ($propertyName, &$assignExprs, $paramNames): ?int {
function (Node $node) use ($propertyName, &$assignExprs, $paramNames, $classMethod): ?int {
if (! $node instanceof Assign) {
return null;
}
Expand All @@ -104,6 +111,11 @@ function (Node $node) use ($propertyName, &$assignExprs, $paramNames): ?int {
return null;
}

$parentClassMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
if ($parentClassMethod !== $classMethod) {
return null;
}

$assignExprs[] = $node->expr;
return null;
}
Expand Down