-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ion-types-are-turned-into-nullable-reflection-named-types Ensure that simple union types like `A|null` yield a nullable `ReflectionNamedType`
- Loading branch information
Showing
26 changed files
with
305 additions
and
107 deletions.
There are no files selected for viewing
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
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
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
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BetterReflection\Reflection\StringCast; | ||
|
||
use Roave\BetterReflection\Reflection\ReflectionIntersectionType; | ||
use Roave\BetterReflection\Reflection\ReflectionNamedType; | ||
use Roave\BetterReflection\Reflection\ReflectionUnionType; | ||
|
||
use function array_filter; | ||
use function array_values; | ||
use function count; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ReflectionTypeStringCast | ||
{ | ||
public static function toString( | ||
ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType $type, | ||
): string { | ||
if ($type instanceof ReflectionUnionType) { | ||
// php-src has this weird behavior where a union type composed of a single type `T` | ||
// together with `null` means that a `ReflectionNamedType` for `?T` is produced, | ||
// rather than `T|null`. This is done to keep BC compatibility with PHP 7.1 (which | ||
// introduced nullable types), but at reflection level, this is mostly a nuisance. | ||
// In order to keep parity with core `Reflector#__toString()` behavior, we stashed | ||
// this weird behavior in here. | ||
$nonNullTypes = array_values(array_filter( | ||
$type->getTypes(), | ||
static fn (ReflectionNamedType $type): bool => $type->getName() !== 'null', | ||
)); | ||
|
||
if ($type->allowsNull() && count($nonNullTypes) === 1) { | ||
return '?' . $nonNullTypes[0]->__toString(); | ||
} | ||
} | ||
|
||
return $type->__toString(); | ||
} | ||
} |
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
Oops, something went wrong.