Skip to content

Commit b29c03e

Browse files
authored
VerbosityLevel: early return type traversal (#4670)
1 parent 8dcd2ae commit b29c03e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/Type/VerbosityLevel.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,14 @@ public function isCache(): bool
9494
/** @api */
9595
public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType = null): self
9696
{
97+
$moreVerbose = false;
98+
$veryVerbose = false;
9799
$moreVerboseCallback = static function (Type $type, callable $traverse) use (&$moreVerbose, &$veryVerbose): Type {
100+
// stop deep traversal to not waste resources.
101+
if ($veryVerbose) {
102+
return $type;
103+
}
104+
98105
if ($type->isCallable()->yes()) {
99106
$moreVerbose = true;
100107

@@ -138,10 +145,6 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
138145
return $traverse($type);
139146
};
140147

141-
/** @var bool $moreVerbose */
142-
$moreVerbose = false;
143-
/** @var bool $veryVerbose */
144-
$veryVerbose = false;
145148
TypeTraverser::map($acceptingType, $moreVerboseCallback);
146149

147150
if ($veryVerbose) {
@@ -158,6 +161,11 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
158161

159162
$containsInvariantTemplateType = false;
160163
TypeTraverser::map($acceptingType, static function (Type $type, callable $traverse) use (&$containsInvariantTemplateType): Type {
164+
// stop deep traversal to not waste resources.
165+
if ($containsInvariantTemplateType) {
166+
return $type;
167+
}
168+
161169
if ($type instanceof GenericObjectType || $type instanceof GenericStaticType) {
162170
$reflection = $type->getClassReflection();
163171
if ($reflection !== null) {

0 commit comments

Comments
 (0)