Repository navigation
Expand file tree
/
Copy pathAddParametersForBlockNodeVisitor.php
More file actions
171 lines (148 loc) · 5.92 KB
/
Copy pathAddParametersForBlockNodeVisitor.php
File metadata and controls
171 lines (148 loc) · 5.92 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
<?php
declare(strict_types=1);
namespace Efabrica\PHPStanLatte\Compiler\NodeVisitor;
use Efabrica\PHPStanLatte\Resolver\NameResolver\NameResolver;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Parser\Parser;
use PHPStan\Parser\ParserErrorsException;
use function array_combine;
use function preg_match;
use function preg_match_all;
use function preg_replace;
use function str_contains;
use function str_replace;
use function str_starts_with;
use function trim;
final class AddParametersForBlockNodeVisitor extends NodeVisitorAbstract
{
private NameResolver $nameResolver;
private Parser $parser;
public function __construct(NameResolver $nameResolver, Parser $parser)
{
$this->nameResolver = $nameResolver;
$this->parser = $parser;
}
/**
* @return Node[]|null
*/
public function leaveNode(Node $node): ?array
{
if (!$node instanceof ClassMethod) {
return null;
}
$methodName = $this->nameResolver->resolve($node->name);
if ($methodName === null) {
return null;
}
if (!str_starts_with($methodName, 'block')) {
return null;
}
$comment = $node->getDocComment();
if ($comment === null) {
return null;
}
$parameters = [];
$pattern = '/(?<define>{define\s+(?<block_name>.*?)\s*,?\s+(?<parameters>.*)})\s+on line (?<line>\d+)/s';
if (preg_match($pattern, $comment->getText(), $match) === 1) {
$define = $match['define'];
$typesAndVariablesPattern = '/(?<type>[\?\\\[\]\<\>[:alnum:]]*)[ ]*\$(?<variable>[[:alnum:]]+)/s';
preg_match_all($typesAndVariablesPattern, $match['parameters'], $typesAndVariables);
$variableTypes = array_combine($typesAndVariables['variable'], $typesAndVariables['type']) ?: [];
foreach ($variableTypes as $variable => $type) {
// parameters fallback if parser will fail
$parameters[$variable] = [
'variable' => $variable,
'type' => $type,
'default' => null,
];
if (str_contains($type, '[]')) {
// replace something[] to array because it is not supported by php for now
$define = str_replace($type . ' ', 'array ', $define);
}
}
// create php code from latte code of define
$phpContent = '<?php ' . preg_replace(['/^{define /', '/' . $match['block_name'] . ',? /', '/}$/'], ['function ', $methodName . '(', ') {}'], $define);
try {
// parse php code
$stmts = $this->parser->parseString($phpContent);
$function = $stmts[0] ?? null;
if ($function instanceof Function_) {
foreach ($function->params as $param) {
$variable = $this->nameResolver->resolve($param->var);
if ($variable === null) {
continue;
}
$parameters[$variable] = [
'variable' => $variable,
'type' => $variableTypes[$variable] ?? '',
'default' => $param->default,
];
}
}
} catch (ParserErrorsException $e) {
}
} else {
// process default blocks content etc.
$pattern = '/{block\s+(?<block_name>.*?)}\s+on line (?<line>\d+)/s';
preg_match($pattern, $comment->getText(), $match);
}
$nodeComment = isset($match['line']) ? '/* line ' . $match['line'] . ' */' : '';
$node->params = [];
if ($parameters) {
$nodeComment .= "\n/**\n";
foreach ($parameters as $parameter) {
$type = trim($parameter['type']);
$nodeComment .= ' * @param ' . ($type ? $type . ' ' : '') . '$' . $parameter['variable'] . "\n";
$node->params[] = new Param(new Variable($parameter['variable']), $parameter['default']);
}
$nodeComment .= '*/';
}
$node->setDocComment(new Doc($nodeComment));
$stmts = (array)$node->stmts;
$newStmts = [];
foreach ($stmts as $stmt) {
if ($this->isAssignVariablesFromLargs($stmt)) {
// remove assign variables from ʟ_args e.g. $param = $ʟ_args[0] ?? $ʟ_args['param'] ?? null;
continue;
} elseif ($this->isUnsetLargs($stmt)) {
// remove unset($ʟ_args);
continue;
}
$newStmts[] = $stmt;
}
$node->stmts = $newStmts;
return [$node];
}
private function isAssignVariablesFromLargs(Stmt $stmt): bool
{
if (!($stmt instanceof Expression && $stmt->expr instanceof Assign)) {
return false;
}
if (!$stmt->expr->expr instanceof Coalesce) {
return false;
}
if (!($stmt->expr->expr->left instanceof ArrayDimFetch && $stmt->expr->expr->left->var instanceof Variable)) {
return false;
}
if ($this->nameResolver->resolve($stmt->expr->expr->left->var->name) === 'ʟ_args') {
return true;
}
return false;
}
private function isUnsetLargs(Stmt $stmt): bool
{
return $stmt instanceof Unset_ && ($stmt->vars[0] ?? null) instanceof Variable && $this->nameResolver->resolve($stmt->vars[0]->name) === 'ʟ_args';
}
}