| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Analyser; |
| 4: | |
| 5: | use PhpParser\Node\Expr; |
| 6: | use PHPStan\Type\NeverType; |
| 7: | use PHPStan\Type\Type; |
| 8: | use PHPStan\Type\TypeCombinator; |
| 9: | use function array_key_exists; |
| 10: | use function array_merge; |
| 11: | |
| 12: | final class SpecifiedTypes |
| 13: | { |
| 14: | |
| 15: | private bool $overwrite = false; |
| 16: | |
| 17: | |
| 18: | private array $newConditionalExpressionHolders = []; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | private array $conditionalExpressionHolderRecipes = []; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | private array $deferredAugments = []; |
| 36: | |
| 37: | private ?Expr $rootExpr = null; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | private array $alternativeTypes = []; |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function __construct( |
| 58: | private array $sureTypes = [], |
| 59: | private array $sureNotTypes = [], |
| 60: | ) |
| 61: | { |
| 62: | } |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | public function setAlwaysOverwriteTypes(): self |
| 82: | { |
| 83: | $self = clone $this; |
| 84: | $self->overwrite = true; |
| 85: | |
| 86: | return $self; |
| 87: | } |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function setRootExpr(?Expr $rootExpr): self |
| 93: | { |
| 94: | $self = clone $this; |
| 95: | $self->rootExpr = $rootExpr; |
| 96: | |
| 97: | return $self; |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | public function setNewConditionalExpressionHolders(array $newConditionalExpressionHolders): self |
| 104: | { |
| 105: | $self = clone $this; |
| 106: | $self->newConditionalExpressionHolders = $newConditionalExpressionHolders; |
| 107: | |
| 108: | return $self; |
| 109: | } |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | public function setConditionalExpressionHolderRecipes(array $recipes): self |
| 115: | { |
| 116: | $self = clone $this; |
| 117: | $self->conditionalExpressionHolderRecipes = $recipes; |
| 118: | |
| 119: | return $self; |
| 120: | } |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | public function getConditionalExpressionHolderRecipes(): array |
| 126: | { |
| 127: | return $this->conditionalExpressionHolderRecipes; |
| 128: | } |
| 129: | |
| 130: | public function withDeferredAugment(DeferredSpecifiedTypesAugment $augment): self |
| 131: | { |
| 132: | $self = clone $this; |
| 133: | $self->deferredAugments = [...$this->deferredAugments, $augment]; |
| 134: | |
| 135: | return $self; |
| 136: | } |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | public function getDeferredAugments(): array |
| 142: | { |
| 143: | return $this->deferredAugments; |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | public function getSureTypes(): array |
| 151: | { |
| 152: | return $this->sureTypes; |
| 153: | } |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | public function getSureNotTypes(): array |
| 160: | { |
| 161: | return $this->sureNotTypes; |
| 162: | } |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | public function getAlternativeTypes(): array |
| 168: | { |
| 169: | return $this->alternativeTypes; |
| 170: | } |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | public function withoutConditionalExpressionHolders(): self |
| 179: | { |
| 180: | $self = clone $this; |
| 181: | $self->newConditionalExpressionHolders = []; |
| 182: | $self->conditionalExpressionHolderRecipes = []; |
| 183: | |
| 184: | return $self; |
| 185: | } |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | public function withAlternativeTypesOf(self $other): self |
| 193: | { |
| 194: | $self = new self($this->sureTypes, $this->sureNotTypes); |
| 195: | $self->alternativeTypes = $other->alternativeTypes; |
| 196: | $self->overwrite = $this->overwrite; |
| 197: | $self->newConditionalExpressionHolders = $this->newConditionalExpressionHolders; |
| 198: | $self->rootExpr = $this->rootExpr; |
| 199: | |
| 200: | return $self; |
| 201: | } |
| 202: | |
| 203: | public function shouldOverwrite(): bool |
| 204: | { |
| 205: | return $this->overwrite; |
| 206: | } |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | public function getNewConditionalExpressionHolders(): array |
| 212: | { |
| 213: | return $this->newConditionalExpressionHolders; |
| 214: | } |
| 215: | |
| 216: | public function getRootExpr(): ?Expr |
| 217: | { |
| 218: | return $this->rootExpr; |
| 219: | } |
| 220: | |
| 221: | public function removeExpr(string $exprString): self |
| 222: | { |
| 223: | $self = clone $this; |
| 224: | unset($self->sureTypes[$exprString]); |
| 225: | unset($self->sureNotTypes[$exprString]); |
| 226: | unset($self->alternativeTypes[$exprString]); |
| 227: | |
| 228: | return $self; |
| 229: | } |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | public function intersectWith(SpecifiedTypes $other): self |
| 243: | { |
| 244: | $sureTypeUnion = []; |
| 245: | $sureNotTypeUnion = []; |
| 246: | $alternativeUnion = []; |
| 247: | $rootExpr = $this->mergeRootExpr($this->rootExpr, $other->rootExpr); |
| 248: | |
| 249: | $keys = []; |
| 250: | foreach ([$this->sureTypes, $this->sureNotTypes, $this->alternativeTypes, $other->sureTypes, $other->sureNotTypes, $other->alternativeTypes] as $map) { |
| 251: | foreach ($map as $exprString => $entry) { |
| 252: | $keys[$exprString] = $entry[0]; |
| 253: | } |
| 254: | } |
| 255: | |
| 256: | foreach ($keys as $exprString => $exprNode) { |
| 257: | $thisTerms = $this->collectTerms($exprString); |
| 258: | $otherTerms = $other->collectTerms($exprString); |
| 259: | if ($thisTerms === null || $otherTerms === null) { |
| 260: | |
| 261: | continue; |
| 262: | } |
| 263: | |
| 264: | $terms = array_merge($thisTerms, $otherTerms); |
| 265: | $sures = []; |
| 266: | $subtracts = []; |
| 267: | $pureSure = true; |
| 268: | $pureSureNot = true; |
| 269: | foreach ($terms as [$sure, $subtract]) { |
| 270: | if ($sure === null) { |
| 271: | $pureSure = false; |
| 272: | } else { |
| 273: | $sures[] = $sure; |
| 274: | } |
| 275: | if ($subtract === null) { |
| 276: | $pureSureNot = false; |
| 277: | } else { |
| 278: | $subtracts[] = $subtract; |
| 279: | } |
| 280: | if ($sure === null || $subtract === null) { |
| 281: | continue; |
| 282: | } |
| 283: | |
| 284: | $pureSure = false; |
| 285: | $pureSureNot = false; |
| 286: | } |
| 287: | |
| 288: | if ($pureSure) { |
| 289: | $sureTypeUnion[$exprString] = [$exprNode, TypeCombinator::union(...$sures)]; |
| 290: | } elseif ($pureSureNot) { |
| 291: | $merged = TypeCombinator::intersect(...$subtracts); |
| 292: | if ($merged instanceof NeverType) { |
| 293: | |
| 294: | continue; |
| 295: | } |
| 296: | $sureNotTypeUnion[$exprString] = [$exprNode, $merged]; |
| 297: | } else { |
| 298: | $alternativeUnion[$exprString] = [$exprNode, $terms]; |
| 299: | } |
| 300: | } |
| 301: | |
| 302: | $result = new self($sureTypeUnion, $sureNotTypeUnion); |
| 303: | $result->alternativeTypes = $alternativeUnion; |
| 304: | if ($this->overwrite && $other->overwrite) { |
| 305: | $result = $result->setAlwaysOverwriteTypes(); |
| 306: | } |
| 307: | |
| 308: | return $result->setRootExpr($rootExpr); |
| 309: | } |
| 310: | |
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | private function collectTerms(string|int $exprString): ?array |
| 319: | { |
| 320: | if (isset($this->alternativeTypes[$exprString])) { |
| 321: | $terms = $this->alternativeTypes[$exprString][1]; |
| 322: | |
| 323: | |
| 324: | if (isset($this->sureTypes[$exprString]) || isset($this->sureNotTypes[$exprString])) { |
| 325: | $extraSure = $this->sureTypes[$exprString][1] ?? null; |
| 326: | $extraSubtract = $this->sureNotTypes[$exprString][1] ?? null; |
| 327: | $folded = []; |
| 328: | foreach ($terms as [$sure, $subtract]) { |
| 329: | if ($extraSure !== null) { |
| 330: | $sure = $sure === null ? $extraSure : TypeCombinator::intersect($sure, $extraSure); |
| 331: | } |
| 332: | if ($extraSubtract !== null) { |
| 333: | $subtract = $subtract === null ? $extraSubtract : TypeCombinator::union($subtract, $extraSubtract); |
| 334: | } |
| 335: | $folded[] = [$sure, $subtract]; |
| 336: | } |
| 337: | |
| 338: | return $folded; |
| 339: | } |
| 340: | |
| 341: | return $terms; |
| 342: | } |
| 343: | |
| 344: | $sure = $this->sureTypes[$exprString][1] ?? null; |
| 345: | $subtract = $this->sureNotTypes[$exprString][1] ?? null; |
| 346: | if ($sure === null && $subtract === null) { |
| 347: | return null; |
| 348: | } |
| 349: | |
| 350: | return [[$sure, $subtract]]; |
| 351: | } |
| 352: | |
| 353: | |
| 354: | public function unionWith(SpecifiedTypes $other): self |
| 355: | { |
| 356: | $sureTypeUnion = $this->sureTypes + $other->sureTypes; |
| 357: | $sureNotTypeUnion = $this->sureNotTypes + $other->sureNotTypes; |
| 358: | $rootExpr = $this->mergeRootExpr($this->rootExpr, $other->rootExpr); |
| 359: | |
| 360: | foreach ($this->sureTypes as $exprString => [$exprNode, $type]) { |
| 361: | if (!isset($other->sureTypes[$exprString])) { |
| 362: | continue; |
| 363: | } |
| 364: | |
| 365: | $sureTypeUnion[$exprString] = [ |
| 366: | $exprNode, |
| 367: | TypeCombinator::intersect($type, $other->sureTypes[$exprString][1]), |
| 368: | ]; |
| 369: | } |
| 370: | |
| 371: | foreach ($this->sureNotTypes as $exprString => [$exprNode, $type]) { |
| 372: | if (!isset($other->sureNotTypes[$exprString])) { |
| 373: | continue; |
| 374: | } |
| 375: | |
| 376: | $sureNotTypeUnion[$exprString] = [ |
| 377: | $exprNode, |
| 378: | TypeCombinator::union($type, $other->sureNotTypes[$exprString][1]), |
| 379: | ]; |
| 380: | } |
| 381: | |
| 382: | $result = new self($sureTypeUnion, $sureNotTypeUnion); |
| 383: | $result->alternativeTypes = $this->alternativeTypes + $other->alternativeTypes; |
| 384: | if ($this->overwrite || $other->overwrite) { |
| 385: | $result = $result->setAlwaysOverwriteTypes(); |
| 386: | } |
| 387: | |
| 388: | $conditionalExpressionHolders = $this->newConditionalExpressionHolders; |
| 389: | foreach ($other->newConditionalExpressionHolders as $exprString => $holders) { |
| 390: | if (!array_key_exists($exprString, $conditionalExpressionHolders)) { |
| 391: | $conditionalExpressionHolders[$exprString] = $holders; |
| 392: | } else { |
| 393: | $conditionalExpressionHolders[$exprString] = array_merge($conditionalExpressionHolders[$exprString], $holders); |
| 394: | } |
| 395: | } |
| 396: | $result->newConditionalExpressionHolders = $conditionalExpressionHolders; |
| 397: | $result->conditionalExpressionHolderRecipes = array_merge($this->conditionalExpressionHolderRecipes, $other->conditionalExpressionHolderRecipes); |
| 398: | $result->deferredAugments = array_merge($this->deferredAugments, $other->deferredAugments); |
| 399: | |
| 400: | return $result->setRootExpr($rootExpr); |
| 401: | } |
| 402: | |
| 403: | private function mergeRootExpr(?Expr $rootExprA, ?Expr $rootExprB): ?Expr |
| 404: | { |
| 405: | if ($rootExprA === $rootExprB) { |
| 406: | return $rootExprA; |
| 407: | } |
| 408: | |
| 409: | if ($rootExprA === null || $rootExprB === null) { |
| 410: | return $rootExprA ?? $rootExprB; |
| 411: | } |
| 412: | |
| 413: | return null; |
| 414: | } |
| 415: | |
| 416: | } |
| 417: | |