Skip to content

Commit

Permalink
[Php80] Skip callable type different definition on ClassPropertyAssig…
Browse files Browse the repository at this point in the history
…nToConstructorPromotionRector (#3010)
  • Loading branch information
samsonasik committed Oct 24, 2022
1 parent b4c0637 commit f915a1f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class SkipCallableTypeDifferentDefinition
{
/** @var CallbackHandler */
private $fallback;

public function __construct(callable $fallback)
{
$this->fallback = $fallback;
}
}

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

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class SkipCallableTypeDifferentDefinition2
{
/** @var \stdClass */
private $fallback;

public function __construct(callable $fallback)
{
$this->fallback = $fallback;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,25 @@ private function shouldSkipParam(Param $param): bool
$type = $param->type;
}

if ($this->isCallableTypeIdentifier($type)) {
return true;
}

if (! $type instanceof UnionType) {
return false;
}

foreach ($type->types as $type) {
if (! $type instanceof Identifier) {
continue;
}

if (! $this->isName($type, 'callable')) {
continue;
if ($this->isCallableTypeIdentifier($type)) {
return true;
}

return true;
}

return false;
}

private function isCallableTypeIdentifier(?Node $node): bool
{
return $node instanceof Identifier && $this->isName($node, 'callable');
}
}
14 changes: 12 additions & 2 deletions src/NodeAnalyzer/PropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;

final class PropertyAnalyzer
{
Expand All @@ -26,7 +27,7 @@ public function hasForbiddenType(Property $property): bool
return true;
}

if ($this->isCallableType($propertyType)) {
if ($this->isForbiddenType($propertyType)) {
return true;
}

Expand All @@ -36,14 +37,23 @@ public function hasForbiddenType(Property $property): bool

$types = $propertyType->getTypes();
foreach ($types as $type) {
if ($this->isCallableType($type)) {
if ($this->isForbiddenType($type)) {
return true;
}
}

return false;
}

private function isForbiddenType(Type $type): bool
{
if ($type instanceof NonExistingObjectType) {
return true;
}

return $this->isCallableType($type);
}

private function isCallableType(Type $type): bool
{
if ($type instanceof TypeWithClassName) {
Expand Down

0 comments on commit f915a1f

Please sign in to comment.