Skip to content

Add generic support to @method definitions #160

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

Merged
merged 7 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions src/Ast/PhpDoc/MethodTagValueGenericNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;

class MethodTagValueGenericNode implements Node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this doesn't need a new node, but TemplateTagValueNode can be reused instead. We'd also gain default template types (#148) automatically :)

{

use NodeAttributes;

/** @var string */
public $name;

/** @var TypeNode|null */
public $bound;

public function __construct(string $name, ?TypeNode $bound)
{
$this->name = $name;
$this->bound = $bound;
}


public function __toString(): string
{
$bound = $this->bound !== null ? " of {$this->bound}" : '';
return trim("{$this->name}{$bound}");
}

}
9 changes: 7 additions & 2 deletions src/Ast/PhpDoc/MethodTagValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ class MethodTagValueNode implements PhpDocTagValueNode
/** @var string */
public $methodName;

/** @var MethodTagValueGenericNode[] */
public $generics;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This thing should be called $templateTypes probably.


/** @var MethodTagValueParameterNode[] */
public $parameters;

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

public function __construct(bool $isStatic, ?TypeNode $returnType, string $methodName, array $parameters, string $description)
public function __construct(bool $isStatic, ?TypeNode $returnType, string $methodName, array $generics, array $parameters, string $description)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter needs to be added as last and needs to be optional, otherwise it's a BC break.

{
$this->isStatic = $isStatic;
$this->returnType = $returnType;
$this->methodName = $methodName;
$this->generics = $generics;
$this->parameters = $parameters;
$this->description = $description;
}
Expand All @@ -40,9 +44,10 @@ public function __toString(): string
{
$static = $this->isStatic ? 'static ' : '';
$returnType = $this->returnType !== null ? "{$this->returnType} " : '';
$generics = count($this->generics) > 0 ? '<' . implode(', ', $this->generics) . '>' : '';
$parameters = implode(', ', $this->parameters);
$description = $this->description !== '' ? " {$this->description}" : '';
return "{$static}{$returnType}{$this->methodName}({$parameters}){$description}";
return "{$static}{$returnType}{$this->methodName}{$generics}({$parameters}){$description}";
}

}
23 changes: 22 additions & 1 deletion src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ private function parseMethodTagValue(TokenIterator $tokens): Ast\PhpDoc\MethodTa
exit;
}

$generics = [];
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
do {
$generics[] = $this->parseMethodTagValueGeneric($tokens);
} while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
}

$parameters = [];
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES);
if (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PARENTHESES)) {
Expand All @@ -357,9 +365,22 @@ private function parseMethodTagValue(TokenIterator $tokens): Ast\PhpDoc\MethodTa
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);

$description = $this->parseOptionalDescription($tokens);
return new Ast\PhpDoc\MethodTagValueNode($isStatic, $returnType, $methodName, $parameters, $description);
return new Ast\PhpDoc\MethodTagValueNode($isStatic, $returnType, $methodName, $generics, $parameters, $description);
}

private function parseMethodTagValueGeneric(TokenIterator $tokens): Ast\PhpDoc\MethodTagValueGenericNode
{
$name = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

if ($tokens->tryConsumeTokenValue('of') || $tokens->tryConsumeTokenValue('as')) {
$bound = $this->typeParser->parse($tokens);
} else {
$bound = null;
}

return new Ast\PhpDoc\MethodTagValueGenericNode($name, $bound);
}

private function parseMethodTagValueParameter(TokenIterator $tokens): Ast\PhpDoc\MethodTagValueParameterNode
{
Expand Down
Loading