diff --git a/src/Psalm/Type/Atomic/TKeyedArray.php b/src/Psalm/Type/Atomic/TKeyedArray.php index 97e2d01660c..f38eeaae2fd 100644 --- a/src/Psalm/Type/Atomic/TKeyedArray.php +++ b/src/Psalm/Type/Atomic/TKeyedArray.php @@ -455,6 +455,8 @@ public function getMinCount(): int return $prop_min_count; } + /** @var int<1, max>|null */ + private ?int $prop_max_count = null; /** * Returns null if there is no upper limit. * @@ -465,6 +467,9 @@ public function getMaxCount(): ?int if ($this->fallback_params) { return null; } + if ($this->prop_max_count !== null) { + return $this->prop_max_count; + } $prop_max_count = 0; foreach ($this->properties as $property) { if (!$property->isNever()) { @@ -472,7 +477,7 @@ public function getMaxCount(): ?int } } assert($prop_max_count !== 0); - return $prop_max_count; + return $this->prop_max_count = $prop_max_count; } /** * Whether all keys are always defined (ignores unsealedness).