Skip to content

Commit

Permalink
[TypeDeclaration] Skip nullable callable on TypedPropertyFromAssignsR…
Browse files Browse the repository at this point in the history
…ector (#6308)

* [TypeDeclaration] Skip nullable callable on TypedPropertyFromAssignsRector

* [TypeDeclaration] Skip nullable callable on TypedPropertyFromAssignsRector

* Fix
  • Loading branch information
samsonasik committed Sep 16, 2024
1 parent 5c30f24 commit db5688c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;

final class UnionCallableReturn
{
public function run(callable $a)
{
if (rand(0, 1)) {
return $a;
}

return [];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;

final class UnionCallableReturn
{
public function run(callable $a): callable|array
{
if (rand(0, 1)) {
return $a;
}

return [];
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

final class SkipNullableCallable
{
private $prop;

public function set(callable $prop): void
{
$this->prop = $prop;
}

public function reset(): void
{
$this->prop = null;
}
}
10 changes: 8 additions & 2 deletions src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\CallableType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
$phpParserUnionType = $this->matchPhpParserUnionType($type);
$phpParserUnionType = $this->matchPhpParserUnionType($type, $typeKind);
if ($phpParserUnionType instanceof PhpParserUnionType) {
return $this->resolveUnionTypeNode($phpParserUnionType);
}
Expand Down Expand Up @@ -171,7 +172,7 @@ private function hasObjectAndStaticType(PhpParserUnionType $phpParserUnionType):
/**
* @return Name|FullyQualified|ComplexType|Identifier|null
*/
private function matchPhpParserUnionType(UnionType $unionType): ?Node
private function matchPhpParserUnionType(UnionType $unionType, string $typeKind): ?Node
{
$phpParserUnionedTypes = [];

Expand All @@ -183,6 +184,11 @@ private function matchPhpParserUnionType(UnionType $unionType): ?Node
return null;
}

// special callable type only not allowed on property
if ($typeKind === TypeKind::PROPERTY && $unionedType instanceof CallableType) {
return null;
}

$phpParserUnionedTypes[] = $phpParserNode;
}

Expand Down

0 comments on commit db5688c

Please sign in to comment.