Skip to content

Commit d4dad9f

Browse files
committed
Faster AcceptsResult, IsSuperTypeOfResult
1 parent ae83400 commit d4dad9f

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

src/Type/AcceptsResult.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPStan\ShouldNotHappenException;
66
use PHPStan\TrinaryLogic;
7-
use function array_map;
87
use function array_merge;
98
use function array_unique;
109
use function array_values;
@@ -122,15 +121,16 @@ public static function extremeIdentity(self ...$operands): self
122121
throw new ShouldNotHappenException();
123122
}
124123

125-
$result = TrinaryLogic::extremeIdentity(...array_map(static fn (self $result) => $result->result, $operands));
124+
$results = [];
126125
$reasons = [];
127126
foreach ($operands as $operand) {
127+
$results[] = $operand->result;
128128
foreach ($operand->reasons as $reason) {
129129
$reasons[] = $reason;
130130
}
131131
}
132132

133-
return new self($result, array_values(array_unique($reasons)));
133+
return new self(TrinaryLogic::extremeIdentity(...$results), array_values(array_unique($reasons)));
134134
}
135135

136136
/** @see TrinaryLogic::maxMin() */
@@ -140,15 +140,16 @@ public static function maxMin(self ...$operands): self
140140
throw new ShouldNotHappenException();
141141
}
142142

143-
$result = TrinaryLogic::maxMin(...array_map(static fn (self $result) => $result->result, $operands));
143+
$results = [];
144144
$reasons = [];
145145
foreach ($operands as $operand) {
146+
$results[] = $operand->result;
146147
foreach ($operand->reasons as $reason) {
147148
$reasons[] = $reason;
148149
}
149150
}
150151

151-
return new self($result, array_values(array_unique($reasons)));
152+
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
152153
}
153154

154155
}

src/Type/IsSuperTypeOfResult.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPStan\ShouldNotHappenException;
66
use PHPStan\TrinaryLogic;
7-
use function array_map;
87
use function array_merge;
98
use function array_unique;
109
use function array_values;
@@ -155,9 +154,16 @@ public static function extremeIdentity(self ...$operands): self
155154
throw new ShouldNotHappenException();
156155
}
157156

158-
$result = TrinaryLogic::extremeIdentity(...array_map(static fn (self $result) => $result->result, $operands));
157+
$results = [];
158+
$reasons = [];
159+
foreach ($operands as $operand) {
160+
$results[] = $operand->result;
161+
foreach ($operand->reasons as $reason) {
162+
$reasons[] = $reason;
163+
}
164+
}
159165

160-
return new self($result, self::mergeReasons($operands));
166+
return new self(TrinaryLogic::extremeIdentity(...$results), array_values(array_unique($reasons)));
161167
}
162168

163169
/** @see TrinaryLogic::maxMin() */
@@ -167,9 +173,16 @@ public static function maxMin(self ...$operands): self
167173
throw new ShouldNotHappenException();
168174
}
169175

170-
$result = TrinaryLogic::maxMin(...array_map(static fn (self $result) => $result->result, $operands));
176+
$results = [];
177+
$reasons = [];
178+
foreach ($operands as $operand) {
179+
$results[] = $operand->result;
180+
foreach ($operand->reasons as $reason) {
181+
$reasons[] = $reason;
182+
}
183+
}
171184

172-
return new self($result, self::mergeReasons($operands));
185+
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
173186
}
174187

175188
public function negate(): self
@@ -182,21 +195,4 @@ public function describe(): string
182195
return $this->result->describe();
183196
}
184197

185-
/**
186-
* @param array<self> $operands
187-
*
188-
* @return list<string>
189-
*/
190-
private static function mergeReasons(array $operands): array
191-
{
192-
$reasons = [];
193-
foreach ($operands as $operand) {
194-
foreach ($operand->reasons as $reason) {
195-
$reasons[] = $reason;
196-
}
197-
}
198-
199-
return array_values(array_unique($reasons));
200-
}
201-
202198
}

0 commit comments

Comments
 (0)