PHP 8.2.31
Preview: ClassMethodPropertyFetchManipulator.php Size: 4.25 KB
/opt/cpanel/ea-wappspector/vendor/rector/rector/src/NodeManipulator/ClassMethodPropertyFetchManipulator.php

<?php

declare (strict_types=1);
namespace Rector\NodeManipulator;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
final class ClassMethodPropertyFetchManipulator
{
    /**
     * @readonly
     * @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
     */
    private $simpleCallableNodeTraverser;
    /**
     * @readonly
     * @var \Rector\NodeNameResolver\NodeNameResolver
     */
    private $nodeNameResolver;
    /**
     * @readonly
     * @var \Rector\NodeManipulator\FunctionLikeManipulator
     */
    private $functionLikeManipulator;
    public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, NodeNameResolver $nodeNameResolver, \Rector\NodeManipulator\FunctionLikeManipulator $functionLikeManipulator)
    {
        $this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
        $this->nodeNameResolver = $nodeNameResolver;
        $this->functionLikeManipulator = $functionLikeManipulator;
    }
    /**
     * In case the property name is different to param name:
     *
     * E.g.:
     * (SomeType $anotherValue)
     * $this->value = $anotherValue;
     * ↓
     * (SomeType $anotherValue)
     */
    public function findParamAssignToPropertyName(ClassMethod $classMethod, string $propertyName) : ?Param
    {
        $assignedParamName = null;
        $this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use($propertyName, &$assignedParamName) : ?int {
            if ($node instanceof Class_) {
                return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
            }
            if (!$node instanceof Assign) {
                return null;
            }
            if (!$node->var instanceof PropertyFetch && !$node->var instanceof StaticPropertyFetch) {
                return null;
            }
            if (!$this->nodeNameResolver->isName($node->var, $propertyName)) {
                return null;
            }
            if ($node->expr instanceof MethodCall || $node->expr instanceof StaticCall) {
                return null;
            }
            $assignedParamName = $this->nodeNameResolver->getName($node->expr);
            return NodeTraverser::STOP_TRAVERSAL;
        });
        /** @var string|null $assignedParamName */
        if ($assignedParamName === null) {
            return null;
        }
        /** @var Param $param */
        foreach ($classMethod->params as $param) {
            if (!$this->nodeNameResolver->isName($param, $assignedParamName)) {
                continue;
            }
            return $param;
        }
        return null;
    }
    /**
     * E.g.:
     * $this->value = 1000;
     * ↓
     * (int $value)
     *
     * @return Expr[]
     */
    public function findAssignsToPropertyName(ClassMethod $classMethod, string $propertyName) : array
    {
        $assignExprs = [];
        $paramNames = $this->functionLikeManipulator->resolveParamNames($classMethod);
        $this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use($propertyName, &$assignExprs, $paramNames) : ?int {
            if ($node instanceof Class_) {
                return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
            }
            if (!$node instanceof Assign) {
                return null;
            }
            if (!$node->var instanceof PropertyFetch && !$node->var instanceof StaticPropertyFetch) {
                return null;
            }
            if (!$this->nodeNameResolver->isName($node->var, $propertyName)) {
                return null;
            }
            // skip param assigns
            if ($this->nodeNameResolver->isNames($node->expr, $paramNames)) {
                return null;
            }
            $assignExprs[] = $node->expr;
            return null;
        });
        return $assignExprs;
    }
}

Directory Contents

Dirs: 0 × Files: 15

Name Size Perms Modified Actions
2.92 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
4.83 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
2.61 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
9.08 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
2.61 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.79 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.85 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
2.18 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
4.25 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.08 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
755 B lrw-r--r-- 2024-11-08 13:59:10
Edit Download
6.19 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
2.46 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
8.65 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
3.08 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).