Skip to content

Commit aa6ae1d

Browse files
Refacto
1 parent d8b9c48 commit aa6ae1d

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/Rules/PhpDoc/VarTagTypeRuleHelper.php

+22-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\PhpDoc\TypeNodeResolver;
1212
use PHPStan\Rules\IdentifierRuleError;
1313
use PHPStan\Rules\RuleErrorBuilder;
14-
use PHPStan\TrinaryLogic;
1514
use PHPStan\Type\ArrayType;
1615
use PHPStan\Type\Generic\GenericObjectType;
1716
use PHPStan\Type\MixedType;
@@ -141,15 +140,15 @@ private function shouldVarTagTypeBeReported(Node\Expr $expr, Type $type, Type $v
141140
$type = new ArrayType(new MixedType(), new MixedType());
142141
}
143142

144-
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
143+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
145144
}
146145

147146
if ($expr instanceof Expr\ConstFetch) {
148-
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
147+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
149148
}
150149

151150
if ($expr instanceof Node\Scalar) {
152-
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
151+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
153152
}
154153

155154
if ($expr instanceof Expr\New_) {
@@ -164,50 +163,58 @@ private function shouldVarTagTypeBeReported(Node\Expr $expr, Type $type, Type $v
164163
private function checkType(Type $type, Type $varTagType, int $depth = 0): bool
165164
{
166165
if ($this->strictWideningCheck) {
167-
return !$this->isSuperTypeOfVarType($type, $varTagType, true);
166+
return !$this->isSuperTypeOfVarType($type, $varTagType);
168167
}
169168

170169
if ($type->isConstantArray()->yes()) {
171170
if ($type->isIterableAtLeastOnce()->no()) {
172171
$type = new ArrayType(new MixedType(), new MixedType());
173-
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
172+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
174173
}
175174
}
176175

177176
if ($type->isIterable()->yes() && $varTagType->isIterable()->yes()) {
178-
if (!$this->isSuperTypeOfVarType($type, $varTagType, false)) {
177+
if (!$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType)) {
179178
return true;
180179
}
181180

182181
$innerType = $type->getIterableValueType();
183182
$innerVarTagType = $varTagType->getIterableValueType();
184183

185184
if ($type->equals($innerType) || $varTagType->equals($innerVarTagType)) {
186-
return !$this->isSuperTypeOfVarType($innerType, $innerVarTagType, true);
185+
return !$this->isSuperTypeOfVarType($innerType, $innerVarTagType);
187186
}
188187

189188
return $this->checkType($innerType, $innerVarTagType, $depth + 1);
190189
}
191190

192191
if ($depth === 0 && $type->isConstantValue()->yes()) {
193-
return !$this->isSuperTypeOfVarType($type, $varTagType, false);
192+
return !$this->isAtLeastMaybeSuperTypeOfVarType($type, $varTagType);
194193
}
195194

196-
return !$this->isSuperTypeOfVarType($type, $varTagType, true);
195+
return !$this->isSuperTypeOfVarType($type, $varTagType);
197196
}
198197

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

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

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

210-
return $validationCallable($type->isSuperTypeOf($varTagType));
217+
return !$type->isSuperTypeOf($varTagType)->no();
211218
}
212219

213220
}

0 commit comments

Comments
 (0)