Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Php80] Add typed property Closure support on ClassPropertyAssignToConstructorPromotionRector #3453

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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