|
| 1 | +Upgrading from phpstan/phpdoc-parser 1.x to 2.0 |
| 2 | +================================= |
| 3 | + |
| 4 | +### PHP version requirements |
| 5 | + |
| 6 | +phpstan/phpdoc-parser now requires PHP 7.4 or newer to run. |
| 7 | + |
| 8 | +### Changed constructors of parser classes |
| 9 | + |
| 10 | +Instead of different arrays and boolean values passed into class constructors during setup, parser classes now share a common ParserConfig object. |
| 11 | + |
| 12 | +Before: |
| 13 | + |
| 14 | +```php |
| 15 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 16 | +use PHPStan\PhpDocParser\Parser\ConstExprParser; |
| 17 | +use PHPStan\PhpDocParser\Parser\TypeParser; |
| 18 | +use PHPStan\PhpDocParser\Parser\PhpDocParser; |
| 19 | + |
| 20 | +$usedAttributes = ['lines' => true, 'indexes' => true]; |
| 21 | + |
| 22 | +$lexer = new Lexer(); |
| 23 | +$constExprParser = new ConstExprParser(true, true, $usedAttributes); |
| 24 | +$typeParser = new TypeParser($constExprParser, true, $usedAttributes); |
| 25 | +$phpDocParser = new PhpDocParser($typeParser, $constExprParser, true, true, $usedAttributes); |
| 26 | +``` |
| 27 | + |
| 28 | +After: |
| 29 | + |
| 30 | +```php |
| 31 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 32 | +use PHPStan\PhpDocParser\ParserConfig; |
| 33 | +use PHPStan\PhpDocParser\Parser\ConstExprParser; |
| 34 | +use PHPStan\PhpDocParser\Parser\TypeParser; |
| 35 | +use PHPStan\PhpDocParser\Parser\PhpDocParser; |
| 36 | + |
| 37 | +$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]); |
| 38 | +$lexer = new Lexer($config); |
| 39 | +$constExprParser = new ConstExprParser($config); |
| 40 | +$typeParser = new TypeParser($config, $constExprParser); |
| 41 | +$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); |
| 42 | +``` |
| 43 | + |
| 44 | +The point of ParserConfig is that over the course of phpstan/phpdoc-parser 2.x development series it's most likely going to gain new optional parameters akin to PHPStan's [bleeding edge](https://phpstan.org/blog/what-is-bleeding-edge). These parameters will allow opting in to new behaviour which will become the default in 3.0. |
| 45 | + |
| 46 | +With ParserConfig object, it's now going to be impossible to configure parser classes inconsistently. Which [happened to users](https://github.com/phpstan/phpdoc-parser/issues/251#issuecomment-2333927959) when they were separate boolean values. |
| 47 | + |
| 48 | +### Support for parsing Doctrine annotations |
| 49 | + |
| 50 | +This parser now supports parsing [Doctrine Annotations](https://github.com/doctrine/annotations). The AST nodes representing Doctrine Annotations live in the [PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine namespace](https://phpstan.github.io/phpdoc-parser/2.0.x/namespace-PHPStan.PhpDocParser.Ast.PhpDoc.Doctrine.html). |
| 51 | + |
| 52 | +### Whitespace before description is required |
| 53 | + |
| 54 | +phpdoc-parser 1.x sometimes silently consumed invalid part of a PHPDoc type as description: |
| 55 | + |
| 56 | +```php |
| 57 | +/** @return \Closure(...int, string): string */ |
| 58 | +``` |
| 59 | + |
| 60 | +This became `IdentifierTypeNode` of `\Closure` and with `(...int, string): string` as description. (Valid callable syntax is: `\Closure(int ...$u, string): string`.) |
| 61 | + |
| 62 | +Another example: |
| 63 | + |
| 64 | +```php |
| 65 | +/** @return array{foo: int}} */ |
| 66 | +``` |
| 67 | + |
| 68 | +The extra `}` also became description. |
| 69 | + |
| 70 | +Both of these examples are now InvalidTagValueNode. |
| 71 | + |
| 72 | +If these parts are supposed to be PHPDoc descriptions, you need to put whitespace between the type and the description text: |
| 73 | + |
| 74 | +```php |
| 75 | +/** @return \Closure (...int, string): string */ |
| 76 | +/** @return array{foo: int} } */ |
| 77 | +``` |
| 78 | + |
| 79 | +## Type aliases with invalid types are preserved |
| 80 | + |
| 81 | +In phpdoc-parser 1.x, invalid type alias syntax was represented as [`InvalidTagValueNode`](https://phpstan.github.io/phpdoc-parser/2.0.x/PHPStan.PhpDocParser.Ast.PhpDoc.InvalidTagValueNode.html), losing information about a type alias being present. |
| 82 | + |
| 83 | +```php |
| 84 | +/** |
| 85 | + * @phpstan-type TypeAlias |
| 86 | + */ |
| 87 | +``` |
| 88 | + |
| 89 | +This `@phpstan-type` is missing the actual type to alias. In phpdoc-parser 2.0 this is now represented as [`TypeAliasTagValueNode`](https://phpstan.github.io/phpdoc-parser/2.0.x/PHPStan.PhpDocParser.Ast.PhpDoc.TypeAliasTagValueNode.html) (instead of `InvalidTagValueNode`) with [`InvalidTypeNode`](https://phpstan.github.io/phpdoc-parser/2.0.x/PHPStan.PhpDocParser.Ast.Type.InvalidTypeNode.html) in place of the type. |
| 90 | + |
| 91 | +## Removal of QuoteAwareConstExprStringNode |
| 92 | + |
| 93 | +The class [QuoteAwareConstExprStringNode](https://phpstan.github.io/phpdoc-parser/1.23.x/PHPStan.PhpDocParser.Ast.ConstExpr.QuoteAwareConstExprStringNode.html) has been removed. |
| 94 | + |
| 95 | +Instead, [ConstExprStringNode](https://phpstan.github.io/phpdoc-parser/2.0.x/PHPStan.PhpDocParser.Ast.ConstExpr.ConstExprStringNode.html) gained information about the kind of quotes being used. |
| 96 | + |
| 97 | +## Removed 2nd parameter of `ConstExprParser::parse()` (`$trimStrings`) |
| 98 | + |
| 99 | +`ConstExprStringNode::$value` now contains unescaped values without surrounding `''` or `""` quotes. |
| 100 | + |
| 101 | +Use `ConstExprStringNode::__toString()` or [`Printer`](https://phpstan.github.io/phpdoc-parser/2.0.x/PHPStan.PhpDocParser.Printer.Printer.html) to get the escaped value along with surrounding quotes. |
| 102 | + |
| 103 | +## Text between tags always belongs to description |
| 104 | + |
| 105 | +Multi-line descriptions between tags were previously represented as separate [PhpDocTextNode](https://phpstan.github.io/phpdoc-parser/2.0.x/PHPStan.PhpDocParser.Ast.PhpDoc.PhpDocTextNode.html): |
| 106 | + |
| 107 | +```php |
| 108 | +/** |
| 109 | + * @param Foo $foo 1st multi world description |
| 110 | + * some text in the middle |
| 111 | + * @param Bar $bar 2nd multi world description |
| 112 | + */ |
| 113 | +``` |
| 114 | + |
| 115 | +The line with `some text in the middle` in phpdoc-parser 2.0 is now part of the description of the first `@param` tag. |
0 commit comments