GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — TypeParser - Handle union on nullable by Korbeil · Pull Request #86 · phpstan/phpdoc-parser

Conversation

@Korbeil

@Korbeil Korbeil commented Oct 26, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

I was working on symfony/symfony#40457 to introduce this parser as the default extractor for Symfony. And one of the actual tests (https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php#L86-L89) contains a PhpDoc with @var ?string|int.
And the only remaining issue I have on this extractor is that PhpStan can't parse correctly nullable union types !

Here is the output with the latest version of phpstan/phpdoc-parser:

^ PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode^ {#358
  +children: array:1 [
    0 => PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode^ {#316
      +name: "@var"
      +value: PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode^ {#399
        +value: "?string|int"
        +exception: PHPStan\PhpDocParser\Parser\ParserException^ {#398
          -currentTokenValue: "|"
          -currentTokenType: 1
          -currentOffset: 23
          -expectedTokenType: 27
          #message: "Unexpected token "|", expected TOKEN_OTHER at offset 23"
          #code: 0
          #file: "./vendor/phpstan/phpdoc-parser/src/Parser/TokenIterator.php"
          #line: 183
          trace: {
            ./vendor/phpstan/phpdoc-parser/src/Parser/TokenIterator.php:183 { …}
            ./vendor/phpstan/phpdoc-parser/src/Parser/TokenIterator.php:80 { …}
            ./vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php:438 { …}
            ./vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php:220 { …}
            ./vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php:113 { …}
            ./vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php:92 { …}
            ./vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php:53 { …}
            ./vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php:38 { …}
            ./src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php:233 {
              Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor->getDocBlockFromProperty(string $class, string $property): PhpDocNode^
              › $tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
              › $phpDocNode = $this->phpDocParser->parse($tokens);
              › dd($phpDocNode);
            }
            ./src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php:199 { …}
            ./src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php:73 { …}
            ./src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpStanExtractorTest.php:184 { …}
            ./.phpunit/phpunit-9.5-0/src/Framework/TestCase.php:1528 { …}
            ./.phpunit/phpunit-9.5-0/src/Framework/TestCase.php:1134 { …}
            ./.phpunit/phpunit-9.5-0/src/Framework/TestResult.php:722 { …}
            ./.phpunit/phpunit-9.5-0/src/Framework/TestCase.php:886 { …}
            ./.phpunit/phpunit-9.5-0/src/Framework/TestSuite.php:678 { …}
            ./.phpunit/phpunit-9.5-0/src/Framework/TestSuite.php:678 { …}
            ./.phpunit/phpunit-9.5-0/src/Framework/TestSuite.php:678 { …}
            ./.phpunit/phpunit-9.5-0/src/TextUI/TestRunner.php:670 { …}
            ./.phpunit/phpunit-9.5-0/src/TextUI/Command.php:143 { …}
            ./.phpunit/phpunit-9.5-0/src/TextUI/Command.php:96 { …}
            ./.phpunit/phpunit-9.5-0/phpunit:22 { …}
            ./vendor/symfony/phpunit-bridge/bin/simple-phpunit.php:430 { …}
            ./vendor/symfony/phpunit-bridge/bin/simple-phpunit:13 { …}
            ./phpunit:28 { …}
          }
        }
      }
    }
  ]
}

So this PR introduce a fix to make this parser work correctly with nullable union types

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Korbeil
Korbeil force-pushed the feature/phpdoc-handle-union-on-nullable branch from 1121c9c to 15d0179 Compare October 26, 2021 19:02
JanTvrdik
JanTvrdik reviewed Oct 26, 2021
View reviewed changes
Comment thread src/Parser/TypeParser.php Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Korbeil
TypeParser - Handle union on nullable
ffa44f7
@Korbeil
Korbeil force-pushed the feature/phpdoc-handle-union-on-nullable branch from 15d0179 to ffa44f7 Compare October 26, 2021 20:03
@Korbeil

Korbeil commented Oct 27, 2021

Copy link
Copy Markdown
Author

I didn't tested it and now I see that is no valid syntax (see https://3v4l.org/r7KOM) so I can understand the 👎
So I'm not sure if I should keep this PR open, I'll think about it and come back later 😅

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ondrejmirtes

ondrejmirtes commented Oct 27, 2021

Copy link
Copy Markdown
Member

Yeah, it's definitely better to update the Symfony code in this case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Korbeil

Korbeil commented Oct 27, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

Closing this PR as this is no valid PHP syntax, we'll just ignore this syntax in the Symfony extractor.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Korbeil Korbeil closed this Oct 27, 2021
@valzargaming valzargaming mentioned this pull request Sep 14, 2026
TypeParser: read the "|" that follows a "?" #321
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JanTvrdik JanTvrdik JanTvrdik left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Korbeil @ondrejmirtes @JanTvrdik
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.