Title: Bugs, shortcomings, and other minor issues in the parser · Issue #343 · phpstan/phpdoc-parser · GitHub
Open Graph Title: Bugs, shortcomings, and other minor issues in the parser · Issue #343 · phpstan/phpdoc-parser
X Title: Bugs, shortcomings, and other minor issues in the parser · Issue #343 · phpstan/phpdoc-parser
Description: Hi! As you may recall, I work with parsers, and I've been using type grammars as a benchmark for my projects. While running benchmarks, developing a fully-fledged, semantically independent type parser, and performing fuzz testing, I enco...
Open Graph Description: Hi! As you may recall, I work with parsers, and I've been using type grammars as a benchmark for my projects. While running benchmarks, developing a fully-fledged, semantically independent type par...
X Description: Hi! As you may recall, I work with parsers, and I've been using type grammars as a benchmark for my projects. While running benchmarks, developing a fully-fledged, semantically independent type...
Opengraph URL: https://github.com/phpstan/phpdoc-parser/issues/343
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bugs, shortcomings, and other minor issues in the parser","articleBody":"Hi! As you may recall, I work with parsers, and I've been using type grammars as a benchmark for my projects.\n\nWhile running benchmarks, developing a [fully-fledged, semantically independent type parser](https://typelang.dev/), and performing fuzz testing, I encountered several issues in the current PHPStan type parser.\n\nFor your convenience, I have listed them below, ranging from clear-cut bugs to minor issues and potential feature requests.\n\nAll PHPStan claims were verified against `phpstan/phpdoc-parser` 2.4.1 (PHP 8.1) in two modes:\n\n- **raw**: `TypeParser::parse()` followed by a check that the whole input is consumed\n- **docblock**: the same string as `/** @return \u003ctype> */` through `PhpDocParser`\n\nIf necessary, I can split this into separate issues. Just let me know the relevant numbers, and I will move them into individual issues for your convenience.\n\n### 1. Variadic combined with optional\n\n```\ncallable(int ...=): void // accepted\ncallable(int ...$a=): void // accepted\n```\n\n**Current behavior**\n\nAccepted. `=` marks a parameter as optional, but a variadic parameter cannot be optional\n\n**Expected**\n\nA `...` together with `=` must be rejected\n\n\n### 2. Nested conditional types: then branch versus else branch\n\n```\n(T is int ? A : B is C ? D : E) // accepted, printed as (T is int ? A : (B is C ? D : E))\n(T is int ? (A is C ? D : E) : B) // accepted\n(T is int ? A is C ? D : E : B) // rejected: Unexpected token \"is\", expected ':'\n(T is int ? A : B|C is D ? E : F) // rejected: Unexpected token \"is\", expected ')'\n```\n\n**Current behavior**\n\nAn unparenthesized conditional type is allowed only in the *else* branch, the *then* branch needs parentheses.\n\n**Expected**\n\n1. If unparenthesized nesting is allowed, it is allowed in both branches\n2. (prefer) Otherwise only the parenthesized form is allowed\n\nThe rule is currently asymmetric and undocumented\n\n\n### 3. Line breaks inside a type\n\nhttps://phpstan.org/r/43dd2349-937d-4307-8156-18e219294ff0\n\nRejected:\n```\n\"int|\\nstring\"\n\"?\\nint\"\n\"callable(int $a)\\n: void\"\n\"callable(int $a) :\\nvoid\"\n\"(T is\\nint ? A : B)\"\n\"(T\\nis\\nint\\n?\\nA\\n:\\nB)\"\n```\n\nBut accepted:\n```\n\"int\\n|string\"\n\"array\u003c\\n int,\\n string\\n>\"\n\"array{\\n a: int,\\n b: string\\n}\"\n\"callable(\\n int $a\\n): void\"\n```\n\n**Current behavior**\n\nA line break works before `|` and inside `\u003c>`, `{}`, `()`, but not after `|`, after `?`, around the `:` of a callable, or inside a parenthesized conditional type. A tab is accepted in the same positions (`\"?\\tint\"`, `\"int\\t|\\tstring\"`).\n\n**Expected**\n\nA line break is equivalent to a space in every position between the tokens of a type, so a long union or a conditional type can be wrapped at any operator.\n\n\n### 4. Trailing comma in `...\u003cK, V>`\n\n```\narray{a: int, ...\u003cK,>} // rejected: Unexpected token \">\", expected type\narray{a: int, ...\u003cK>,} // accepted\nFoo\u003cint,> // accepted\ncallable\u003cT,>(): void // accepted\n```\n\n**Current behavior**\n\nA trailing comma is rejected inside the arguments of the unsealed part and accepted in every other list.\n\n**Expected**\n\nA trailing comma is accepted there too.\n\n\n### 5. Variance keywords: `covariant` as a name\n\n```\nFoo\u003ccovariant> // rejected\nFoo\u003ccontravariant> // rejected\nFoo\u003ccovariant covariant> // accepted\ncallable\u003ccovariant>(covariant): covariant // accepted\n```\n\n**Current behavior**\n\nThe same word `covariant` is parsed differently in different positions.\n\n**Expected**\n\nOne defined rule:\n- Either it is a reserved word everywhere\n- Or an ordinary name wherever no type follows it\n\n### 6. Number with an underscore right after the dot\n\n```\n1._5 // accepted, printed as 1.5\n```\n\n**Current behavior**\n\nAccepted.\n\n**Expected**\n\nRejected (Syntax Error). It is a syntax error in PHP\n\n\n### 7. Hyphen (`-`) and backslash (`\\\\`) in a class constant name\n\n```\nFoo::BAR-BAR\nFoo::BAR\\Baz\n```\n\n**Current behavior**\n\nAccepted, the hyphen and the backslash become part of the constant name.\n\n**Expected**\n\nRejected.\n\n\n### 8. Odd template names\n\n```\ncallable\u003cT\\X>(int): int\ncallable\u003c\\T>(int): int\ncallable\u003cT-X>(int): int\ncallable\u003cnon-empty-string>(int): int\ncallable\u003cint>(int): int\n\n// AND\n\n@template T\\X of A = Default\n@template non-empty-string of B = Default\n...etc\n```\n\n\n**Current behavior**\n\nAccepted, a template name may be anything, including a builtin or a pseudo type.\n\n**Expected**\n\nA template name is a single word without `\\` and `-`. A builtin name is either rejected or explicitly allowed.\n\n\n### 9. Conditional type without parentheses\n\nThis is a good solution that requires developers to be explicit. However, there are some debatable situations.\n\n```\n// Error, parentheses are required.\nFoo\u003cT is int ? A : B>\narray{a: T is int ? A : B}\n```\n\nAnd bugs (https://phpstan.org/r/853c0920-857a-4d8f-8ff0-384382af1dbf).\n\nThe type ends before `is`, the rest becomes the description:\n```\nT is int ? A : B // Silently \"truncates\" the type as \"T\", which can lead to problems\ncallable(): T is X ? A : B // Truncates to \"callable(): T\"\nC|T is int ? A : B // Truncates to \"C|T\"\n?T is int ? A : B // Truncates to \"?T\"\n```\n\nThe results are `T`, `callable(): T`, `(C | T)` and `?T`\n\n\n### 10. Space or line break before `[`, `{`, `\u003c`, `(`\n\n```\nT [K]\narray {a: int}\n\"array\\n{a: int}\"\n\"Foo\\n\u003cint>\"\n\"callable\\n(int): void\"\n```\n\nPHPStan truncates to `T`, `array`, `array`, `Foo`, `callable`. The rest becomes the description.\n\n**BUT** a space before `\u003c` is accepted (`array \u003cint>`, `Foo \u003cT> (T): T`), so the rule differs between brackets.\n\n\n### 11. Class constant on a variable\n\n(?) Feature request\n\n```\n$this::BAR\n$this::class\n$a::BAR\n```\n\n**Current behavior**\n\nSyntax error\n\n\n### 12. Space after the sign of a number\n\n```\n- 1\n- 42\n- 1.5\n```\n\n**Current behavior**\n\nSyntax error\n\n**Expected**\n\nIn PHP `- 42.0` is a valid expression.\n","author":{"url":"https://github.com/SerafimArts","@type":"Person","name":"SerafimArts"},"datePublished":"2026-11-10T00:29:43Z","url":"https://github.com/phpstan/phpdoc-parser/issues/343"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:65f797db-b14b-8df6-1bf3-8708910e4f82 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A3EE:4C4E6:BD183:1027FC:6B00628F |
| html-safe-nonce | 52e94826c0c84026a0897976d13f81de8bfd4f0ab0732212366fde70db911d1d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBM0VFOjRDNEU2OkJEMTgzOjEwMjdGQzo2QjAwNjI4RiIsInZpc2l0b3JfaWQiOiI1MTA4ODIzNTU0MTEyMTE5NDM5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 85db384e3a49a9d9fc7034d5c37e8b03fe083fea5af6a090e71f217f260b097c |
| hovercard-subject-tag | issue:6297557911 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/phpstan/phpdoc-parser/343/issue_layout |
| twitter:image | https://opengraph.githubassets.com/312ab4e9323199836c4373af3a161adf7640ea20c54bf88f9b4b6203d29f07d1/phpstan/phpdoc-parser/issues/343 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/312ab4e9323199836c4373af3a161adf7640ea20c54bf88f9b4b6203d29f07d1/phpstan/phpdoc-parser/issues/343 |
| og:image:alt | Hi! As you may recall, I work with parsers, and I've been using type grammars as a benchmark for my projects. While running benchmarks, developing a fully-fledged, semantically independent type par... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SerafimArts |
| hostname | github.com |
| expected-hostname | github.com |
| None | 25854482a1773a7c1ebb8be3d2bc1c6e61d82d78126be63b5044474a86d44d9e |
| turbo-cache-control | no-preview |
| go-import | github.com/phpstan/phpdoc-parser git https://github.com/phpstan/phpdoc-parser.git |
| octolytics-dimension-user_id | 16435710 |
| octolytics-dimension-user_login | phpstan |
| octolytics-dimension-repository_id | 111290081 |
| octolytics-dimension-repository_nwo | phpstan/phpdoc-parser |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 111290081 |
| octolytics-dimension-repository_network_root_nwo | phpstan/phpdoc-parser |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e4f5c239e31f1f680e6bbc419ff92d774da616ec |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width