|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\PhpDocParser\Ast\Type; |
| 4 | + |
| 5 | +use PHPStan\PhpDocParser\Ast\Node; |
| 6 | +use PHPStan\PhpDocParser\Ast\NodeAttributes; |
| 7 | +use function array_map; |
| 8 | +use function implode; |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents a property access expression in conditional types. |
| 12 | + */ |
| 13 | +class PropertyAccessNode implements Node |
| 14 | +{ |
| 15 | + |
| 16 | + use NodeAttributes; |
| 17 | + |
| 18 | + public const HOLDER_SELF = 'self'; |
| 19 | + public const HOLDER_PARENT = 'parent'; |
| 20 | + public const HOLDER_STATIC = 'static'; |
| 21 | + |
| 22 | + public bool $isStatic; |
| 23 | + |
| 24 | + /** |
| 25 | + * For static access: 'self', 'parent', or 'static' |
| 26 | + * For instance access: null (holder is implicitly $this) |
| 27 | + * |
| 28 | + * @var self::HOLDER_*|null |
| 29 | + */ |
| 30 | + public ?string $holder; |
| 31 | + |
| 32 | + /** @var list<PropertyAccessPathItem> */ |
| 33 | + public array $path; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param self::HOLDER_*|null $holder |
| 37 | + * @param list<PropertyAccessPathItem> $path |
| 38 | + */ |
| 39 | + public function __construct(bool $isStatic, ?string $holder, array $path) |
| 40 | + { |
| 41 | + $this->isStatic = $isStatic; |
| 42 | + $this->holder = $holder; |
| 43 | + $this->path = $path; |
| 44 | + } |
| 45 | + |
| 46 | + public function __toString(): string |
| 47 | + { |
| 48 | + if ($this->isStatic) { |
| 49 | + return $this->holder . '::$' . $this->path[0]->name; |
| 50 | + } |
| 51 | + |
| 52 | + $pathString = implode('->', array_map( |
| 53 | + static fn (PropertyAccessPathItem $item): string => $item->name, |
| 54 | + $this->path, |
| 55 | + )); |
| 56 | + |
| 57 | + return '$this->' . $pathString; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param array<string, mixed> $properties |
| 62 | + */ |
| 63 | + public static function __set_state(array $properties): self |
| 64 | + { |
| 65 | + $instance = new self( |
| 66 | + $properties['isStatic'], |
| 67 | + $properties['holder'], |
| 68 | + $properties['path'], |
| 69 | + ); |
| 70 | + if (isset($properties['attributes'])) { |
| 71 | + foreach ($properties['attributes'] as $key => $value) { |
| 72 | + $instance->setAttribute($key, $value); |
| 73 | + } |
| 74 | + } |
| 75 | + return $instance; |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments