Skip to content

Commit 538960f

Browse files
committed
add isset support, revert order to respect original one
1 parent 7770d83 commit 538960f

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/closure_instance_of.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ final class ClosureInstanceOf extends TestCase
3535
$someMock->expects($this->any())
3636
->method('trans')
3737
->with($this->callback(function ($args): void {
38-
$this->assertInstanceOf(\stdClass::class, $args[0]);
3938
$this->assertCount(5, $args);
39+
$this->assertInstanceOf(\stdClass::class, $args[0]);
4040
}));
4141
}
4242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class IssetArrayKeys extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
return count($args) === 5 && isset($args[0]) && $args[0] === 'some_value';
17+
}));
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
29+
final class IssetArrayKeys extends TestCase
30+
{
31+
public function test()
32+
{
33+
$someMock = $this->getMockBuilder('AnyType')->getMock();
34+
35+
$someMock->expects($this->any())
36+
->method('trans')
37+
->with($this->callback(function ($args): void {
38+
$this->assertCount(5, $args);
39+
$this->assertArrayHasKey(0, $args);
40+
$this->assertSame('some_value', $args[0]);
41+
}));
42+
}
43+
}
44+
45+
?>

rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/with_arrow_function.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ final class WithArrowFunction extends TestCase
3939
->with(
4040
$this->callback(
4141
function ($args): void {
42-
$this->assertInstanceOf(\stdClass::class, $args[0]);
4342
$this->assertCount(5, $args);
43+
$this->assertInstanceOf(\stdClass::class, $args[0]);
4444
}
4545
)
4646
);

rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/with_callback_assert.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ final class WithCallbackAssert extends TestCase
3535
$someMock->expects($this->any())
3636
->method('trans')
3737
->with($this->callback(function ($args): void {
38-
$this->assertSame('some_value', $args[0]);
3938
$this->assertCount(5, $args);
39+
$this->assertSame('some_value', $args[0]);
4040
}));
4141
}
4242
}

rules/CodeQuality/NodeFactory/FromBinaryAndAssertExpressionsFactory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Rector\PHPUnit\CodeQuality\NodeFactory;
66

7+
use PhpParser\Node\Expr\Isset_;
78
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\ArrayDimFetch;
810
use PhpParser\Node\Expr\BinaryOp\Identical;
911
use PhpParser\Node\Expr\ClassConstFetch;
1012
use PhpParser\Node\Expr\FuncCall;
@@ -34,6 +36,23 @@ public function create(array $exprs): ?array
3436
$assertMethodCalls = [];
3537

3638
foreach ($exprs as $expr) {
39+
if ($expr instanceof Isset_) {
40+
foreach ($expr->vars as $issetVariable) {
41+
if ($issetVariable instanceof ArrayDimFetch) {
42+
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
43+
'this',
44+
'assertArrayHasKey',
45+
[$issetVariable->dim, $issetVariable->var]
46+
);
47+
} else {
48+
// not supported yet
49+
return null;
50+
}
51+
}
52+
53+
continue;
54+
}
55+
3756
if ($expr instanceof Instanceof_) {
3857
if (! $expr->class instanceof Name) {
3958
return null;
@@ -85,6 +104,9 @@ public function create(array $exprs): ?array
85104
return null;
86105
}
87106

107+
// to keep order from binary
108+
$assertMethodCalls = array_reverse($assertMethodCalls);
109+
88110
$stmts = [];
89111
foreach ($assertMethodCalls as $assertMethodCall) {
90112
$stmts[] = new Expression($assertMethodCall);

0 commit comments

Comments
 (0)