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

[TypeDeclaration] Skip nullable callable on TypedPropertyFromAssignsRector #6308

Merged
merged 3 commits into from
Sep 16, 2024
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,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