|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of phpDocumentor. |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + * |
| 8 | + * @link http://phpdoc.org |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace phpDocumentor\Reflection\PseudoTypes; |
| 15 | + |
| 16 | +use phpDocumentor\Reflection\PseudoType; |
| 17 | +use phpDocumentor\Reflection\Type; |
| 18 | +use phpDocumentor\Reflection\Types\Mixed_; |
| 19 | + |
| 20 | +use function sprintf; |
| 21 | + |
| 22 | +/** |
| 23 | + * Value Object representing the conditional type for parameter. |
| 24 | + * |
| 25 | + * @psalm-immutable |
| 26 | + */ |
| 27 | +final class ConditionalForParameter extends Mixed_ implements PseudoType |
| 28 | +{ |
| 29 | + /** @var bool */ |
| 30 | + private $negated; |
| 31 | + /** @var string */ |
| 32 | + private $parameterName; |
| 33 | + /** @var Type */ |
| 34 | + private $targetType; |
| 35 | + /** @var Type */ |
| 36 | + private $if; |
| 37 | + /** @var Type */ |
| 38 | + private $else; |
| 39 | + |
| 40 | + public function __construct(bool $negated, string $parameterName, Type $targetType, Type $if, Type $else) |
| 41 | + { |
| 42 | + $this->negated = $negated; |
| 43 | + $this->parameterName = $parameterName; |
| 44 | + $this->targetType = $targetType; |
| 45 | + $this->if = $if; |
| 46 | + $this->else = $else; |
| 47 | + } |
| 48 | + |
| 49 | + public function isNegated(): bool |
| 50 | + { |
| 51 | + return $this->negated; |
| 52 | + } |
| 53 | + |
| 54 | + public function getParameterName(): string |
| 55 | + { |
| 56 | + return $this->parameterName; |
| 57 | + } |
| 58 | + |
| 59 | + public function getTargetType(): Type |
| 60 | + { |
| 61 | + return $this->targetType; |
| 62 | + } |
| 63 | + |
| 64 | + public function getIf(): Type |
| 65 | + { |
| 66 | + return $this->if; |
| 67 | + } |
| 68 | + |
| 69 | + public function getElse(): Type |
| 70 | + { |
| 71 | + return $this->else; |
| 72 | + } |
| 73 | + |
| 74 | + public function underlyingType(): Type |
| 75 | + { |
| 76 | + return new Mixed_(); |
| 77 | + } |
| 78 | + |
| 79 | + public function __toString(): string |
| 80 | + { |
| 81 | + return sprintf( |
| 82 | + '(%s %s %s ? %s : %s)', |
| 83 | + '$' . $this->parameterName, |
| 84 | + $this->negated ? 'is not' : 'is', |
| 85 | + (string) $this->targetType, |
| 86 | + (string) $this->if, |
| 87 | + (string) $this->else |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
0 commit comments