Repository navigation
Expand file tree
/
Copy pathTransformFormStackToGetFormNodeVisitor.php
More file actions
110 lines (94 loc) · 3.46 KB
/
Copy pathTransformFormStackToGetFormNodeVisitor.php
File metadata and controls
110 lines (94 loc) · 3.46 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
<?php
declare(strict_types=1);
namespace Efabrica\PHPStanLatte\Compiler\NodeVisitor;
use Efabrica\PHPStanLatte\Compiler\Helper\FormHelper;
use Efabrica\PHPStanLatte\Compiler\NodeVisitor\Behavior\ExprTypeNodeVisitorBehavior;
use Efabrica\PHPStanLatte\Compiler\NodeVisitor\Behavior\ExprTypeNodeVisitorInterface;
use Efabrica\PHPStanLatte\Resolver\NameResolver\NameResolver;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\NodeVisitorAbstract;
/**
* changed output from:
* <code>
* $form = $this->global->formsStack[] = $this->global->uiControl[$formName];
* </code>
*
* to:
* <code>
* $form = FormHelper::getForm($this->global->formNamesToFormClasses[$formName]);
* </code>
*/
final class TransformFormStackToGetFormNodeVisitor extends NodeVisitorAbstract implements ExprTypeNodeVisitorInterface
{
use ExprTypeNodeVisitorBehavior;
private NameResolver $nameResolver;
public function __construct(NameResolver $nameResolver)
{
$this->nameResolver = $nameResolver;
}
public function enterNode(Node $node): ?Node
{
if (!$node instanceof Assign) {
return null;
}
if ($this->nameResolver->resolve($node->var) !== 'form') {
return null;
}
// nette/forms 3.3: {form $formName} compiles to begin($form = $this->global->uiControl[$formName])
if ($node->expr instanceof ArrayDimFetch) {
$dim = $node->expr->dim;
if ($dim instanceof Variable && $this->isUiControl($node->expr->var)) {
$dimType = $this->getType($dim);
if (!$dimType) {
return null;
}
return new Assign($node->var, new StaticCall(new Name(FormHelper::class), 'getForm', [
new Arg(new ArrayDimFetch(
new PropertyFetch(new PropertyFetch(new Variable('this'), 'global'), 'formNamesToFormClasses'),
$dim
)),
]), $node->getAttributes());
}
}
if (!$node->expr instanceof Assign) {
return null;
}
if (!$node->expr->expr instanceof ArrayDimFetch) {
return null;
}
/** @var ArrayDimFetch $dim */
$dim = $node->expr->expr;
if (!$dim->dim instanceof Variable) {
return null;
}
$dimType = $this->getType($dim->dim);
if (!$dimType) {
return null;
}
return new Assign($node->var, new StaticCall(new Name(FormHelper::class), 'getForm', [
new Arg(new ArrayDimFetch(new PropertyFetch(new PropertyFetch(new Variable('this'), 'global'), 'formNamesToFormClasses'), $dim->dim)),
]), $node->getAttributes());
}
private function isUiControl(Expr $expr): bool
{
if (!$expr instanceof PropertyFetch || !$expr->name instanceof Identifier) {
return false;
}
if ($this->nameResolver->resolve($expr->name) !== 'uiControl') {
return false;
}
if (!$expr->var instanceof PropertyFetch || !$expr->var->name instanceof Identifier) {
return false;
}
return $this->nameResolver->resolve($expr->var->name) === 'global';
}
}