Skip to content

Commit 189a4cc

Browse files
committed
VarTagTypeRuleHelper - remove namespace and uses from NameScope
Node provided by `Type::toPhpDocNode()` is already FQN.
1 parent 3019d39 commit 189a4cc

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

src/Analyser/NameScope.php

+14
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ public function withTemplateTypeMap(TemplateTypeMap $map): self
174174
);
175175
}
176176

177+
public function withoutNamespaceAndUses(): self
178+
{
179+
return new self(
180+
null,
181+
[],
182+
$this->className,
183+
$this->functionName,
184+
$this->templateTypeMap,
185+
$this->typeAliasesMap,
186+
$this->bypassTypeAliases,
187+
$this->constUses,
188+
);
189+
}
190+
177191
public function withClassName(string $className): self
178192
{
179193
return new self(

src/Rules/PhpDoc/VarTagTypeRuleHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private function createNameScope(Scope $scope): NameScope
240240
$scope->isInClass() ? $scope->getClassReflection()->getName() : null,
241241
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
242242
$function !== null ? $function->getName() : null,
243-
);
243+
)->withoutNamespaceAndUses();
244244
}
245245

246246
}

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,8 @@ public function testBug5091(): void
12121212
public function testBug9459(): void
12131213
{
12141214
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9459.php');
1215-
$this->assertCount(0, $errors);
1215+
$this->assertCount(1, $errors);
1216+
$this->assertSame('PHPDoc tag @var with type callable(): array<mixed> is not subtype of native type Closure(): array{}.', $errors[0]->getMessage());
12161217
}
12171218

12181219
public function testBug9573(): void

tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,12 @@ public function testBug11535(): void
226226
$this->checkTypeAgainstPhpDocType = true;
227227
$this->strictWideningCheck = true;
228228

229-
$this->analyse([__DIR__ . '/data/bug-11535.php'], []);
229+
$this->analyse([__DIR__ . '/data/bug-11535.php'], [
230+
[
231+
'PHPDoc tag @var with type Closure(string): array<int> is not subtype of native type Closure(string): array{1, 2, 3}.',
232+
6,
233+
],
234+
]);
230235
}
231236

232237
public function testEnums(): void
@@ -561,4 +566,11 @@ public function testBug12457(): void
561566
]);
562567
}
563568

569+
public function testNewIsAlwaysFinalClass(): void
570+
{
571+
$this->checkTypeAgainstPhpDocType = true;
572+
$this->strictWideningCheck = true;
573+
$this->analyse([__DIR__ . '/data/new-is-always-final-var-tag-type.php'], []);
574+
}
575+
564576
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace NewIsAlwaysFinalVarTagType;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @return static
10+
*/
11+
public function returnStatic()
12+
{
13+
return $this;
14+
}
15+
16+
}
17+
18+
function (): void {
19+
$foo = new Foo();
20+
21+
/** @var Foo $bar */
22+
$bar = $foo->returnStatic();
23+
};

0 commit comments

Comments
 (0)