Skip to content

Commit

Permalink
CleaningParser - do not remove inline @var tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 16, 2021
1 parent 7c8e81d commit 2652f2d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/Parser/CleaningVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public function __construct()
public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Stmt\Function_) {
$node->stmts = $this->keepVariadicsAndYields($node->stmts);
$node->stmts = $this->keepVariadicsAndYieldsAndInlineVars($node->stmts);
return $node;
}

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

if ($node instanceof Node\Expr\Closure) {
$node->stmts = $this->keepVariadicsAndYields($node->stmts);
$node->stmts = $this->keepVariadicsAndYieldsAndInlineVars($node->stmts);
return $node;
}

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

return false;
});
Expand All @@ -59,11 +62,12 @@ private function keepVariadicsAndYields(array $stmts): array
$newStmts[] = new Node\Stmt\Expression($result);
continue;
}
if (!$result instanceof Node\Expr\FuncCall) {
if ($result instanceof Node\Expr\FuncCall && $result->name instanceof Node\Name) {
$newStmts[] = new Node\Stmt\Expression(new Node\Expr\FuncCall(new Node\Name\FullyQualified($result->name), [], $result->getAttributes()));
continue;
}

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

return $newStmts;
Expand Down
2 changes: 1 addition & 1 deletion src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private function shouldPhpDocNodeBeCachedToDisk(PhpDocNode $phpDocNode): bool
private function getResolvedPhpDocMap(string $fileName): array
{
if (!isset($this->memoryCache[$fileName])) {
$cacheKey = sprintf('%s-phpdocstring-v10-function-name-stack', $fileName);
$cacheKey = sprintf('%s-phpdocstring-v11-inline-vars', $fileName);
$variableCacheKey = implode(',', array_map(static function (array $file): string {
return sprintf('%s-%d', $file['filename'], $file['modifiedTime']);
}, $this->getCachedDependentFilesWithTimestamps($fileName)));
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Parser/data/cleaning-1-after.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ public function both()
\func_get_args();
}
}
class InlineVars
{
public function doFoo()
{
/** @var Test */
/** @var Test2 */
/** @var Test3 */
yield;
\func_get_args();
}
}
22 changes: 22 additions & 0 deletions tests/PHPStan/Parser/data/cleaning-1-before.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,25 @@ public function both()
}

}

class InlineVars
{
public function doFoo()
{
/** @var Test */
$foo = doFoo();

/** @var Test2 */
$foo = doFoo();

/** @var Test3 */
$foo = doFoo();

if (rand(0, 1)) {
yield;
}
if (rand(0, 1)) {
func_get_args();
}
}
}

0 comments on commit 2652f2d

Please sign in to comment.