Skip to content

Commit

Permalink
[TypeDeclaration] Skip on ArrayDimFetch on EmptyOnNullableObjectToIns…
Browse files Browse the repository at this point in the history
…tanceOfRector (#3473)

* [TypeDeclaration] Skip on ArrayDimFetch on EmptyOnNullableObjectToInstanceOfRector

* Fixed 🎉

* final touch: eol
  • Loading branch information
samsonasik committed Mar 11, 2023
1 parent a0a6b55 commit 874f5e1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType;

final class SkipOnArrayDimFetch
{
/**
* @var AnotherType[]
*/
private $data = [];

public function run($key): AnotherType
{
if (! empty($this->data[$key])) {
return $this->data[$key];
}

$this->data[$key] = new AnotherType();
return $this->data[$key];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\TypeDeclaration\Rector\Empty_;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Instanceof_;
Expand Down Expand Up @@ -81,6 +82,10 @@ public function refactor(Node $node): ?Node
$isNegated = false;
}

if ($empty->expr instanceof ArrayDimFetch) {
return null;
}

$exprType = $this->getType($empty->expr);
if (! $exprType instanceof ObjectType) {
return null;
Expand Down

0 comments on commit 874f5e1

Please sign in to comment.