Repository navigation
Expand file tree
/
Copy pathLinkNodeVisitor.php
More file actions
174 lines (149 loc) · 5.33 KB
/
Copy pathLinkNodeVisitor.php
File metadata and controls
174 lines (149 loc) · 5.33 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace Efabrica\PHPStanLatte\Compiler\NodeVisitor;
use Efabrica\PHPStanLatte\Compiler\NodeVisitor\Behavior\ActualClassNodeVisitorBehavior;
use Efabrica\PHPStanLatte\Compiler\NodeVisitor\Behavior\ActualClassNodeVisitorInterface;
use Efabrica\PHPStanLatte\Error\Error;
use Efabrica\PHPStanLatte\LinkProcessor\LinkProcessorFactory;
use Efabrica\PHPStanLatte\LinkProcessor\LinkProcessorInterface;
use Efabrica\PHPStanLatte\Resolver\NameResolver\NameResolver;
use Nette\Application\InvalidPresenterException;
use PhpParser\ConstExprEvaluationException;
use PhpParser\ConstExprEvaluator;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeVisitorAbstract;
use Throwable;
use function array_slice;
use function count;
use function in_array;
use function is_string;
use function ltrim;
use function strpos;
use function substr;
/**
* changed output from:
* <code>
* echo LR\Filters::escapeHtmlAttr($this->global->uiPresenter->link("MyPresenter:myAction", [$parameter1, $parameter2]));
* </code>
*
* to:
* <code>
* $myPresenter->actionMyAction($parameter1, $parameter2);
* $myPresenter->renderMyAction($parameter1, $parameter2);
* </code>
*/
final class LinkNodeVisitor extends NodeVisitorAbstract implements ActualClassNodeVisitorInterface
{
use ActualClassNodeVisitorBehavior;
private NameResolver $nameResolver;
private LinkProcessorFactory $linkProcessorFactory;
public function __construct(
NameResolver $nameResolver,
LinkProcessorFactory $linkProcessorFactory
) {
$this->nameResolver = $nameResolver;
$this->linkProcessorFactory = $linkProcessorFactory;
}
/**
* @return Node[]|null
*/
public function leaveNode(Node $node): ?array
{
if (!$node instanceof Echo_) {
return null;
}
$staticCall = $node->exprs[0] ?? null;
if (!$staticCall instanceof StaticCall) {
return null;
}
if (count($staticCall->getArgs()) !== 1) {
return null;
}
$arg = $staticCall->getArgs()[0];
$methodCall = $arg->value;
if (!$methodCall instanceof MethodCall) {
return null;
}
if (!$this->isMethodCallUiLink($methodCall)) {
return null;
}
return $this->prepareNodes($methodCall, $node->getAttributes());
}
/**
* @param array<string, mixed> $attributes
* @return Node[]|null
*/
private function prepareNodes(MethodCall $methodCall, array $attributes): ?array
{
$linkArgs = $methodCall->getArgs();
$target = $linkArgs[0]->value;
try {
$targetName = (new ConstExprEvaluator())->evaluateDirectly($target);
} catch (ConstExprEvaluationException $e) {
return null;
}
if (!is_string($targetName)) {
return null;
}
$targetName = ltrim($targetName, '/');
// remove anchor
$hashPosition = strpos($targetName, '#');
if ($hashPosition !== false) {
$targetName = substr($targetName, 0, $hashPosition);
}
// ignore links to this, it requires parameters from request which is not available in static analysis
if ($targetName === 'this') {
return null;
}
$linkProcessor = $this->linkProcessorFactory->create($targetName);
if (!$linkProcessor instanceof LinkProcessorInterface) {
return null;
}
$linkProcessor->setActualClass($this->actualClass);
$linkParams = array_slice($linkArgs, 1);
try {
$expressions = $linkProcessor->createLinkExpressions($targetName, $linkParams, $attributes);
} catch (InvalidPresenterException $e) {
$errorNode = (new Error($e->getMessage()))
->setTip('Check if your PHPStan configuration for latte > applicationMapping is correct. See https://github.com/efabrica-team/phpstan-latte/docs/configuration.md#applicationmapping')
->toNode();
$errorNode->setAttributes($attributes);
return [$errorNode];
} catch (Throwable $e) {
$errorNode = (new Error($e->getMessage()))
->toNode();
$errorNode->setAttributes($attributes);
return [$errorNode];
}
if ($expressions === []) {
return null;
}
return [
new If_(new Identical(new FuncCall(new Name('uniqid')), new String_('random')), [
'stmts' => $expressions,
], $attributes),
];
}
private function isMethodCallUiLink(MethodCall $methodCall): bool
{
$methodName = $this->nameResolver->resolve($methodCall->name);
if ($methodName !== 'link') {
return false;
}
$propertyFetch = $methodCall->var;
if (!$propertyFetch instanceof PropertyFetch) {
return false;
}
$propertyFetchName = $this->nameResolver->resolve($propertyFetch->name);
return in_array($propertyFetchName, ['uiControl', 'uiPresenter'], true);
}
}