Skip to content
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
2 changes: 1 addition & 1 deletion src/PhpParser/Node/Manipulator/FunctionLikeManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getReturnedLocalPropertyNames(FunctionLike $functionLike): array
return null;
}

if (! $this->propertyFetchManipulator->isLocalProperty($node->expr)) {
if (! $this->propertyFetchManipulator->isLocalPropertyFetch($node->expr)) {
return null;
}

Expand Down
66 changes: 9 additions & 57 deletions src/PhpParser/Node/Manipulator/PropertyFetchManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
Expand All @@ -44,21 +42,14 @@ final class PropertyFetchManipulator
*/
private $nodeNameResolver;

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(
NodeTypeResolver $nodeTypeResolver,
ReflectionProvider $reflectionProvider,
NodeNameResolver $nodeNameResolver,
CallableNodeTraverser $callableNodeTraverser
NodeNameResolver $nodeNameResolver
) {
$this->nodeTypeResolver = $nodeTypeResolver;
$this->reflectionProvider = $reflectionProvider;
$this->nodeNameResolver = $nodeNameResolver;
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function isPropertyToSelf(PropertyFetch $propertyFetch): bool
Expand Down Expand Up @@ -108,33 +99,6 @@ public function isMagicOnType(PropertyFetch $propertyFetch, Type $type): bool
return ! $this->hasPublicProperty($propertyFetch, $nodeName);
}

/**
* @return string[]
*/
public function getPropertyNamesOfAssignOfVariable(Node $node, string $paramName): array
{
$propertyNames = [];

$this->callableNodeTraverser->traverseNodesWithCallable($node, function (Node $node) use (
$paramName,
&$propertyNames
) {
if (! $this->isVariableAssignToThisPropertyFetch($node, $paramName)) {
return null;
}

/** @var Assign $node */
$propertyName = $this->nodeNameResolver->getName($node->expr);
if ($propertyName) {
$propertyNames[] = $propertyName;
}

return null;
});

return $propertyNames;
}

/**
* Matches:
* "$this->someValue = $<variableName>;"
Expand All @@ -153,28 +117,23 @@ public function isVariableAssignToThisPropertyFetch(Node $node, string $variable
return false;
}

if (! $node->var instanceof PropertyFetch) {
return false;
}

// must be local property
return $this->nodeNameResolver->isName($node->var->var, 'this');
return $this->isLocalPropertyFetch($node->var);
}

/**
* @param string[] $propertyNames
*/
public function isLocalPropertyOfNames(Node $node, array $propertyNames): bool
{
if (! $this->isLocalProperty($node)) {
if (! $this->isLocalPropertyFetch($node)) {
return false;
}

/** @var PropertyFetch $node */
return $this->nodeNameResolver->isNames($node->name, $propertyNames);
}

public function isLocalProperty(Node $node): bool
public function isLocalPropertyFetch(Node $node): bool
{
if (! $node instanceof PropertyFetch) {
return false;
Expand All @@ -197,13 +156,7 @@ public function matchPropertyFetch(Node $node): ?Node
}

if ($node instanceof ArrayDimFetch) {
$nestedNode = $node->var;

while ($nestedNode instanceof ArrayDimFetch) {
$nestedNode = $nestedNode->var;
}

return $this->matchPropertyFetch($nestedNode);
return $this->matchPropertyFetch($node->var);
}

return null;
Expand All @@ -219,15 +172,14 @@ public function isToThisPropertyFetchOfSpecificNameAssign(Node $node, string $pr
return false;
}

if (! $node->var instanceof PropertyFetch) {
if (! $this->isLocalPropertyFetch($node->var)) {
return false;
}

if (! $this->nodeNameResolver->isName($node->var->var, 'this')) {
return false;
}
/** @var PropertyFetch $propertyFetch */
$propertyFetch = $node->var;

return $this->nodeNameResolver->isName($node->var->name, $propertyName);
return $this->nodeNameResolver->isName($propertyFetch->name, $propertyName);
}

private function hasPublicProperty(PropertyFetch $propertyFetch, string $propertyName): bool
Expand Down