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
7 changes: 6 additions & 1 deletion src/Ast/Type/ConditionalTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public function __toString(): string
{
return sprintf(
'(%s %s %s ? %s : %s)',
$this->subjectType,
// "?Foo is Bar ? ... : ..." reads as a nullable type followed by
// something else, so the subject keeps the parentheses it was read
// with
$this->subjectType instanceof NullableTypeNode
? '(' . $this->subjectType . ')'
: $this->subjectType,
$this->negated ? 'is not' : 'is',
$this->targetType,
$this->if,
Expand Down
6 changes: 6 additions & 0 deletions src/Ast/Type/NullableTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public function __construct(TypeNode $type)

public function __toString(): string
{
if ($this->type instanceof self) {
// "??Foo" is no type at all, so the one written inside keeps the
// parentheses it was read with
return '?(' . $this->type . ')';
}

return '?' . $this->type;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode
$type = new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances);
if ($startLine !== null && $startIndex !== null) {
$type = $this->enrichWithAttributes($tokens, $type, $startLine, $startIndex);
} else {
// The comments read between the arguments have to be given away
// even where the node is not placed, or they would still be waiting
// for a node once the whole PHPDoc has been read
$tokens->flushComments();
}

$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
Expand Down
39 changes: 37 additions & 2 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ final class Printer
IntersectionTypeNode::class,
NullableTypeNode::class,
],
NullableTypeNode::class . '->type' => [
UnionTypeNode::class,
IntersectionTypeNode::class,
NullableTypeNode::class,
],
ConditionalTypeNode::class . '->subjectType' => [
UnionTypeNode::class,
IntersectionTypeNode::class,
NullableTypeNode::class,
],
];

/** @var array<string, list<class-string<TypeNode>>> */
Expand Down Expand Up @@ -441,7 +451,7 @@ private function printType(TypeNode $node): string
if ($node instanceof ConditionalTypeNode) {
return sprintf(
'(%s %s %s ? %s : %s)',
$this->printType($node->subjectType),
$this->printConditionalSubjectType($node->subjectType),
$node->negated ? 'is not' : 'is',
$this->printType($node->targetType),
$this->printType($node->if),
Expand Down Expand Up @@ -491,7 +501,11 @@ private function printType(TypeNode $node): string
return (string) $node;
}
if ($node instanceof NullableTypeNode) {
if ($node->type instanceof IntersectionTypeNode || $node->type instanceof UnionTypeNode) {
if (
$node->type instanceof IntersectionTypeNode
|| $node->type instanceof UnionTypeNode
|| $node->type instanceof NullableTypeNode
) {
return '?(' . $this->printType($node->type) . ')';
}

Expand All @@ -517,6 +531,27 @@ private function wrapInParentheses(TypeNode $node): string
return '(' . $this->printType($node) . ')';
}

/**
* What a conditional type asks about, written so that it is read back as
* the very same type.
*
* "?Foo is Bar ? ... : ..." and "Foo|Bar is Baz ? ... : ..." both read as a
* type followed by something the type says nothing about, so a subject of
* either kind keeps its parentheses.
*/
private function printConditionalSubjectType(TypeNode $type): string
{
if (
$type instanceof UnionTypeNode
|| $type instanceof IntersectionTypeNode
|| $type instanceof NullableTypeNode
) {
return $this->wrapInParentheses($type);
}

return $this->printType($type);
}

private function printOffsetAccessType(TypeNode $type): string
{
if (
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8245,4 +8245,39 @@ public function testTextBetweenTagsBelongsToDescription(
$this->assertSame(Lexer::TOKEN_END, $tokens->currentTokenType());
}

/**
* @return iterable<array{string}>
*/
public function dataCommentInsideGenericType(): iterable
{
yield ['/**' . PHP_EOL . ' * @use Foo<int // a comment' . PHP_EOL . ' * >' . PHP_EOL . ' */'];
yield ['/**' . PHP_EOL . ' * @var Foo<int, // a comment' . PHP_EOL . ' * string>' . PHP_EOL . ' */'];
yield ['/**' . PHP_EOL . ' * @extends Foo<*, // a comment' . PHP_EOL . ' * string>' . PHP_EOL . ' */'];
yield ['/**' . PHP_EOL . ' * @implements Foo<int // a comment' . PHP_EOL . ' * > description' . PHP_EOL . ' */'];
}

/**
* A comment written inside a generic type is read and given to the node the
* reading reaches first after it. Where no attributes are asked for there is
* no node to give it to, and it used to be left waiting for one until the
* whole PHPDoc had been read, which is where the reading gave up.
*
* @dataProvider dataCommentInsideGenericType
*/
public function testCommentInsideGenericTypeIsAlwaysFlushed(string $input): void
{
foreach ([[], ['lines' => true, 'indexes' => true, 'comments' => true]] as $usedAttributes) {
$config = new ParserConfig($usedAttributes);
$constExprParser = new ConstExprParser($config);
$typeParser = new TypeParser($config, $constExprParser);
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);

$tokens = new TokenIterator($this->lexer->tokenize($input));
$phpDocNode = $phpDocParser->parse($tokens);

$this->assertCount(1, $phpDocNode->children);
$this->assertSame(Lexer::TOKEN_END, $tokens->currentTokenType());
}
}

}
68 changes: 68 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,74 @@ public function provideParseData(): array
),
),
],
[
'?(?Foo)',
new NullableTypeNode(
new NullableTypeNode(
new IdentifierTypeNode('Foo'),
),
),
],
[
'?(?(Foo|Bar))',
new NullableTypeNode(
new NullableTypeNode(
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
),
),
],
[
'Foo[?(?Bar)]',
new OffsetAccessTypeNode(
new IdentifierTypeNode('Foo'),
new NullableTypeNode(
new NullableTypeNode(
new IdentifierTypeNode('Bar'),
),
),
),
],
[
'((?Foo) is Bar ? true : false)',
new ConditionalTypeNode(
new NullableTypeNode(
new IdentifierTypeNode('Foo'),
),
new IdentifierTypeNode('Bar'),
new IdentifierTypeNode('true'),
new IdentifierTypeNode('false'),
false,
),
],
[
'((Foo | Bar) is Baz ? true : false)',
new ConditionalTypeNode(
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
new IdentifierTypeNode('Baz'),
new IdentifierTypeNode('true'),
new IdentifierTypeNode('false'),
false,
),
],
[
'((Foo & Bar) is Baz ? true : false)',
new ConditionalTypeNode(
new IntersectionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
new IdentifierTypeNode('Baz'),
new IdentifierTypeNode('true'),
new IdentifierTypeNode('false'),
false,
),
],
[
'(T is Foo ? true : T is Bar ? false : null)',
new ConditionalTypeNode(
Expand Down
94 changes: 94 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
Expand Down Expand Up @@ -300,6 +301,52 @@ public function enterNode(Node $node)
$changeReturnType,
];

$wrapNullableTypeInAnotherOne = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if (
$node instanceof NullableTypeNode
&& $node->type instanceof IdentifierTypeNode
&& $node->type->name === 'Foo'
) {
$node->type = new NullableTypeNode(new IdentifierTypeNode('Bar'));

return $node;
}

return $node;
}

};

