Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c92e7c9

Browse files
committed
[typed] add mock object property type
1 parent 5111920 commit c92e7c9

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

packages/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Name;
9+
use PhpParser\Node\Name\FullyQualified;
910
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1011
use PHPStan\Type\Generic\GenericClassStringType;
1112
use PHPStan\Type\IntersectionType;
13+
use PHPStan\Type\ObjectType;
1214
use PHPStan\Type\Type;
1315
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode;
1416
use Rector\Core\Exception\ShouldNotHappenException;
@@ -78,7 +80,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
7880
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
7981
{
8082
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::INTERSECTION_TYPES)) {
81-
return null;
83+
return $this->matchMockObjectType($type);
8284
}
8385

8486
$intersectionedTypeNodes = [];
@@ -93,7 +95,8 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
9395
$resolvedTypeName = (string) $resolvedType;
9496
}
9597

96-
if (in_array($resolvedTypeName, [self::STRING, 'object'], true)) {
98+
if (in_array($resolvedTypeName, [self::STRING, '
99+
}object'], true)) {
97100
return $resolvedType;
98101
}
99102

@@ -102,4 +105,20 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
102105

103106
return new Node\IntersectionType($intersectionedTypeNodes);
104107
}
108+
109+
private function matchMockObjectType(IntersectionType $intersectionType): ?FullyQualified
110+
{
111+
// return mock object as the strict one
112+
foreach ($intersectionType->getTypes() as $intersectionedType) {
113+
if (! $intersectionedType instanceof ObjectType) {
114+
continue;
115+
}
116+
117+
if ($intersectionedType->getClassName() === 'PHPUnit\Framework\MockObject\MockObject') {
118+
return new FullyQualified($intersectionedType->getClassName());
119+
}
120+
}
121+
122+
return null;
123+
}
105124
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MockPropertyType extends TestCase
8+
{
9+
private $someValue;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someValue = $this->createMock('SomeClass');
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class MockPropertyType extends TestCase
26+
{
27+
private \PHPUnit\Framework\MockObject\MockObject $someValue;
28+
29+
protected function setUp(): void
30+
{
31+
$this->someValue = $this->createMock('SomeClass');
32+
}
33+
}
34+
35+
?>

0 commit comments

Comments
 (0)