Skip to content

Commit 9058a46

Browse files
authored
[CodeQuality] Skip on non-mock object on DecorateWillReturnMapWithExpectsMockRector (#655)
* [CodeQuality] Skip on non-mock object on DecorateWillReturnMapWithExpectsMockRector * fix
1 parent 2839ff3 commit 9058a46

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\Fixture;
6+
7+
use PHPUnit\Framework\MockObject\Stub;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\Source\SomeInterfaceToStub;
9+
10+
final class SkipOnCreateStub extends \PHPUnit\Framework\TestCase
11+
{
12+
private SomeInterfaceToStub&Stub $service;
13+
14+
protected function setUp(): void
15+
{
16+
$this->service = $this->createStub(SomeInterfaceToStub::class);
17+
}
18+
19+
public function test()
20+
{
21+
$this->service->method('execute')->willReturnMap(['asdf', 'adsfasdf']);
22+
$this->service->execute();
23+
}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\Source;
6+
7+
interface SomeInterfaceToStub
8+
{
9+
public function execute(): string;
10+
}

rules/CodeQuality/Rector/Expression/DecorateWillReturnMapWithExpectsMockRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use PhpParser\Node\Identifier;
1313
use PhpParser\Node\Scalar\Int_;
1414
use PhpParser\Node\Stmt\Expression;
15+
use PHPStan\Type\ObjectType;
1516
use Rector\PHPStan\ScopeFetcher;
17+
use Rector\PHPUnit\Enum\PHPUnitClassName;
1618
use Rector\Rector\AbstractRector;
1719
use Rector\ValueObject\MethodName;
1820
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -108,6 +110,10 @@ public function refactor(Node $node)
108110
return null;
109111
}
110112

113+
if (! $this->isObjectType($topmostCall->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
114+
return null;
115+
}
116+
111117
if ($methodCall->isFirstClassCallable()) {
112118
return null;
113119
}

0 commit comments

Comments
 (0)