-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php80] Skip union callable on ClassPropertyAssignToConstructorPromot…
…ionRector (#1101) * Add failing test fixture for ClassPropertyAssignToConstructorPromotionRector * Closes #1100 * cs fix * Add PropertyAnalyzer * [ci-review] Rector Rectify * rectify * clean up * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify Co-authored-by: zingimmick <zingimmick@outlook.com> Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
a7d8b39
commit 5ca93fd
Showing
5 changed files
with
115 additions
and
12 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...ssignToConstructorPromotionRector/Fixture/skip_nullable_callable_without_typehint.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture; | ||
|
||
final class SkipNullableCallableWithoutTypehint | ||
{ | ||
/** @var callable|null */ | ||
public $cb; | ||
|
||
public function __construct($cb = null) | ||
{ | ||
$this->cb = $cb; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...rtyAssignToConstructorPromotionRector/Fixture/skip_union_typed_with_callable_type.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture; | ||
|
||
final class SkipUnionTypedWithCallableType | ||
{ | ||
/** @var callable|array */ | ||
public $cb; | ||
|
||
public function __construct(callable|array $cb = null) | ||
{ | ||
$this->cb = $cb; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\Type\CallableType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
use Rector\NodeTypeResolver\NodeTypeResolver; | ||
|
||
final class PropertyAnalyzer | ||
{ | ||
public function __construct( | ||
private NodeTypeResolver $nodeTypeResolver | ||
) { | ||
} | ||
|
||
public function hasForbiddenType(Property $property): bool | ||
{ | ||
$propertyType = $this->nodeTypeResolver->getType($property); | ||
if ($propertyType instanceof NullType) { | ||
return true; | ||
} | ||
|
||
if ($this->isCallableType($propertyType)) { | ||
return true; | ||
} | ||
|
||
if (! $propertyType instanceof UnionType) { | ||
return false; | ||
} | ||
|
||
$types = $propertyType->getTypes(); | ||
foreach ($types as $type) { | ||
if ($this->isCallableType($type)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function isCallableType(Type $type): bool | ||
{ | ||
return $type instanceof CallableType; | ||
} | ||
} |