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: | public function getName(): string |
30: | { |
31: | return $this->betterReflectionType->getName(); |
32: | } |
33: | |
34: | |
35: | public function __toString(): string |
36: | { |
37: | $type = strtolower($this->betterReflectionType->getName()); |
38: | |
39: | if ( |
40: | ! $this->allowsNull |
41: | || $type === 'mixed' |
42: | || $type === 'null' |
43: | ) { |
44: | return $this->betterReflectionType->__toString(); |
45: | } |
46: | |
47: | return '?' . $this->betterReflectionType->__toString(); |
48: | } |
49: | |
50: | public function allowsNull(): bool |
51: | { |
52: | return $this->allowsNull; |
53: | } |
54: | |
55: | public function isBuiltin(): bool |
56: | { |
57: | $type = strtolower($this->betterReflectionType->getName()); |
58: | |
59: | if ($type === 'self' || $type === 'parent' || $type === 'static') { |
60: | return false; |
61: | } |
62: | |
63: | return $this->betterReflectionType->isBuiltin(); |
64: | } |
65: | } |
66: | |