yield [
'/** @return ?Foo */',
'/** @return ?(?Bar) */',
$wrapNullableTypeInAnotherOne,
];

$changeConditionalSubjectType = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof ConditionalTypeNode && $node->subjectType instanceof IdentifierTypeNode) {
$node->subjectType = new NullableTypeNode(new IdentifierTypeNode('Bar'));

return $node;
}

return $node;
}

};

yield [
'/** @return (Foo is Bar ? true : false) */',
'/** @return ((?Bar) is Bar ? true : false) */',
$changeConditionalSubjectType,
];

yield [
'/** @return Foo*/',
'/** @return Bar*/',
Expand Down Expand Up @@ -2773,6 +2820,53 @@ public function dataPrintType(): iterable
new ArrayTypeNode(new NullableTypeNode(new IdentifierTypeNode('Foo'))),
'(?Foo)[]',
];
yield [
new NullableTypeNode(new NullableTypeNode(new IdentifierTypeNode('Foo'))),
'?(?Foo)',
];
yield [
new OffsetAccessTypeNode(
new IdentifierTypeNode('Foo'),
new NullableTypeNode(new NullableTypeNode(new IdentifierTypeNode('Bar'))),
),
'Foo[?(?Bar)]',
];
yield [
new ConditionalTypeNode(
new NullableTypeNode(new IdentifierTypeNode('Foo')),
new IdentifierTypeNode('Bar'),
new IdentifierTypeNode('true'),
new IdentifierTypeNode('false'),
false,
),
'((?Foo) is Bar ? true : false)',
];
yield [
new ConditionalTypeNode(
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
new IdentifierTypeNode('Baz'),
new IdentifierTypeNode('true'),
new IdentifierTypeNode('false'),
false,
),
'((Foo|Bar) is Baz ? true : false)',
];
yield [
new ConditionalTypeNode(
new IntersectionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
new IdentifierTypeNode('Baz'),
new IdentifierTypeNode('true'),
new IdentifierTypeNode('false'),
false,
),
'((Foo&Bar) is Baz ? true : false)',
];
yield [
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
Expand Down
Loading