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: /** @psalm-suppress MissingImmutableAnnotation */
13: final class ReflectionNamedType extends CoreReflectionNamedType
14: {
15: /**
16: * @var BetterReflectionNamedType
17: */
18: private $betterReflectionType;
19: /**
20: * @var bool
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: public function __toString(): string
35: {
36: $type = strtolower($this->betterReflectionType->getName());
37:
38: if (
39: ! $this->allowsNull
40: || $type === 'mixed'
41: || $type === 'null'
42: ) {
43: return $this->betterReflectionType->__toString();
44: }
45:
46: return '?' . $this->betterReflectionType->__toString();
47: }
48:
49: public function allowsNull(): bool
50: {
51: return $this->allowsNull;
52: }
53:
54: public function isBuiltin(): bool
55: {
56: $type = strtolower($this->betterReflectionType->getName());
57:
58: if ($type === 'self' || $type === 'parent' || $type === 'static') {
59: return false;
60: }
61:
62: return $this->betterReflectionType->isBuiltin();
63: }
64: }
65: