Skip to content

Commit c8d063c

Browse files
committed
fix
1 parent 08107a5 commit c8d063c

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

rules/CodeQuality/NodeFactory/FromBinaryAndAssertExpressionsFactory.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpParser\Node\Expr\Instanceof_;
1414
use PhpParser\Node\Expr\Isset_;
1515
use PhpParser\Node\Expr\MethodCall;
16+
use PhpParser\Node\Expr\StaticCall;
1617
use PhpParser\Node\Name;
1718
use PhpParser\Node\Name\FullyQualified;
1819
use PhpParser\Node\Scalar\Int_;
@@ -33,14 +34,14 @@ public function __construct(
3334
* @param Expr[] $exprs
3435
* @return Stmt[]
3536
*/
36-
public function create(array $exprs): array
37+
public function create(array $exprs, bool $isStaticClosure = false): array
3738
{
3839
$assertMethodCalls = [];
3940

4041
foreach ($exprs as $expr) {
4142
// implicit bool compare
4243
if ($expr instanceof MethodCall) {
43-
$assertMethodCalls[] = $this->nodeFactory->createMethodCall('this', 'assertTrue', [$expr]);
44+
$assertMethodCalls[] = $this->createAssertMethodCall($isStaticClosure, 'assertTrue', [$expr]);
4445

4546
continue;
4647
}
@@ -51,8 +52,8 @@ public function create(array $exprs): array
5152
$dimExpr = $expr->getArgs()[0]
5253
->value;
5354

54-
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
55-
'this',
55+
$assertMethodCalls[] = $this->createAssertMethodCall(
56+
$isStaticClosure,
5657
'assertArrayHasKey',
5758
[$dimExpr, $variableExpr]
5859
);
@@ -63,8 +64,8 @@ public function create(array $exprs): array
6364
if ($expr instanceof Isset_) {
6465
foreach ($expr->vars as $issetVariable) {
6566
if ($issetVariable instanceof ArrayDimFetch) {
66-
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
67-
'this',
67+
$assertMethodCalls[] = $this->createAssertMethodCall(
68+
$isStaticClosure,
6869
'assertArrayHasKey',
6970
[$issetVariable->dim, $issetVariable->var]
7071
);
@@ -86,8 +87,8 @@ public function create(array $exprs): array
8687
$classNameExpr = $expr->class;
8788
}
8889

89-
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
90-
'this',
90+
$assertMethodCalls[] = $this->createAssertMethodCall(
91+
$isStaticClosure,
9192
'assertInstanceOf',
9293
[$classNameExpr, $expr->expr]
9394
);
@@ -102,8 +103,8 @@ public function create(array $exprs): array
102103
->value;
103104

104105
// create assertCount()
105-
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
106-
'this',
106+
$assertMethodCalls[] = $this->createAssertMethodCall(
107+
$isStaticClosure,
107108
'assertCount',
108109
[$expr->right, $countedExpr]
109110
);
@@ -116,8 +117,8 @@ public function create(array $exprs): array
116117
}
117118

118119
// create assertSame()
119-
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
120-
'this',
120+
$assertMethodCalls[] = $this->createAssertMethodCall(
121+
$isStaticClosure,
121122
$expr instanceof Identical ? 'assertSame' : 'assertEquals',
122123
[$expr->right, $expr->left]
123124
);
@@ -141,4 +142,16 @@ public function create(array $exprs): array
141142

142143
return $stmts;
143144
}
145+
146+
/**
147+
* @param Expr[] $args
148+
*/
149+
private function createAssertMethodCall(bool $isStaticClosure, string $method, array $args): MethodCall|StaticCall
150+
{
151+
if ($isStaticClosure) {
152+
return new StaticCall(new Name('self'), $method, $this->nodeFactory->createArgs($args));
153+
}
154+
155+
return $this->nodeFactory->createMethodCall('this', $method, $args);
156+
}
144157
}

rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ public function refactor(Node $node): MethodCall|null
125125
continue;
126126
}
127127

128-
$assertExprStmts = $this->fromBinaryAndAssertExpressionsFactory->create($joinedExprs);
128+
$innerFunctionLike = $argAndFunctionLike->getFunctionLike();
129+
$isStaticClosure = $innerFunctionLike instanceof Closure && $innerFunctionLike->static;
130+
131+
$assertExprStmts = $this->fromBinaryAndAssertExpressionsFactory->create($joinedExprs, $isStaticClosure);
129132
if ($assertExprStmts === []) {
130133
continue;
131134
}
@@ -134,7 +137,6 @@ public function refactor(Node $node): MethodCall|null
134137

135138
// last si return true;
136139
$assertExprStmts[] = new Return_($this->nodeFactory->createTrue());
137-
$innerFunctionLike = $argAndFunctionLike->getFunctionLike();
138140

139141
if ($innerFunctionLike instanceof Closure) {
140142
$innerFunctionLike->stmts = array_merge($nonReturnCallbackStmts, $assertExprStmts);

0 commit comments

Comments
 (0)