Skip to content

Commit c4acd25

Browse files
Refacto
1 parent d8b9c48 commit c4acd25

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/Rules/PhpDoc/VarTagTypeRuleHelper.php

+22-14
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, false);
144+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
145145
}
146146

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

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

155155
if ($expr instanceof Expr\New_) {
@@ -164,50 +164,58 @@ 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, true);
167+
return !$this->isSuperTypeOfVarType($type, $varTagType);
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, false);
173+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
174174
}
175175
}
176176

177177
if ($type->isIterable()->yes() && $varTagType->isIterable()->yes()) {
178-
if (!$this->isSuperTypeOfVarType($type, $varTagType, false)) {
178+
if (!$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType)) {
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, true);
186+
return !$this->isSuperTypeOfVarType($innerType, $innerVarTagType);
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, false);
193+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
194194
}
195195

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

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

203-
$result = $type->isSuperTypeOf($varTagType);
204-
if ($validationCallable($result)) {
207+
return $type->isSuperTypeOf($varTagType)->yes();
208+
}
209+
210+
private function isAtLeastMaybeSuperTypeOfVarType(Type $type, Type $varTagType): bool
211+
{
212+
if (!$type->isSuperTypeOf($varTagType)->no()) {
205213
return true;
206214
}
207215

208216
$type = $this->typeNodeResolver->resolve($type->toPhpDocNode(), new NameScope(null, []));
209217

210-
return $validationCallable($type->isSuperTypeOf($varTagType));
218+
return !$type->isSuperTypeOf($varTagType)->no();
211219
}
212220

213221
}

0 commit comments

Comments
 (0)