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

Simplify PropertyFetchByMethodAnalyzer #6147

Merged
merged 2 commits into from
Apr 15, 2021
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
4 changes: 1 addition & 3 deletions .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ jobs:
fail-fast: false
matrix:
directories:
- src tests
- packages packages-tests
- src tests rules-tests packages packages-tests
- rules
- rules-tests

runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,4 @@ parameters:
message: '#\$this as argument is not allowed\. Refactor method to service composition#'
path: rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php

-
message: '#Class cognitive complexity is 32, keep it under 30#'
path: rules/Privatization/Rector/Class_/ChangeLocalPropertyToVariableRector.php

- '#Method Rector\\Core\\PhpParser\\Node\\BetterNodeFinder\:\:findParentTypes\(\) should return T of PhpParser\\Node\|null but returns class\-string<T of PhpParser\\Node\>\|T of PhpParser\\Node#'
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private function isLocalPropertyOfNamesNotIdenticalToNull(Expr $expr, array $pro
) && $this->valueResolver->isNull($expr->right)) {
return true;
}

if (! $this->propertyFetchAnalyzer->isLocalPropertyOfNames($expr->right, $propertyNames)) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand Down Expand Up @@ -96,6 +97,7 @@ public function refactor(Node $node): ?Node
}

if ($phpDocInfo->hasChanged()) {
$this->file->addRectorClassWithLine(new RectorWithLineChange($this, $node->getLine()));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -93,6 +94,7 @@ public function refactor(Node $node): ?Node
}

if ($phpDocInfo->hasChanged()) {
$this->file->addRectorClassWithLine(new RectorWithLineChange($this, $node->getLine()));
return $node;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\PhpDoc\TagRemover\ReturnTagRemover;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -77,6 +78,7 @@ public function refactor(Node $node): ?Node
$this->returnTagRemover->removeReturnTagIfUseless($phpDocInfo, $node);

if ($phpDocInfo->hasChanged()) {
$this->file->addRectorClassWithLine(new RectorWithLineChange($this, $node->getLine()));
return $node;
}

Expand Down
2 changes: 2 additions & 0 deletions rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -69,6 +70,7 @@ public function refactor(Node $node): ?Node
$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $node);

if ($phpDocInfo->hasChanged()) {
$this->file->addRectorClassWithLine(new RectorWithLineChange($this, $node->getLine()));
return $node;
}

Expand Down
192 changes: 192 additions & 0 deletions rules/Privatization/NodeAnalyzer/PropertyFetchByMethodAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

declare(strict_types=1);

namespace Rector\Privatization\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeTraverser;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;

final class PropertyFetchByMethodAnalyzer
{
/**
* @var array<class-string<Stmt>>
*/
private const SCOPE_CHANGING_NODE_TYPES = [Do_::class, While_::class, If_::class, Else_::class];

/**
* @var NodeNameResolver
*/
private $nodeNameResolver;

/**
* @var PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;

/**
* @var SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;

public function __construct(
NodeNameResolver $nodeNameResolver,
PropertyFetchAnalyzer $propertyFetchAnalyzer,
SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
$this->nodeNameResolver = $nodeNameResolver;
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
}

/**
* @param string[] $propertyNames
* @return array<string, string[]>
*/
public function collectPropertyFetchByMethods(Class_ $class, array $propertyNames): array
{
$propertyUsageByMethods = [];

foreach ($propertyNames as $propertyName) {
foreach ($class->getMethods() as $classMethod) {
// assigned in constructor injection → skip
if ($this->nodeNameResolver->isName($classMethod, MethodName::CONSTRUCT)) {
return [];
}

if (! $this->propertyFetchAnalyzer->containsLocalPropertyFetchName($classMethod, $propertyName)) {
continue;
}

if ($this->isPropertyChangingInMultipleMethodCalls($classMethod, $propertyName)) {
continue;
}

$classMethodName = $this->nodeNameResolver->getName($classMethod);
$propertyUsageByMethods[$propertyName][] = $classMethodName;
}
}

return $propertyUsageByMethods;
}

/**
* Covers https://github.com/rectorphp/rector/pull/2558#discussion_r363036110
*/
private function isPropertyChangingInMultipleMethodCalls(ClassMethod $classMethod, string $propertyName): bool
{
$isPropertyChanging = false;
$isPropertyReadInIf = false;
$isIfFollowedByAssign = false;

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->getStmts(),
function (Node $node) use (
&$isPropertyChanging,
$propertyName,
&$isPropertyReadInIf,
&$isIfFollowedByAssign
): ?int {
if ($isPropertyReadInIf) {
if (! $this->propertyFetchAnalyzer->isLocalPropertyOfNames($node, [$propertyName])) {
return null;
}

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Assign && $parentNode->var === $node) {
$isIfFollowedByAssign = true;
}
}

if (! $this->isScopeChangingNode($node)) {
return null;
}

if ($node instanceof If_) {
$isPropertyReadInIf = $this->refactorIf($node, $propertyName);
}

$isPropertyChanging = $this->isPropertyChanging($node, $propertyName);
if (! $isPropertyChanging) {
return null;
}

return NodeTraverser::STOP_TRAVERSAL;
}
);

return $isPropertyChanging || $isIfFollowedByAssign || $isPropertyReadInIf;
}

private function isScopeChangingNode(Node $node): bool
{
foreach (self::SCOPE_CHANGING_NODE_TYPES as $type) {
if (is_a($node, $type, true)) {
return true;
}
}

return false;
}

private function refactorIf(If_ $if, string $privatePropertyName): ?bool
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($if->cond, function (Node $node) use (
$privatePropertyName,
&$isPropertyReadInIf
): ?int {
if (! $this->propertyFetchAnalyzer->isLocalPropertyOfNames($node, [$privatePropertyName])) {
return null;
}

$isPropertyReadInIf = true;

return NodeTraverser::STOP_TRAVERSAL;
});

return $isPropertyReadInIf;
}

private function isPropertyChanging(Node $node, string $privatePropertyName): bool
{
$isPropertyChanging = false;
// here cannot be any property assign

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($node, function (Node $node) use (
&$isPropertyChanging,
$privatePropertyName
): ?int {
if (! $node instanceof Assign) {
return null;
}

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

if (! $this->nodeNameResolver->isName($node->var->name, $privatePropertyName)) {
return null;
}

$isPropertyChanging = true;

return NodeTraverser::STOP_TRAVERSAL;
});

return $isPropertyChanging;
}
}
Loading