Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for @pure-unless-parameter-passed #259

Open
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions src/Ast/PhpDoc/PhpDocNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ public function getPureUnlessCallableIsImpureTagValues(string $tagName = '@pure-
);
}

/**
* @return PureUnlessParameterIsPassedTagValueNode[]
*/
public function getPureUnlessParameterIsPassedTagValues(string $tagName = '@pure-unless-parameter-passed'): array
{
return array_filter(
array_column($this->getTagsByName($tagName), 'value'),
static fn (PhpDocTagValueNode $value): bool => $value instanceof PureUnlessParameterIsPassedTagValueNode,
);
}

/**
* @return TemplateTagValueNode[]
*/
Expand Down
29 changes: 29 additions & 0 deletions src/Ast/PhpDoc/PureUnlessParameterIsPassedTagValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\PhpDoc;

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

class PureUnlessParameterIsPassedTagValueNode implements PhpDocTagValueNode
{

use NodeAttributes;

public string $parameterName;

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

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

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

}
13 changes: 13 additions & 0 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
$tagValue = $this->parsePureUnlessCallableIsImpureTagValue($tokens);
break;

case '@pure-unless-parameter-passed':
case '@phpstan-pure-unless-parameter-passed':
$tagValue = $this->parsePureUnlessParameterIsPassed($tokens);
break;

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

private function parsePureUnlessParameterIsPassed(TokenIterator $tokens): Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode
{
$parameterName = $this->parseRequiredVariableName($tokens);
$description = $this->parseOptionalDescription($tokens, false);

return new Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode($parameterName, $description);
}

private function parseVarTagValue(TokenIterator $tokens): Ast\PhpDoc\VarTagValueNode
{
$type = $this->typeParser->parse($tokens);
Expand Down
4 changes: 4 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessCallableIsImpureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
Expand Down Expand Up @@ -346,6 +347,9 @@ private function printTagValue(PhpDocTagValueNode $node): string
if ($node instanceof PureUnlessCallableIsImpureTagValueNode) {
return trim("{$node->parameterName} {$node->description}");
}
if ($node instanceof PureUnlessParameterIsPassedTagValueNode) {
return trim("{$node->parameterName} {$node->description}");
}
if ($node instanceof PropertyTagValueNode) {
$type = $this->printType($node->type);
return trim("{$type} {$node->propertyName} {$node->description}");
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessCallableIsImpureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
Expand Down Expand Up @@ -753,6 +754,37 @@ public function providePureUnlessCallableIsImpureTagsData(): Iterator
];
}

public function providePureUnlessParameterIsPassedTagsData(): Iterator
{
yield [
'OK',
'/** @pure-unless-parameter-passed $foo */',
new PhpDocNode([
new PhpDocTagNode(
'@pure-unless-parameter-passed',
new PureUnlessParameterIsPassedTagValueNode(
'$foo',
'',
),
),
]),
];

yield [
'OK with description',
'/** @pure-unless-parameter-passed $foo test two three */',
new PhpDocNode([
new PhpDocTagNode(
'@pure-unless-parameter-passed',
new PureUnlessParameterIsPassedTagValueNode(
'$foo',
'test two three',
),
),
]),
];
}

public function provideVarTagsData(): Iterator
{
yield [
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessCallableIsImpureTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
Expand Down Expand Up @@ -1788,6 +1789,24 @@ public function enterNode(Node $node)
},
];

yield [
'/** @pure-unless-parameter-passed $foo test */',
'/** @pure-unless-parameter-passed $bar foo */',
new class extends AbstractNodeVisitor {

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

return $node;
}

},
];

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