Repository navigation
Expand file tree
/
Copy pathRemoveTernaryConditionWithDynamicFormFieldsNodeVisitor.php
More file actions
73 lines (58 loc) · 2.03 KB
/
Copy pathRemoveTernaryConditionWithDynamicFormFieldsNodeVisitor.php
File metadata and controls
73 lines (58 loc) · 2.03 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
<?php
declare(strict_types=1);
namespace Efabrica\PHPStanLatte\Compiler\NodeVisitor;
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\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\NodeVisitorAbstract;
final class RemoveTernaryConditionWithDynamicFormFieldsNodeVisitor 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 Ternary) {
return null;
}
if (!$node->cond instanceof FuncCall) {
return null;
}
if (!$node->else instanceof ArrayDimFetch) {
return null;
}
if ($this->nameResolver->resolve($node->cond) !== 'is_object') {
return null;
}
$functionArg = $node->cond->getArgs()[0] ?? null;
if ($functionArg === null) {
return null;
}
if (!$functionArg->value instanceof Assign) {
return null;
}
$argValueType = $this->getType($functionArg->value->expr);
if ($argValueType === null) {
return null;
}
if ($argValueType->isObject()->yes()) {
return $functionArg->value->expr;
}
$newDim = $functionArg->value->expr;
if ($argValueType->isInteger()->yes()) {
// cast integer to string, because now all names of form fields are strings
$newDim = new String_($newDim);
}
$node->else->dim = $newDim;
return $node->else;
}
}