Skip to content

Commit

Permalink
[Php74] Skip array type on trait on RestoreDefaultNullToNullableTypeP…
Browse files Browse the repository at this point in the history
…ropertyRector (#2094)

* [Php74] Skip array type on trait on RestoreDefaultNullToNullableTypePropertyRector

* Fixed 🎉
  • Loading branch information
samsonasik committed Apr 18, 2022
1 parent 961f67c commit 8e51a6c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
9 changes: 9 additions & 0 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\ClassAutoloadingException;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -116,6 +117,10 @@ public function isObjectType(Node $node, ObjectType $requiredObjectType): bool

public function getType(Node $node): Type
{
if ($node instanceof Property && $node->type instanceof NullableType) {
return $this->getType($node->type);
}

if ($node instanceof NullableType) {
if ($node->type instanceof Name && $node->type->hasAttribute(AttributeKey::NAMESPACED_NAME)) {
$node->type = new FullyQualified($node->type->getAttribute(AttributeKey::NAMESPACED_NAME));
Expand Down Expand Up @@ -167,6 +172,10 @@ public function getType(Node $node): Type
}
}

if ($node instanceof Identifier) {
return $this->identifierTypeResolver->resolve($node);
}

return new MixedType();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector\Fixture;

trait SkipArrayTypeOnTrait
{
/** @var stdClass[] */
private array $targets;

public function withTargets(stdClass ...$targets): self
{
$this->targets = $targets;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ private function shouldSkip(Property $property): bool
return true;
}

if (! $this->nodeTypeResolver->isNullableType($property)) {
return true;
}

// is variable assigned in constructor
$propertyName = $this->getName($property);
$classLike = $this->betterNodeFinder->findParentType($property, Class_::class);
Expand All @@ -104,10 +108,6 @@ private function shouldSkip(Property $property): bool
return false;
}

if (! $this->nodeTypeResolver->isNullableType($property)) {
return true;
}

return $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName);
}
}

0 comments on commit 8e51a6c

Please sign in to comment.