Skip to content

TypeParser - Handle union on nullable - #86

Closed
Korbeil wants to merge 1 commit into
phpstan:masterfrom
Korbeil:feature/phpdoc-handle-union-on-nullable
Closed

Korbeil wants to merge 1 commit into
phpstan:masterfrom
Korbeil:feature/phpdoc-handle-union-on-nullable

Conversation

@Korbeil

@Korbeil Korbeil commented Oct 26, 2021

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

@Korbeil
Korbeil force-pushed the feature/phpdoc-handle-union-on-nullable branch from 1121c9c to 15d0179 Compare October 26, 2021 19:02
Comment thread src/Parser/TypeParser.php Outdated
@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 😅

@ondrejmirtes

Copy link
Copy Markdown
Member

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

@Korbeil

Korbeil commented Oct 27, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants