Skip to content

Commit bf80d32

Browse files
authored
[Php74] Skip ObjectProphecy type on TypedPropertyRector (#1358)
1 parent 9e81001 commit bf80d32

4 files changed

Lines changed: 92 additions & 3 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureClassLikeTypeOnly;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Prophecy\Prophecy\ObjectProphecy;
7+
use Prophecy\PhpUnit\ProphecyTrait;
8+
use Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\ReturnString;
9+
10+
class ReturnStringTest extends TestCase
11+
{
12+
use ProphecyTrait;
13+
14+
/**
15+
* @var ReturnString|ObjectProphecy
16+
*/
17+
private $obj;
18+
19+
protected function setUp(): void
20+
{
21+
$this->obj = $this->prophesize(ReturnString::class);
22+
}
23+
24+
public function testReturnName()
25+
{
26+
$this->obj->getName()->willReturn('name')->shouldBeCalled();
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureClassLikeTypeOnly;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Prophecy\Prophecy\ObjectProphecy;
7+
use Prophecy\PhpUnit\ProphecyTrait;
8+
use Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\ReturnString;
9+
10+
class ReturnString2Test extends TestCase
11+
{
12+
use ProphecyTrait;
13+
14+
/**
15+
* @var ReturnString|ObjectProphecy
16+
*/
17+
private $obj = null;
18+
19+
protected function setUp(): void
20+
{
21+
$this->obj = $this->prophesize(ReturnString::class);
22+
}
23+
24+
public function testReturnName()
25+
{
26+
$this->obj->getName()->willReturn('name')->shouldBeCalled();
27+
}
28+
}

rules/Php74/Rector/Property/TypedPropertyRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
2828
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
2929
use Rector\NodeTypeResolver\Node\AttributeKey;
30+
use Rector\Php74\TypeAnalyzer\ObjectTypeAnalyzer;
3031
use Rector\Php74\TypeAnalyzer\PropertyUnionTypeResolver;
3132
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
3233
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
3334
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
34-
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;
3535
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
3636
use Rector\VendorLocker\VendorLockResolver;
3737
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -81,6 +81,7 @@ public function __construct(
8181
private PropertyAnalyzer $propertyAnalyzer,
8282
private PropertyUnionTypeResolver $propertyUnionTypeResolver,
8383
private AstResolver $astResolver,
84+
private ObjectTypeAnalyzer $objectTypeAnalyzer
8485
) {
8586
}
8687

@@ -153,8 +154,7 @@ public function refactor(Node $node): ?Node
153154
}
154155
}
155156

156-
// we are not sure what object type this is
157-
if ($varType instanceof NonExistingObjectType) {
157+
if ($this->objectTypeAnalyzer->isSpecial($varType)) {
158158
return null;
159159
}
160160

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Php74\TypeAnalyzer;
6+
7+
use PHPStan\Type\Type;
8+
use PHPStan\Type\UnionType;
9+
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
10+
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;
11+
12+
final class ObjectTypeAnalyzer
13+
{
14+
public function isSpecial(Type $varType): bool
15+
{
16+
// we are not sure what object type this is
17+
if ($varType instanceof NonExistingObjectType) {
18+
return true;
19+
}
20+
21+
$types = ! $varType instanceof UnionType
22+
? [$varType]
23+
: $varType->getTypes();
24+
25+
foreach ($types as $type) {
26+
if ($type instanceof FullyQualifiedObjectType && $type->getClassName() === 'Prophecy\Prophecy\ObjectProphecy') {
27+
return true;
28+
}
29+
}
30+
31+
return false;
32+
}
33+
}

0 commit comments

Comments
 (0)