Repository navigation
Expand file tree
/
Copy pathFuncCallStaticCallToMethodCallAnalyzer.php
More file actions
79 lines (67 loc) · 2.68 KB
/
Copy pathFuncCallStaticCallToMethodCallAnalyzer.php
File metadata and controls
79 lines (67 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Rector\Transform\NodeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\ObjectType;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeManipulator\ClassDependencyManipulator;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\NodeFactory;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Transform\NodeFactory\PropertyFetchFactory;
use Rector\Transform\NodeTypeAnalyzer\TypeProvidingExprFromClassResolver;
final readonly class FuncCallStaticCallToMethodCallAnalyzer
{
public function __construct(
private TypeProvidingExprFromClassResolver $typeProvidingExprFromClassResolver,
private PropertyNaming $propertyNaming,
private NodeNameResolver $nodeNameResolver,
private NodeFactory $nodeFactory,
private PropertyFetchFactory $propertyFetchFactory,
private ClassDependencyManipulator $classDependencyManipulator,
) {
}
public function matchTypeProvidingExpr(
Class_ $class,
ClassMethod $classMethod,
ObjectType $objectType,
): MethodCall | PropertyFetch | Variable | null {
$expr = $this->typeProvidingExprFromClassResolver->resolveTypeProvidingExprFromClass(
$class,
$classMethod,
$objectType,
);
if ($expr instanceof Expr) {
if ($expr instanceof Variable) {
$this->addClassMethodParamForVariable($expr, $objectType, $classMethod);
}
return $expr;
}
// Cannot add constructor dependency when nearest parent constructor is final
if ($this->classDependencyManipulator->hasFinalParentConstructor($class)) {
return null;
}
$propertyName = $this->propertyNaming->fqnToVariableName($objectType);
$this->classDependencyManipulator->addConstructorDependency(
$class,
new PropertyMetadata($propertyName, $objectType)
);
return $this->propertyFetchFactory->createFromType($objectType);
}
private function addClassMethodParamForVariable(
Variable $variable,
ObjectType $objectType,
ClassMethod | Function_ $functionLike
): void {
/** @var string $variableName */
$variableName = $this->nodeNameResolver->getName($variable);
// add variable to __construct as dependency
$functionLike->params[] = $this->nodeFactory->createParamFromNameAndType($variableName, $objectType);
}
}