Skip to content

Commit aba4348

Browse files
Refacto
1 parent ff13cef commit aba4348

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/Rules/PhpDoc/VarTagTypeRuleHelper.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ private function shouldVarTagTypeBeReported(Node\Expr $expr, Type $type, Type $v
141141
$type = new ArrayType(new MixedType(), new MixedType());
142142
}
143143

144-
return $this->isSuperTypeOfVarType($type, $varTagType)->no();
144+
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
145145
}
146146

147147
if ($expr instanceof Expr\ConstFetch) {
148-
return $this->isSuperTypeOfVarType($type, $varTagType)->no();
148+
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
149149
}
150150

151151
if ($expr instanceof Node\Scalar) {
152-
return $this->isSuperTypeOfVarType($type, $varTagType)->no();
152+
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
153153
}
154154

155155
if ($expr instanceof Expr\New_) {
@@ -164,43 +164,50 @@ private function shouldVarTagTypeBeReported(Node\Expr $expr, Type $type, Type $v
164164
private function checkType(Type $type, Type $varTagType, int $depth = 0): bool
165165
{
166166
if ($this->strictWideningCheck) {
167-
return !$this->isSuperTypeOfVarType($type, $varTagType)->yes();
167+
return !$this->isSuperTypeOfVarType($type, $varTagType, true);
168168
}
169169

170170
if ($type->isConstantArray()->yes()) {
171171
if ($type->isIterableAtLeastOnce()->no()) {
172172
$type = new ArrayType(new MixedType(), new MixedType());
173-
return $this->isSuperTypeOfVarType($type, $varTagType)->no();
173+
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
174174
}
175175
}
176176

177177
if ($type->isIterable()->yes() && $varTagType->isIterable()->yes()) {
178-
if ($this->isSuperTypeOfVarType($type, $varTagType)->no()) {
178+
if (!$this->isSuperTypeOfVarType($type, $varTagType, false)) {
179179
return true;
180180
}
181181

182182
$innerType = $type->getIterableValueType();
183183
$innerVarTagType = $varTagType->getIterableValueType();
184184

185185
if ($type->equals($innerType) || $varTagType->equals($innerVarTagType)) {
186-
return !$this->isSuperTypeOfVarType($innerType, $innerVarTagType)->yes();
186+
return !$this->isSuperTypeOfVarType($innerType, $innerVarTagType, true);
187187
}
188188

189189
return $this->checkType($innerType, $innerVarTagType, $depth + 1);
190190
}
191191

192192
if ($depth === 0 && $type->isConstantValue()->yes()) {
193-
return $this->isSuperTypeOfVarType($type, $varTagType)->no();
193+
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
194194
}
195195

196-
return !$this->isSuperTypeOfVarType($type, $varTagType)->yes();
196+
return !$this->isSuperTypeOfVarType($type, $varTagType, true);
197197
}
198198

199-
private function isSuperTypeOfVarType(Type $type, Type $varTagType): TrinaryLogic
199+
private function isSuperTypeOfVarType(Type $type, Type $varTagType, bool $strict): bool
200200
{
201+
$validationCallable = static fn (TrinaryLogic $trinaryLogic): bool => $strict ? $trinaryLogic->yes() : !$trinaryLogic->no();
202+
203+
$result = $type->isSuperTypeOf($varTagType);
204+
if ($validationCallable($result)) {
205+
return true;
206+
}
207+
201208
$type = $this->typeNodeResolver->resolve($type->toPhpDocNode(), new NameScope(null, []));
202209

203-
return $type->isSuperTypeOf($varTagType);
210+
return $validationCallable($type->isSuperTypeOf($varTagType));
204211
}
205212

206213
}

0 commit comments

Comments
 (0)