Skip to content

Commit 2652f2d

Browse files
committed
CleaningParser - do not remove inline @var tags
1 parent 7c8e81d commit 2652f2d

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/Parser/CleaningVisitor.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ public function __construct()
2020
public function enterNode(Node $node): ?Node
2121
{
2222
if ($node instanceof Node\Stmt\Function_) {
23-
$node->stmts = $this->keepVariadicsAndYields($node->stmts);
23+
$node->stmts = $this->keepVariadicsAndYieldsAndInlineVars($node->stmts);
2424
return $node;
2525
}
2626

2727
if ($node instanceof Node\Stmt\ClassMethod && $node->stmts !== null) {
28-
$node->stmts = $this->keepVariadicsAndYields($node->stmts);
28+
$node->stmts = $this->keepVariadicsAndYieldsAndInlineVars($node->stmts);
2929
return $node;
3030
}
3131

3232
if ($node instanceof Node\Expr\Closure) {
33-
$node->stmts = $this->keepVariadicsAndYields($node->stmts);
33+
$node->stmts = $this->keepVariadicsAndYieldsAndInlineVars($node->stmts);
3434
return $node;
3535
}
3636

@@ -41,7 +41,7 @@ public function enterNode(Node $node): ?Node
4141
* @param Node\Stmt[] $stmts
4242
* @return Node\Stmt[]
4343
*/
44-
private function keepVariadicsAndYields(array $stmts): array
44+
private function keepVariadicsAndYieldsAndInlineVars(array $stmts): array
4545
{
4646
$results = $this->nodeFinder->find($stmts, static function (Node $node): bool {
4747
if ($node instanceof Node\Expr\YieldFrom || $node instanceof Node\Expr\Yield_) {
@@ -50,6 +50,9 @@ private function keepVariadicsAndYields(array $stmts): array
5050
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name) {
5151
return in_array($node->name->toLowerString(), ParametersAcceptor::VARIADIC_FUNCTIONS, true);
5252
}
53+
if ($node instanceof Node\Stmt && $node->getDocComment() !== null) {
54+
return strpos($node->getDocComment()->getText(), '@var') !== false;
55+
}
5356

5457
return false;
5558
});
@@ -59,11 +62,12 @@ private function keepVariadicsAndYields(array $stmts): array
5962
$newStmts[] = new Node\Stmt\Expression($result);
6063
continue;
6164
}
62-
if (!$result instanceof Node\Expr\FuncCall) {
65+
if ($result instanceof Node\Expr\FuncCall && $result->name instanceof Node\Name) {
66+
$newStmts[] = new Node\Stmt\Expression(new Node\Expr\FuncCall(new Node\Name\FullyQualified($result->name), [], $result->getAttributes()));
6367
continue;
6468
}
6569

66-
$newStmts[] = new Node\Stmt\Expression(new Node\Expr\FuncCall(new Node\Name\FullyQualified('func_get_args')));
70+
$newStmts[] = new Node\Stmt\Nop($result->getAttributes());
6771
}
6872

6973
return $newStmts;

src/Type/FileTypeMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private function shouldPhpDocNodeBeCachedToDisk(PhpDocNode $phpDocNode): bool
217217
private function getResolvedPhpDocMap(string $fileName): array
218218
{
219219
if (!isset($this->memoryCache[$fileName])) {
220-
$cacheKey = sprintf('%s-phpdocstring-v10-function-name-stack', $fileName);
220+
$cacheKey = sprintf('%s-phpdocstring-v11-inline-vars', $fileName);
221221
$variableCacheKey = implode(',', array_map(static function (array $file): string {
222222
return sprintf('%s-%d', $file['filename'], $file['modifiedTime']);
223223
}, $this->getCachedDependentFilesWithTimestamps($fileName)));

tests/PHPStan/Parser/data/cleaning-1-after.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,14 @@ public function both()
3131
\func_get_args();
3232
}
3333
}
34+
class InlineVars
35+
{
36+
public function doFoo()
37+
{
38+
/** @var Test */
39+
/** @var Test2 */
40+
/** @var Test3 */
41+
yield;
42+
\func_get_args();
43+
}
44+
}

tests/PHPStan/Parser/data/cleaning-1-before.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,25 @@ public function both()
5454
}
5555

5656
}
57+
58+
class InlineVars
59+
{
60+
public function doFoo()
61+
{
62+
/** @var Test */
63+
$foo = doFoo();
64+
65+
/** @var Test2 */
66+
$foo = doFoo();
67+
68+
/** @var Test3 */
69+
$foo = doFoo();
70+
71+
if (rand(0, 1)) {
72+
yield;
73+
}
74+
if (rand(0, 1)) {
75+
func_get_args();
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)