Skip to content

Commit

Permalink
[Php80] Add typed property Closure support on ClassPropertyAssignToCo…
Browse files Browse the repository at this point in the history
…nstructorPromotionRector (#3453)
  • Loading branch information
samsonasik committed Mar 4, 2023
1 parent 64a96e3 commit ed16cab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

final class ClosureType
{
private readonly \Closure $configureContainerBuilder;

public function __construct(\Closure $configureContainerBuilder)
{
$this->configureContainerBuilder = $configureContainerBuilder;
}
}

?>
-----
<?php

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

final class ClosureType
{
public function __construct(private readonly \Closure $configureContainerBuilder)
{
}
}

?>
15 changes: 8 additions & 7 deletions src/NodeAnalyzer/PropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Core\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\CallableType;
use PHPStan\Type\NullType;
Expand All @@ -27,7 +28,7 @@ public function hasForbiddenType(Property $property): bool
return true;
}

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

Expand All @@ -37,27 +38,27 @@ public function hasForbiddenType(Property $property): bool

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

return false;
}

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

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

private function isCallableType(Type $type): bool
private function isCallableType(Property $property, Type $type): bool
{
if ($type instanceof TypeWithClassName) {
return $type->getClassName() === 'Closure';
if ($type instanceof TypeWithClassName && $type->getClassName() === 'Closure') {
return ! $property->type instanceof Node;
}

return $type instanceof CallableType;
Expand Down

0 comments on commit ed16cab

Please sign in to comment.