1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\Reflection\Adapter; |
6: | |
7: | use ReflectionNamedType as CoreReflectionNamedType; |
8: | use PHPStan\BetterReflection\Reflection\ReflectionNamedType as BetterReflectionNamedType; |
9: | |
10: | use function strtolower; |
11: | |
12: | |
13: | final class ReflectionNamedType extends CoreReflectionNamedType |
14: | { |
15: | |
16: | |
17: | |
18: | private $betterReflectionType; |
19: | |
20: | |
21: | |
22: | private $allowsNull; |
23: | public function __construct(BetterReflectionNamedType $betterReflectionType, bool $allowsNull) |
24: | { |
25: | $this->betterReflectionType = $betterReflectionType; |
26: | $this->allowsNull = $allowsNull; |
27: | } |
28: | |
29: | |
30: | public function getName(): string |
31: | { |
32: | return $this->betterReflectionType->getName(); |
33: | } |
34: | |
35: | |
36: | public function __toString(): string |
37: | { |
38: | $type = strtolower($this->betterReflectionType->getName()); |
39: | |
40: | if ( |
41: | ! $this->allowsNull |
42: | || $type === 'mixed' |
43: | || $type === 'null' |
44: | ) { |
45: | return $this->betterReflectionType->__toString(); |
46: | } |
47: | |
48: | return '?' . $this->betterReflectionType->__toString(); |
49: | } |
50: | |
51: | public function allowsNull(): bool |
52: | { |
53: | return $this->allowsNull; |
54: | } |
55: | |
56: | public function isBuiltin(): bool |
57: | { |
58: | $type = strtolower($this->betterReflectionType->getName()); |
59: | |
60: | if ($type === 'self' || $type === 'parent' || $type === 'static') { |
61: | return false; |
62: | } |
63: | |
64: | return $this->betterReflectionType->isBuiltin(); |
65: | } |
66: | |
67: | public function isIdentifier(): bool |
68: | { |
69: | return $this->betterReflectionType->isIdentifier(); |
70: | } |
71: | } |
72: | |