|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link http://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\Reflection\PseudoTypes; |
| 15 | + |
| 16 | +use phpDocumentor\Reflection\PseudoType; |
| 17 | +use phpDocumentor\Reflection\Type; |
| 18 | +use phpDocumentor\Reflection\Types\Callable_; |
| 19 | +use phpDocumentor\Reflection\Types\Mixed_; |
| 20 | +use phpDocumentor\Reflection\Types\Nullable; |
| 21 | + |
| 22 | +/** @psalm-immutable */ |
| 23 | +final class OffsetAccess implements PseudoType |
| 24 | +{ |
| 25 | + /** @var Type */ |
| 26 | + private $valueType; |
| 27 | + |
| 28 | + /** @var Type */ |
| 29 | + private $offsetType; |
| 30 | + |
| 31 | + public function __construct(Type $valueType, Type $offsetType) |
| 32 | + { |
| 33 | + $this->valueType = $valueType; |
| 34 | + $this->offsetType = $offsetType; |
| 35 | + } |
| 36 | + |
| 37 | + public function getValueType(): Type |
| 38 | + { |
| 39 | + return $this->valueType; |
| 40 | + } |
| 41 | + |
| 42 | + public function getOffsetType(): Type |
| 43 | + { |
| 44 | + return $this->offsetType; |
| 45 | + } |
| 46 | + |
| 47 | + public function underlyingType(): Type |
| 48 | + { |
| 49 | + return new Mixed_(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
| 54 | + */ |
| 55 | + public function __toString(): string |
| 56 | + { |
| 57 | + if ( |
| 58 | + $this->valueType instanceof Callable_ |
| 59 | + || $this->valueType instanceof Nullable |
| 60 | + ) { |
| 61 | + return '(' . $this->valueType . ')[' . $this->offsetType . ']'; |
| 62 | + } |
| 63 | + |
| 64 | + return $this->valueType . '[' . $this->offsetType . ']'; |
| 65 | + } |
| 66 | +} |
0 commit comments