| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Reflection; |
| 4: | |
| 5: | use function array_key_exists; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | final class PassedByReference |
| 11: | { |
| 12: | |
| 13: | private const NO = 1; |
| 14: | private const READS_ARGUMENT = 2; |
| 15: | private const CREATES_NEW_VARIABLE = 3; |
| 16: | |
| 17: | |
| 18: | private static array $registry = []; |
| 19: | |
| 20: | private function __construct(private int $value) |
| 21: | { |
| 22: | } |
| 23: | |
| 24: | private static function create(int $value): self |
| 25: | { |
| 26: | if (!array_key_exists($value, self::$registry)) { |
| 27: | self::$registry[$value] = new self($value); |
| 28: | } |
| 29: | |
| 30: | return self::$registry[$value]; |
| 31: | } |
| 32: | |
| 33: | public static function createNo(): self |
| 34: | { |
| 35: | return self::create(self::NO); |
| 36: | } |
| 37: | |
| 38: | public static function createCreatesNewVariable(): self |
| 39: | { |
| 40: | return self::create(self::CREATES_NEW_VARIABLE); |
| 41: | } |
| 42: | |
| 43: | public static function createReadsArgument(): self |
| 44: | { |
| 45: | return self::create(self::READS_ARGUMENT); |
| 46: | } |
| 47: | |
| 48: | public function no(): bool |
| 49: | { |
| 50: | return $this->value === self::NO; |
| 51: | } |
| 52: | |
| 53: | public function yes(): bool |
| 54: | { |
| 55: | return !$this->no(); |
| 56: | } |
| 57: | |
| 58: | public function equals(self $other): bool |
| 59: | { |
| 60: | return $this->value === $other->value; |
| 61: | } |
| 62: | |
| 63: | public function createsNewVariable(): bool |
| 64: | { |
| 65: | return $this->value === self::CREATES_NEW_VARIABLE; |
| 66: | } |
| 67: | |
| 68: | public function combine(self $other): self |
| 69: | { |
| 70: | if ($this->value > $other->value) { |
| 71: | return $this; |
| 72: | } elseif ($this->value < $other->value) { |
| 73: | return $other; |
| 74: | } |
| 75: | |
| 76: | return $this; |
| 77: | } |
| 78: | |
| 79: | } |
| 80: | |