-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathFromBinaryAndAssertExpressionsFactory.php
More file actions
134 lines (112 loc) · 4.19 KB
/
FromBinaryAndAssertExpressionsFactory.php
File metadata and controls
134 lines (112 loc) · 4.19 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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\NodeFactory;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\NodeFactory;
final readonly class FromBinaryAndAssertExpressionsFactory
{
public function __construct(
private NodeFactory $nodeFactory,
private NodeNameResolver $nodeNameResolver,
) {
}
/**
* @param Expr[] $exprs
* @return Stmt[]|null
*/
public function create(array $exprs): ?array
{
$assertMethodCalls = [];
foreach ($exprs as $expr) {
if ($expr instanceof FuncCall && $this->nodeNameResolver->isName($expr, 'array_key_exists')) {
$variableExpr = $expr->getArgs()[1]
->value;
$dimExpr = $expr->getArgs()[0]
->value;
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
'assertArrayHasKey',
[$dimExpr, $variableExpr]
);
continue;
}
if ($expr instanceof Isset_) {
foreach ($expr->vars as $issetVariable) {
if ($issetVariable instanceof ArrayDimFetch) {
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
'assertArrayHasKey',
[$issetVariable->dim, $issetVariable->var]
);
} else {
// not supported yet
return null;
}
}
continue;
}
if ($expr instanceof Instanceof_) {
if ($expr->class instanceof Name) {
$classNameExpr = new ClassConstFetch(new FullyQualified($expr->class->name), 'class');
} else {
$classNameExpr = $expr->class;
}
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
'assertInstanceOf',
[$classNameExpr, $expr->expr]
);
continue;
}
if ($expr instanceof Identical || $expr instanceof Equal) {
if ($expr->left instanceof FuncCall && $this->nodeNameResolver->isName($expr->left, 'count')) {
if ($expr->right instanceof Int_) {
$countedExpr = $expr->left->getArgs()[0]
->value;
// create assertCount()
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
'assertCount',
[$expr->right, $countedExpr]
);
continue;
}
// unclear, fallback to no change
return null;
}
// create assertSame()
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
$expr instanceof Identical ? 'assertSame' : 'assertEquals',
[$expr->right, $expr->left]
);
} else {
// not supported expr
return null;
}
}
if ($assertMethodCalls === []) {
return null;
}
// to keep order from binary
$assertMethodCalls = array_reverse($assertMethodCalls);
$stmts = [];
foreach ($assertMethodCalls as $assertMethodCall) {
$stmts[] = new Expression($assertMethodCall);
}
return $stmts;
}
}