Skip to content

Commit 1f1d382

Browse files
committed
Optimize CallableType::equals()
1 parent d27df94 commit 1f1d382

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

src/Type/CallableType.php

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
4141
use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
4242
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
43+
use function array_key_exists;
4344
use function array_map;
4445
use function array_merge;
4546
use function count;
@@ -218,7 +219,100 @@ public function equals(Type $type): bool
218219
return false;
219220
}
220221

221-
return $this->describe(VerbosityLevel::precise()) === $type->describe(VerbosityLevel::precise());
222+
if ($this->isCommonCallable) {
223+
if (!$type->isCommonCallable) {
224+
return false;
225+
}
226+
} elseif ($type->isCommonCallable) {
227+
return false;
228+
}
229+
230+
if ($this->variadic !== $type->variadic) {
231+
return false;
232+
}
233+
234+
if ($this->isPure !== $type->isPure) {
235+
return false;
236+
}
237+
238+
if (!$this->returnType->equals($type->returnType)) {
239+
return false;
240+
}
241+
242+
if (count($this->parameters) !== count($type->parameters)) {
243+
return false;
244+
}
245+
246+
foreach ($this->parameters as $i => $parameter) {
247+
$otherParameter = $type->parameters[$i];
248+
if ($parameter->isOptional() !== $otherParameter->isOptional()) {
249+
return false;
250+
}
251+
if (!$parameter->passedByReference()->equals($otherParameter->passedByReference())) {
252+
return false;
253+
}
254+
if ($parameter->isVariadic() !== $otherParameter->isVariadic()) {
255+
return false;
256+
}
257+
if (!$parameter->getType()->equals($otherParameter->getType())) {
258+
return false;
259+
}
260+
if ($parameter->getDefaultValue() !== null) {
261+
if ($otherParameter->getDefaultValue() === null) {
262+
return false;
263+
}
264+
265+
return $parameter->getDefaultValue()->equals($otherParameter->getDefaultValue());
266+
} elseif ($otherParameter->getDefaultValue() !== null) {
267+
return false;
268+
}
269+
}
270+
271+
foreach ([[$this->templateTypeMap, $type->templateTypeMap], [$this->resolvedTemplateTypeMap, $type->resolvedTemplateTypeMap]] as [$templateTypeMap, $otherTemplateTypeMap]) {
272+
if ($templateTypeMap->count() !== $otherTemplateTypeMap->count()) {
273+
return false;
274+
}
275+
276+
foreach ($templateTypeMap->getTypes() as $typeName => $templateType) {
277+
$otherTemplateType = $otherTemplateTypeMap->getType($typeName);
278+
if ($otherTemplateType === null) {
279+
return false;
280+
}
281+
282+
if (!$templateType->equals($otherTemplateType)) {
283+
return false;
284+
}
285+
}
286+
}
287+
288+
foreach ($this->templateTags as $tagName => $tag) {
289+
if (!array_key_exists($tagName, $type->templateTags)) {
290+
return false;
291+
}
292+
293+
$otherTag = $type->templateTags[$tagName];
294+
if ($tag->getName() !== $otherTag->getName()) {
295+
return false;
296+
}
297+
298+
if (!$tag->getBound()->equals($otherTag->getBound())) {
299+
return false;
300+
}
301+
if (!$tag->getVariance()->equals($otherTag->getVariance())) {
302+
return false;
303+
}
304+
if ($tag->getDefault() !== null) {
305+
if ($otherTag->getDefault() === null) {
306+
return false;
307+
}
308+
309+
return $tag->getDefault()->equals($otherTag->getDefault());
310+
} elseif ($otherTag->getDefault() !== null) {
311+
return false;
312+
}
313+
}
314+
315+
return true;
222316
}
223317

224318
public function describe(VerbosityLevel $level): string

0 commit comments

Comments
 (0)