Skip to content

Commit 79663a1

Browse files
committed
[type-declaration] Skip intersection type on TypedPropertyFromCreateMockAssignRector
1 parent ba8614a commit 79663a1

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Fixture;
4+
5+
use PHPUnit\Framework\MockObject\MockObject;
6+
use PHPUnit\Framework\TestCase;
7+
use Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Source\SomeMockedClass;
8+
9+
final class SkipIntersectionType extends TestCase
10+
{
11+
public SomeMockedClass&MockObject $someMock;
12+
13+
protected function setUp(): void
14+
{
15+
$this->someMock = $this->createMock(SomeMockedClass::class);
16+
}
17+
}

rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node
124124
$dateExpr = $this->getArgValue($node, 0);
125125
$baseTimestamp = $this->getArgValue($node, 1);
126126

127-
if ($dateExpr instanceof Expr && !$baseTimestamp instanceof Expr) {
127+
if ($dateExpr instanceof Expr && ! $baseTimestamp instanceof Expr) {
128128
return $this->createCarbonParseTimestamp($dateExpr);
129129
}
130130

rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace Rector\TypeDeclaration\Rector\Class_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\IntersectionType;
89
use PhpParser\Node\NullableType;
910
use PhpParser\Node\Stmt\Class_;
11+
use PhpParser\Node\Stmt\Property;
1012
use PHPStan\Type\ObjectType;
1113
use PHPStan\Type\Type;
1214
use Rector\Enum\ClassName;
@@ -34,7 +36,9 @@ public function __construct(
3436

3537
public function getRuleDefinition(): RuleDefinition
3638
{
37-
return new RuleDefinition('Add typed property from assigned mock', [
39+
return new RuleDefinition(
40+
'Add "PHPUnit\Framework\MockObject\MockObject" typed property from assigned mock to clearly separate from real objects',
41+
[
3842
new CodeSample(
3943
<<<'CODE_SAMPLE'
4044
use PHPUnit\Framework\TestCase;
@@ -52,10 +56,11 @@ protected function setUp(): void
5256
,
5357
<<<'CODE_SAMPLE'
5458
use PHPUnit\Framework\TestCase;
59+
use PHPUnit\Framework\MockObject\MockObject;
5560
5661
final class SomeTest extends TestCase
5762
{
58-
private \PHPUnit\Framework\MockObject\MockObject $someProperty;
63+
private MockObject $someProperty;
5964
6065
protected function setUp(): void
6166
{
@@ -64,6 +69,7 @@ protected function setUp(): void
6469
}
6570
CODE_SAMPLE
6671
),
72+
6773
]);
6874
}
6975

@@ -84,15 +90,12 @@ public function refactor(Node $node): ?Node
8490
$hasChanged = false;
8591

8692
foreach ($node->getProperties() as $property) {
87-
// already use PHPUnit\Framework\MockObject\MockObject type
88-
if ($property->type instanceof Node && $this->isObjectType(
89-
$property->type,
90-
new ObjectType(ClassName::MOCK_OBJECT)
91-
)) {
93+
if (count($property->props) !== 1) {
9294
continue;
9395
}
9496

95-
if (count($property->props) !== 1) {
97+
// already use PHPUnit\Framework\MockObject\MockObject type
98+
if ($this->isAlreadyTypedWithMockObject($property)) {
9699
continue;
97100
}
98101

@@ -135,4 +138,18 @@ public function provideMinPhpVersion(): int
135138
{
136139
return PhpVersionFeature::TYPED_PROPERTIES;
137140
}
141+
142+
private function isAlreadyTypedWithMockObject(Property $property): bool
143+
{
144+
if (! $property->type instanceof Node) {
145+
return false;
146+
}
147+
148+
// complex type, used on purpose
149+
if ($property->type instanceof IntersectionType) {
150+
return true;
151+
}
152+
153+
return $this->isObjectType($property->type, new ObjectType(ClassName::MOCK_OBJECT));
154+
}
138155
}

0 commit comments

Comments
 (0)