| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type; |
| 4: | |
| 5: | use Closure; |
| 6: | use PHPStan\ShouldNotHappenException; |
| 7: | use PHPStan\TrinaryLogic; |
| 8: | use function array_map; |
| 9: | use function array_merge; |
| 10: | use function array_unique; |
| 11: | use function array_values; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | final class IsSuperTypeOfResult |
| 38: | { |
| 39: | |
| 40: | private static self $YES; |
| 41: | |
| 42: | private static self $MAYBE; |
| 43: | |
| 44: | private static self $NO; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | public function __construct( |
| 52: | public readonly TrinaryLogic $result, |
| 53: | public readonly array $reasons, |
| 54: | public readonly array $lazyReasons = [], |
| 55: | ) |
| 56: | { |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | public function yes(): bool |
| 64: | { |
| 65: | return $this->result->yes(); |
| 66: | } |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function maybe(): bool |
| 73: | { |
| 74: | return $this->result->maybe(); |
| 75: | } |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | public function no(): bool |
| 82: | { |
| 83: | return $this->result->no(); |
| 84: | } |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function getReasons(): array |
| 93: | { |
| 94: | if ($this->lazyReasons === []) { |
| 95: | return $this->reasons; |
| 96: | } |
| 97: | |
| 98: | return array_values(array_unique(array_merge( |
| 99: | $this->reasons, |
| 100: | array_map(static fn (Closure $cb): string => $cb(), $this->lazyReasons), |
| 101: | ))); |
| 102: | } |
| 103: | |
| 104: | public static function createYes(): self |
| 105: | { |
| 106: | return self::$YES ??= new self(TrinaryLogic::createYes(), []); |
| 107: | } |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | public static function createNo(array $reasons = [], array $lazyReasons = []): self |
| 114: | { |
| 115: | if ($reasons === [] && $lazyReasons === []) { |
| 116: | return self::$NO ??= new self(TrinaryLogic::createNo(), $reasons); |
| 117: | } |
| 118: | return new self(TrinaryLogic::createNo(), $reasons, $lazyReasons); |
| 119: | } |
| 120: | |
| 121: | public static function createMaybe(): self |
| 122: | { |
| 123: | return self::$MAYBE ??= new self(TrinaryLogic::createMaybe(), []); |
| 124: | } |
| 125: | |
| 126: | public static function createFromBoolean(bool $value): self |
| 127: | { |
| 128: | if ($value === true) { |
| 129: | return self::createYes(); |
| 130: | } |
| 131: | return self::createNo(); |
| 132: | } |
| 133: | |
| 134: | public function toAcceptsResult(): AcceptsResult |
| 135: | { |
| 136: | return new AcceptsResult($this->result, $this->getReasons()); |
| 137: | } |
| 138: | |
| 139: | public function and(self ...$others): self |
| 140: | { |
| 141: | $results = []; |
| 142: | $reasons = []; |
| 143: | $lazyReasons = []; |
| 144: | foreach ($others as $other) { |
| 145: | $results[] = $other->result; |
| 146: | $reasons[] = $other->reasons; |
| 147: | $lazyReasons[] = $other->lazyReasons; |
| 148: | } |
| 149: | |
| 150: | return new self( |
| 151: | $this->result->and(...$results), |
| 152: | array_values(array_unique(array_merge($this->reasons, ...$reasons))), |
| 153: | array_merge($this->lazyReasons, ...$lazyReasons), |
| 154: | ); |
| 155: | } |
| 156: | |
| 157: | public function or(self ...$others): self |
| 158: | { |
| 159: | $results = []; |
| 160: | $reasons = []; |
| 161: | $lazyReasons = []; |
| 162: | foreach ($others as $other) { |
| 163: | $results[] = $other->result; |
| 164: | $reasons[] = $other->reasons; |
| 165: | $lazyReasons[] = $other->lazyReasons; |
| 166: | } |
| 167: | |
| 168: | return new self( |
| 169: | $this->result->or(...$results), |
| 170: | array_values(array_unique(array_merge($this->reasons, ...$reasons))), |
| 171: | array_merge($this->lazyReasons, ...$lazyReasons), |
| 172: | ); |
| 173: | } |
| 174: | |
| 175: | |
| 176: | public function decorateReasons(callable $cb): self |
| 177: | { |
| 178: | $reasons = []; |
| 179: | foreach ($this->reasons as $reason) { |
| 180: | $reasons[] = $cb($reason); |
| 181: | } |
| 182: | |
| 183: | $lazyReasons = []; |
| 184: | foreach ($this->lazyReasons as $lazyReason) { |
| 185: | $lazyReasons[] = static fn (): string => $cb($lazyReason()); |
| 186: | } |
| 187: | |
| 188: | return new self($this->result, $reasons, $lazyReasons); |
| 189: | } |
| 190: | |
| 191: | |
| 192: | public static function extremeIdentity(self ...$operands): self |
| 193: | { |
| 194: | if ($operands === []) { |
| 195: | throw new ShouldNotHappenException(); |
| 196: | } |
| 197: | |
| 198: | $results = []; |
| 199: | $reasons = []; |
| 200: | $lazyReasons = []; |
| 201: | foreach ($operands as $operand) { |
| 202: | $results[] = $operand->result; |
| 203: | foreach ($operand->reasons as $reason) { |
| 204: | $reasons[] = $reason; |
| 205: | } |
| 206: | foreach ($operand->lazyReasons as $lazyReason) { |
| 207: | $lazyReasons[] = $lazyReason; |
| 208: | } |
| 209: | } |
| 210: | |
| 211: | return new self(TrinaryLogic::extremeIdentity(...$results), array_values(array_unique($reasons)), $lazyReasons); |
| 212: | } |
| 213: | |
| 214: | |
| 215: | public static function maxMin(self ...$operands): self |
| 216: | { |
| 217: | if ($operands === []) { |
| 218: | throw new ShouldNotHappenException(); |
| 219: | } |
| 220: | |
| 221: | $results = []; |
| 222: | $reasons = []; |
| 223: | $lazyReasons = []; |
| 224: | foreach ($operands as $operand) { |
| 225: | $results[] = $operand->result; |
| 226: | foreach ($operand->reasons as $reason) { |
| 227: | $reasons[] = $reason; |
| 228: | } |
| 229: | foreach ($operand->lazyReasons as $lazyReason) { |
| 230: | $lazyReasons[] = $lazyReason; |
| 231: | } |
| 232: | } |
| 233: | |
| 234: | return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)), $lazyReasons); |
| 235: | } |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | public static function lazyMaxMin( |
| 243: | array $objects, |
| 244: | callable $callback, |
| 245: | ): self |
| 246: | { |
| 247: | $reasons = []; |
| 248: | $lazyReasons = []; |
| 249: | $hasNo = false; |
| 250: | foreach ($objects as $object) { |
| 251: | $isSuperTypeOf = $callback($object); |
| 252: | if ($isSuperTypeOf->result->yes()) { |
| 253: | return $isSuperTypeOf; |
| 254: | } elseif ($isSuperTypeOf->result->no()) { |
| 255: | $hasNo = true; |
| 256: | } |
| 257: | |
| 258: | foreach ($isSuperTypeOf->reasons as $reason) { |
| 259: | $reasons[] = $reason; |
| 260: | } |
| 261: | foreach ($isSuperTypeOf->lazyReasons as $lazyReason) { |
| 262: | $lazyReasons[] = $lazyReason; |
| 263: | } |
| 264: | } |
| 265: | |
| 266: | return new self( |
| 267: | $hasNo ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe(), |
| 268: | array_values(array_unique($reasons)), |
| 269: | $lazyReasons, |
| 270: | ); |
| 271: | } |
| 272: | |
| 273: | public function negate(): self |
| 274: | { |
| 275: | return new self($this->result->negate(), $this->reasons, $this->lazyReasons); |
| 276: | } |
| 277: | |
| 278: | public function describe(): string |
| 279: | { |
| 280: | return $this->result->describe(); |
| 281: | } |
| 282: | |
| 283: | } |
| 284: | |