Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/config.level2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rules:
- PHPStan\Rules\PhpDoc\IncompatiblePhpDocTypeRule
- PHPStan\Rules\PhpDoc\IncompatiblePropertyPhpDocTypeRule
- PHPStan\Rules\PhpDoc\InvalidPhpDocTagValueRule
- PHPStan\Rules\PhpDoc\InvalidPHPStanDocTagRule
- PHPStan\Rules\PhpDoc\InvalidThrowsPhpDocValueRule
- PHPStan\Rules\PhpDoc\WrongVariableNameInVarTagRule

Expand Down
85 changes: 85 additions & 0 deletions src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PhpDoc;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node>
*/
class InvalidPHPStanDocTagRule implements \PHPStan\Rules\Rule
{

private const POSSIBLE_PHPSTAN_TAGS = [
'@phpstan-param',
'@phpstan-var',
'@phpstan-template',
'@phpstan-extends',
'@phpstan-implements',
'@phpstan-use',
'@phpstan-template',
'@phpstan-template-covariant',
'@phpstan-return',
];

/** @var Lexer */
private $phpDocLexer;

/** @var PhpDocParser */
private $phpDocParser;

public function __construct(Lexer $phpDocLexer, PhpDocParser $phpDocParser)
{
$this->phpDocLexer = $phpDocLexer;
$this->phpDocParser = $phpDocParser;
}

public function getNodeType(): string
{
return \PhpParser\Node::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (
!$node instanceof Node\Stmt\ClassLike
&& !$node instanceof Node\FunctionLike
&& !$node instanceof Node\Stmt\Foreach_
&& !$node instanceof Node\Stmt\Property
&& !$node instanceof Node\Expr\Assign
&& !$node instanceof Node\Expr\AssignRef
) {
return [];
}

$docComment = $node->getDocComment();
if ($docComment === null) {
return [];
}
$phpDocString = $docComment->getText();
$tokens = new TokenIterator($this->phpDocLexer->tokenize($phpDocString));
$phpDocNode = $this->phpDocParser->parse($tokens);

$errors = [];
foreach ($phpDocNode->getTags() as $phpDocTag) {
if (strpos($phpDocTag->name, '@phpstan-') !== 0
|| in_array($phpDocTag->name, self::POSSIBLE_PHPSTAN_TAGS, true)
) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf(
'Encountered unknown tag that had the phpstan prefix: %s',
$phpDocTag->name
))->build();
}

return $errors;
}

}
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/InvalidPHPStanDocTagRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PhpDoc;

use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;

/**
* @extends \PHPStan\Testing\RuleTestCase<InvalidPHPStanDocTagRule>
*/
class InvalidPHPStanDocTagRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
return new InvalidPHPStanDocTagRule(
self::getContainer()->getByType(Lexer::class),
self::getContainer()->getByType(PhpDocParser::class)
);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/invalid-phpstan-doc.php'], [
[
'Encountered unknown tag that had the phpstan prefix: @phpstan-extens',
7,
],
[
'Encountered unknown tag that had the phpstan prefix: @phpstan-pararm',
14,
],
]);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/invalid-phpstan-doc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace InvalidPHPStanDoc;

class Baz{}
/** @phpstan-extens Baz */
class Boo extends Baz
{
/**
* @phpstan-template T
* @phpstan-pararm class-string<T> $a
* @phpstan-return T
*/
function foo(string $a){}
}