Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/Ast/PhpDoc/AllMethodsImpureTagValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\NodeAttributes;
use function trim;

class AllMethodsImpureTagValueNode implements PhpDocTagValueNode
{

use NodeAttributes;

/** @var string (may be empty) */
public string $description;

public function __construct(string $description)
{
$this->description = $description;
}

public function __toString(): string
{
return trim($this->description);
}

}
26 changes: 26 additions & 0 deletions src/Ast/PhpDoc/AllMethodsPureTagValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\NodeAttributes;
use function trim;

class AllMethodsPureTagValueNode implements PhpDocTagValueNode
{

use NodeAttributes;

/** @var string (may be empty) */
public string $description;

public function __construct(string $description)
{
$this->description = $description;
}

public function __toString(): string
{
return trim($this->description);
}

}
22 changes: 22 additions & 0 deletions src/Ast/PhpDoc/PhpDocNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ public function getPureUnlessCallableIsImpureTagValues(string $tagName = '@pure-
);
}

/**
* @return AllMethodsImpureTagValueNode[]
*/
public function getAllMethodsImpureTagValues(string $tagName = '@phpstan-all-methods-impure'): array
{
return array_filter(
array_column($this->getTagsByName($tagName), 'value'),
static fn (PhpDocTagValueNode $value): bool => $value instanceof AllMethodsImpureTagValueNode,
);
}

/**
* @return AllMethodsPureTagValueNode[]
*/
public function getAllMethodsPureTagValues(string $tagName = '@phpstan-all-methods-pure'): array
{
return array_filter(
array_column($this->getTagsByName($tagName), 'value'),
static fn (PhpDocTagValueNode $value): bool => $value instanceof AllMethodsPureTagValueNode,
);
}

/**
* @return TemplateTagValueNode[]
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
$tagValue = $this->parsePureUnlessCallableIsImpureTagValue($tokens);
break;

case '@phpstan-all-methods-impure':
$tagValue = $this->parseAllMethodsImpureTagValue($tokens);
break;

case '@phpstan-all-methods-pure':
$tagValue = $this->parseAllMethodsPureTagValue($tokens);
break;

case '@var':
case '@phpstan-var':
case '@psalm-var':
Expand Down Expand Up @@ -877,6 +885,20 @@ private function parsePureUnlessCallableIsImpureTagValue(TokenIterator $tokens):
return new Ast\PhpDoc\PureUnlessCallableIsImpureTagValueNode($parameterName, $description);
}

private function parseAllMethodsImpureTagValue(TokenIterator $tokens): Ast\PhpDoc\AllMethodsImpureTagValueNode
{
$description = $this->parseOptionalDescription($tokens, false);

return new Ast\PhpDoc\AllMethodsImpureTagValueNode($description);
}

private function parseAllMethodsPureTagValue(TokenIterator $tokens): Ast\PhpDoc\AllMethodsPureTagValueNode
{
$description = $this->parseOptionalDescription($tokens, false);

return new Ast\PhpDoc\AllMethodsPureTagValueNode($description);
}

private function parseVarTagValue(TokenIterator $tokens): Ast\PhpDoc\VarTagValueNode
{
$type = $this->typeParser->parse($tokens);
Expand Down
8 changes: 8 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\AllMethodsImpureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AllMethodsPureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagMethodValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagPropertyValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode;
Expand Down Expand Up @@ -354,6 +356,12 @@ private function printTagValue(PhpDocTagValueNode $node): string
if ($node instanceof PureUnlessCallableIsImpureTagValueNode) {
return trim("{$node->parameterName} {$node->description}");
}
if ($node instanceof AllMethodsImpureTagValueNode) {
return trim($node->description);
}
if ($node instanceof AllMethodsPureTagValueNode) {
return trim($node->description);
}
if ($node instanceof PropertyTagValueNode) {
$type = $this->printType($node->type);
return trim("{$type} {$node->propertyName} {$node->description}");
Expand Down
62 changes: 62 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use PHPStan\PhpDocParser\Ast\ConstExpr\DoctrineConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeTraverser;
use PHPStan\PhpDocParser\Ast\PhpDoc\AllMethodsImpureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AllMethodsPureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagMethodValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagPropertyValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode;
Expand Down Expand Up @@ -102,6 +104,8 @@ protected function setUp(): void
* @dataProvider provideTypelessParamTagsData
* @dataProvider provideParamClosureThisTagsData
* @dataProvider providePureUnlessCallableIsImpureTagsData
* @dataProvider provideAllMethodsImpureTagsData
* @dataProvider provideAllMethodsPureTagsData
* @dataProvider provideVarTagsData
* @dataProvider provideReturnTagsData
* @dataProvider provideThrowsTagsData
Expand Down Expand Up @@ -757,6 +761,64 @@ public function providePureUnlessCallableIsImpureTagsData(): Iterator
];
}

public function provideAllMethodsImpureTagsData(): Iterator
{
yield [
'OK',
'/** @phpstan-all-methods-impure */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-all-methods-impure',
new AllMethodsImpureTagValueNode(
'',
),
),
]),
];

yield [
'OK with description',
'/** @phpstan-all-methods-impure test two three */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-all-methods-impure',
new AllMethodsImpureTagValueNode(
'test two three',
),
),
]),
];
}

public function provideAllMethodsPureTagsData(): Iterator
{
yield [
'OK',
'/** @phpstan-all-methods-pure */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-all-methods-pure',
new AllMethodsPureTagValueNode(
'',
),
),
]),
];

yield [
'OK with description',
'/** @phpstan-all-methods-pure test two three */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-all-methods-pure',
new AllMethodsPureTagValueNode(
'test two three',
),
),
]),
];
}

public function provideVarTagsData(): Iterator
{
yield [
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeTraverser;
use PHPStan\PhpDocParser\Ast\NodeVisitor;
use PHPStan\PhpDocParser\Ast\PhpDoc\AllMethodsImpureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AllMethodsPureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineAnnotation;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArgument;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArray;
Expand Down Expand Up @@ -2115,6 +2117,40 @@ public function enterNode(Node $node)
},
];

yield [
'/** @phpstan-all-methods-impure test */',
'/** @phpstan-all-methods-impure foo */',
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof AllMethodsImpureTagValueNode) {
$node->description = 'foo';
}

return $node;
}

},
];

yield [
'/** @phpstan-all-methods-pure test */',
'/** @phpstan-all-methods-pure foo */',
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof AllMethodsPureTagValueNode) {
$node->description = 'foo';
}

return $node;
}

},
];

yield [
'/** @return Foo[abc] */',
'/** @return self::FOO[abc] */',
Expand Down
Loading