Skip to content

Commit 42f6c2e

Browse files
committed
Improve VarTagTypeRuleHelper according to my taste
1 parent 69176bc commit 42f6c2e

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/Rules/PhpDoc/VarTagTypeRuleHelper.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,20 @@ private function containsPhpStanType(Type $type): bool
129129

130130
private function shouldVarTagTypeBeReported(Node\Expr $expr, Type $type, Type $varTagType): bool
131131
{
132-
if ($expr instanceof Expr\Array_ && $expr->items === []) {
133-
$type = new ArrayType(new MixedType(), new MixedType());
134-
return $type->isSuperTypeOf($varTagType)->no(); // allow annotating empty array with anything that can accept it (e.g. int[]|null)
132+
if ($expr instanceof Expr\Array_) {
133+
if ($expr->items === []) {
134+
$type = new ArrayType(new MixedType(), new MixedType());
135+
}
136+
137+
return $type->isSuperTypeOf($varTagType)->no();
138+
}
139+
140+
if ($expr instanceof Expr\ConstFetch) {
141+
return $type->isSuperTypeOf($varTagType)->no();
142+
}
143+
144+
if ($expr instanceof Node\Scalar) {
145+
return $type->isSuperTypeOf($varTagType)->no();
135146
}
136147

137148
if ($expr instanceof Expr\New_) {
@@ -160,10 +171,6 @@ private function checkType(Type $type, Type $varTagType): bool
160171
return $this->checkType($innerType, $innerVarTagType);
161172
}
162173

163-
if ($type->isConstantValue()->yes()) {
164-
return $type->isSuperTypeOf($varTagType)->no();
165-
}
166-
167174
return !$type->isSuperTypeOf($varTagType)->yes();
168175
}
169176

tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ public function testEmptyArrayInitWithWiderPhpDoc(bool $checkTypeAgainstNativeTy
356356
$errors = !$checkTypeAgainstNativeType
357357
? []
358358
: [
359+
[
360+
'PHPDoc tag @var with type int|null is not subtype of native type null.',
361+
6,
362+
],
359363
[
360364
'PHPDoc tag @var with type int is not subtype of native type array{}.',
361365
24,

0 commit comments

Comments
 (0)