Skip to content

Commit

Permalink
[TypeDeclaration] Skip unitialized property on EmptyOnNullableObjectT…
Browse files Browse the repository at this point in the history
…oInstanceOfRector (#5889)

* [TypeDeclaration] Skip unitialized property on EmptyOnNullableObjectToInstanceOfRector

* fixture update

* fixture update

* fixture update
  • Loading branch information
samsonasik committed May 17, 2024
1 parent 2deccac commit 1986376
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\Source\AnotherObject;

final class SkipUnitializedProperty
{
private AnotherObject $id;

public function setId(AnotherObject $id): self
{
if (!empty($this->id) && !$id->equals($this->id)) {
throw new \InvalidArgumentException('The ID is already set.');
}

$this->id = $id;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

final class AnotherObject
{

public function equals(AnotherObject $anotherObject)
{
return (bool) rand(0, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector\EmptyOnNullableObjectToInstanceOfRectorTest
*/
final class EmptyOnNullableObjectToInstanceOfRector extends AbstractRector
final class EmptyOnNullableObjectToInstanceOfRector extends AbstractScopeAwareRector
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper
Expand Down Expand Up @@ -74,7 +77,7 @@ public function getNodeTypes(): array
/**
* @param Empty_|BooleanNot $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): null|Instanceof_|BooleanNot
{
if ($node instanceof BooleanNot) {
if (! $node->expr instanceof Empty_) {
Expand All @@ -92,7 +95,12 @@ public function refactor(Node $node): ?Node
return null;
}

$exprType = $this->nodeTypeResolver->getNativeType($empty->expr);
$exprType = $scope->getNativeType($empty->expr);
if (! $exprType instanceof UnionType) {
return null;
}

$exprType = TypeCombinator::removeNull($exprType);
if (! $exprType instanceof ObjectType) {
return null;
}
Expand Down

0 comments on commit 1986376

Please sign in to comment.