Skip to content

Commit bfe2003

Browse files
committed
ConstantArrayType::getAllArrays() optimizations
1 parent 8bda796 commit bfe2003

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/Type/Constant/ConstantArrayType.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class ConstantArrayType extends ArrayType implements ConstantType
4444
/** @var int[] */
4545
private $optionalKeys;
4646

47+
/** @var self[]|null */
48+
private $allArrays;
49+
4750
/**
4851
* @param array<int, ConstantIntegerType|ConstantStringType> $keyTypes
4952
* @param array<int, Type> $valueTypes
@@ -93,7 +96,19 @@ public function getOptionalKeys(): array
9396
*/
9497
public function getAllArrays(): array
9598
{
96-
$optionalKeysCombination = $this->powerSet($this->optionalKeys);
99+
if ($this->allArrays !== null) {
100+
return $this->allArrays;
101+
}
102+
103+
if (count($this->optionalKeys) <= 10) {
104+
$optionalKeysCombinations = $this->powerSet($this->optionalKeys);
105+
} else {
106+
$optionalKeysCombinations = [
107+
[],
108+
$this->optionalKeys,
109+
];
110+
}
111+
97112
$requiredKeys = [];
98113
foreach (array_keys($this->keyTypes) as $i) {
99114
if (in_array($i, $this->optionalKeys, true)) {
@@ -103,7 +118,7 @@ public function getAllArrays(): array
103118
}
104119

105120
$arrays = [];
106-
foreach ($optionalKeysCombination as $combination) {
121+
foreach ($optionalKeysCombinations as $combination) {
107122
$keys = array_merge($requiredKeys, $combination);
108123
$keyTypes = [];
109124
$valueTypes = [];
@@ -115,7 +130,7 @@ public function getAllArrays(): array
115130
$arrays[] = new self($keyTypes, $valueTypes, 0 /*TODO*/, []);
116131
}
117132

118-
return $arrays;
133+
return $this->allArrays = $arrays;
119134
}
120135

121136
/**

0 commit comments

Comments
 (0)