-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathFromBinaryAndAssertExpressionsFactory.php
More file actions
95 lines (79 loc) · 2.74 KB
/
FromBinaryAndAssertExpressionsFactory.php
File metadata and controls
95 lines (79 loc) · 2.74 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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\NodeFactory;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
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 Instanceof_) {
if (! $expr->class instanceof Name) {
return null;
}
$className = $expr->class->name;
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
'assertInstanceOf',
[new ClassConstFetch(new FullyQualified($className), 'class'), $expr->expr]
);
continue;
}
if ($expr instanceof Identical) {
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',
'assertSame',
[$expr->right, $expr->left]
);
} else {
// not supported expr
return null;
}
}
if ($assertMethodCalls === []) {
return null;
}
$stmts = [];
foreach ($assertMethodCalls as $assertMethodCall) {
$stmts[] = new Expression($assertMethodCall);
}
return $stmts;
}
}