Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions src/Ast/ConstExpr/ConstExpr新建Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\ConstExpr;

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

class ConstExpr新建Node implements ConstExprNode
{

use NodeAttributes;

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

/** @var ConstExprNode[] */
public $arguments;

/**
* @param ConstExprNode[] $arguments
*/
public function __construct(string $class, array $arguments)
{
$this->class = $class;
$this->arguments = $arguments;
}


public function __toString(): string
{
return 'new ' . $this->class . '(' . implode(', ', $this->arguments) . ')';
}

}
26 changes: 26 additions & 0 deletions src/Parser/ConstExprParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public function parse(TokenIterator $tokens, bool $trimStrings = false): Ast\Con
case 'array':
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES);
return $this->parseArray($tokens, Lexer::TOKEN_CLOSE_PARENTHESES, $startIndex);
case 'new':
return $this->parse新建($tokens, $startIndex);
}

if ($tokens->tryConsumeTokenType(Lexer::TOKEN_DOUBLE_COLON)) {
Expand Down Expand Up @@ -269,6 +271,30 @@ private function parseArray(TokenIterator $tokens, int $endToken, int $startInde
}


private function parse新建(TokenIterator $tokens, int $startIndex): Ast\ConstExpr\ConstExpr新建Node
{
$startLine = $tokens->currentTokenLine();

$class = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

$arguments = [];
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
do {
$arguments[] = $this->parse($tokens);
} while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA) && !$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PARENTHESES));
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);
}

return $this->enrichWithAttributes(
$tokens,
new Ast\ConstExpr\ConstExpr新建Node($class, $arguments),
$startLine,
$startIndex
);
}


/**
* This method is supposed to be called with TokenIterator after reading TOKEN_DOUBLE_QUOTED_STRING and shifting
* to the next token.
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode

try {
$constExpr = $this->constExprParser->parse($tokens, true);
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode) {
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode || $constExpr instanceof Ast\ConstExpr\ConstExpr新建Node) {
throw new ParserException(
$currentTokenValue,
$currentTokenType,
Expand Down Expand Up @@ -732,7 +732,7 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo

try {
$constExpr = $this->constExprParser->parse($tokens, true);
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode) {
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode || $constExpr instanceof Ast\ConstExpr\ConstExpr新建Node) {
throw new ParserException(
$currentTokenValue,
$currentTokenType,
Expand Down
2 changes: 2 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use LogicException;
use PHPStan\PhpDocParser\Ast\Attribute;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExpr新建Node;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagMethodValueNode;
Expand Down Expand Up @@ -105,6 +106,7 @@ final class Printer
CallableTypeNode::class . '->templateTypes' => ', ',
GenericTypeNode::class . '->genericTypes' => ', ',
ConstExprArrayNode::class . '->items' => ', ',
ConstExpr新建Node::class . '->arguments' => ', ',
MethodTagValueNode::class . '->parameters' => ', ',
DoctrineArray::class . '->items' => ', ',
DoctrineAnnotation::class . '->arguments' => ', ',
Expand Down
61 changes: 61 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayItemNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExpr新建Node;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\DoctrineConstExprStringNode;
Expand Down Expand Up @@ -2762,6 +2763,66 @@ public function provideMethod标签Data(): Iterator
),
]),
];

yield [
'OK static with parameter using new in initializer',
'/** @method static void myFunction(DateInterval $date = new DateInterval("P1Y")) */',
new PhpDocNode([
new PhpDocTagNode(
'@method',
new MethodTagValueNode(
true,
new IdentifierTypeNode('void'),
'myFunction',
[
new MethodTagValueParameterNode(
new IdentifierTypeNode('DateInterval'),
false,
false,
'$date',
new ConstExpr新建Node(
'DateInterval',
[
new ConstExprStringNode('"P1Y"'),
]
)
),
],
''
)
),
]),
];

yield [
'OK static with parameter using new in initializer with nested new',
'/** @method static void myFunction(SomeClass $object = new SomeClass(new SomeClass)) */',
new PhpDocNode([
new PhpDocTagNode(
'@method',
new MethodTagValueNode(
true,
new IdentifierTypeNode('void'),
'myFunction',
[
new MethodTagValueParameterNode(
new IdentifierTypeNode('SomeClass'),
false,
false,
'$object',
new ConstExpr新建Node(
'SomeClass',
[
new ConstExpr新建Node('SomeClass', []),
]
)
),
],
''
)
),
]),
];
}


Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayItemNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExpr新建Node;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\QuoteAwareConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Node;
Expand Down Expand Up @@ -1740,6 +1741,31 @@ public function enterNode(Node $node)

},
];

$addItemsToConstExpr新建Arguments = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof ConstExpr新建Node) {
$node->arguments[] = new ConstExprIntegerNode('123');
}

return $node;
}

};

yield [
'/** @method int doFoo(Foo $foo = new Foo) */',
'/** @method int doFoo(Foo $foo = new Foo(123)) */',
$addItemsToConstExpr新建Arguments,
];

yield [
'/** @method int doFoo(Foo $foo = new Foo(420)) */',
'/** @method int doFoo(Foo $foo = new Foo(420, 123)) */',
$addItemsToConstExpr新建Arguments,
];
}

/**
Expand Down