Skip to content

Commit

Permalink
Merge branch 'array-shape-comments' of github.com:shmax/phpdoc-parser…
Browse files Browse the repository at this point in the history
… into array-shape-comments
  • Loading branch information
shmax committed Oct 1, 2023
2 parents bd80a83 + b9b23e5 commit 719be61
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/apiref.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/backward-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
Expand Down Expand Up @@ -53,10 +53,10 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Checkout build-cs"
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: "phpstan/build-cs"
path: "build-cs"
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/create-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.PHPSTAN_BOT_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/merge-maintained-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: "Merge branch"
uses: everlytic/branch-merge@1.1.5
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Generate changelog
id: changelog
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/send-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
php-version: "8.1"

- name: "Checkout phpstan-src"
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: phpstan/phpstan-src
path: phpstan-src
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-slevomat-coding-standard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Checkout Slevomat Coding Standard"
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: slevomat/coding-standard
path: slevomat-cs
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ parameters:
count: 1
path: src/Ast/NodeTraverser.php

-
message: "#^Strict comparison using \\=\\=\\= between 2 and 2 will always evaluate to true\\.$#"
count: 2
path: src/Ast/NodeTraverser.php

-
message: "#^Variable property access on PHPStan\\\\PhpDocParser\\\\Ast\\\\Node\\.$#"
count: 1
Expand Down
4 changes: 1 addition & 3 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,15 +1127,13 @@ private function parseAssertParameter(TokenIterator $tokens): array
{
if ($tokens->isCurrentTokenType(Lexer::TOKEN_THIS_VARIABLE)) {
$parameter = '$this';
$requirePropertyOrMethod = true;
$tokens->next();
} else {
$parameter = $tokens->currentTokenValue();
$requirePropertyOrMethod = false;
$tokens->consumeTokenType(Lexer::TOKEN_VARIABLE);
}

if ($requirePropertyOrMethod || $tokens->isCurrentTokenType(Lexer::TOKEN_ARROW)) {
if ($tokens->isCurrentTokenType(Lexer::TOKEN_ARROW)) {
$tokens->consumeTokenType(Lexer::TOKEN_ARROW);

$propertyOrMethod = $tokens->currentTokenValue();
Expand Down
32 changes: 12 additions & 20 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,40 +413,32 @@ public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);

$startLine = $baseType->getAttribute(Ast\Attribute::START_LINE);
$startIndex = $baseType->getAttribute(Ast\Attribute::START_INDEX);
$genericTypes = [];
$variances = [];

[$genericTypes[], $variances[]] = $this->parseGenericTypeArgument($tokens);

$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);

while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$isFirst = true;
while ($isFirst || $tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
// trailing comma case
$type = new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances);
$startLine = $baseType->getAttribute(Ast\Attribute::START_LINE);
$startIndex = $baseType->getAttribute(Ast\Attribute::START_INDEX);
if ($startLine !== null && $startIndex !== null) {
$type = $this->enrichWithAttributes($tokens, $type, $startLine, $startIndex);
}

return $type;
// trailing comma case
if (!$isFirst && $tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
break;
}
$isFirst = false;

[$genericTypes[], $variances[]] = $this->parseGenericTypeArgument($tokens);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
}

$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);

$type = new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances);
$startLine = $baseType->getAttribute(Ast\Attribute::START_LINE);
$startIndex = $baseType->getAttribute(Ast\Attribute::START_INDEX);
if ($startLine !== null && $startIndex !== null) {
$type = $this->enrichWithAttributes($tokens, $type, $startLine, $startIndex);
}

$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);

return $type;
}

Expand Down Expand Up @@ -546,7 +538,7 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
return $this->parseNullable($tokens);

} elseif ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
$type = $this->parse($tokens);
$type = $this->subParse($tokens);
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
Expand Down
57 changes: 46 additions & 11 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4563,21 +4563,56 @@ public function provideAssertTagsData(): Iterator
];

yield [
'invalid $this',
'OK $this',
'/** @phpstan-assert Type $this */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-assert',
new InvalidTagValueNode(
'Type $this',
new ParserException(
'*/',
Lexer::TOKEN_CLOSE_PHPDOC,
31,
Lexer::TOKEN_ARROW,
null,
1
)
new AssertTagValueNode(
new IdentifierTypeNode('Type'),
'$this',
false,
''
)
),
]),
];

yield [
'OK $this with description',
'/** @phpstan-assert Type $this assert Type to $this */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-assert',
new AssertTagValueNode(
new IdentifierTypeNode('Type'),
'$this',
false,
'assert Type to $this'
)
),
]),
];

yield [
'OK $this with generic type',
'/** @phpstan-assert GenericType<T> $this */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-assert',
new AssertTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('GenericType'),
[
new IdentifierTypeNode('T'),
],
[
GenericTypeNode::VARIANCE_INVARIANT,
]
),
'$this',
false,
''
)
),
]),
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,18 @@ public function provideParseData(): array
]),
]),
],
[
'Closure(Container):($serviceId is class-string<TService> ? TService : mixed)',
new CallableTypeNode(new IdentifierTypeNode('Closure'), [
new CallableTypeParameterNode(new IdentifierTypeNode('Container'), false, false, '', false),
], new ConditionalTypeForParameterNode(
'$serviceId',
new GenericTypeNode(new IdentifierTypeNode('class-string'), [new IdentifierTypeNode('TService')], ['invariant']),
new IdentifierTypeNode('TService'),
new IdentifierTypeNode('mixed'),
false
)),
],
];
}

Expand Down

0 comments on commit 719be61

Please sign in to comment.