Problem/Motivation
#3158708: RouteProvider::getAllRoutes no longer returns an iterable, breaking BC fixed a BC break introduced in #2917331: Decouple from Symfony CMF that changed the return type for RouteProvider::getAllRoutes().
It used to return an Iterator. Then #2917331 changed it to an array. Then #3158708 changed it back.
However, the PHPDoc comment says:
* @return \Symfony\Component\Routing\Route[]
* An iterator of routes keyed by route name.
I thought [] meant an array, not an Iterator. How do you tell the difference in PHPDoc comments?
Related docs
- https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iter...
- https://psalm.dev/docs/annotating_code/templated_annotations/#builtin-te...
- https://github.com/JetBrains/phpstorm-stubs/commit/89f6911ddcc89dd6497ab...
Proposed resolution
Properly document RouteProvider::getAllRoutes() that it returns an Iterator, not an array.
The correct syntax would be:
@return \Iterator<string, \Symfony\Component\Routing\Route>
However, unfortunately core's coder sniffs don't understand that syntax (yet).
Remaining tasks
Figure out how to properly document something that returns an Iterator in PHPDoc.- Get community agreement on the new syntax at #3348310: Adopt the phpdoc standard for documenting collection (eg Iterator) key and value types
- Fix coder.module's core sniffs so that the legitimate syntax is allowed. This current issue is postponed until #3347758: [PP-1] Support the recommended phpdoc syntax for documenting Iterator return types is fixed and incorporated into core's sniffs.
- Update the docs for RouteProvider::getAllRoutes() accordingly.
- Reviews / refinements.
- RTBC.
- Commit.
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #50 | 3161129-49.patch | 681 bytes | andypost |
| #50 | 3161129-49-plus-coder.patch | 2.71 KB | andypost |
| #23 | 3161129-23.patch | 680 bytes | andypost |
Comments
Comment #2
jibranTagging, as it was created as a result of BSI issue.
Comment #3
dwwApparently this is the way to do this:
References:
https://stackoverflow.com/questions/22272280/proper-phpdoc-comment-for-i...
https://www.php.net/manual/en/arrayobject.getiterator.php
But this is also suggested as an alternative:
I never use an IDE, so I don't really know (or care). ;) Attaching both for comparison.
Comment #5
quietone commentedIn migrate source plugins initializeIterator returns \ArrayIterator which has a return like this
* @return \Iteratorin the base class. Therefor, this should be right.s/iterator/\Iterator
Comment #6
ayushmishra206 commentedComment #7
ayushmishra206 commentedMade the changes suggested in #5. Please review.
Comment #8
ayushmishra206 commentedReuploading the interdiff.txt
Comment #9
quietone commented@ayushmishra206, close but not quite what I was saying in #5.
iterator should be \Iterator, with a backslash and capital I.
Comment #10
ayushmishra206 commentedSorry for my lack of understanding. Updated the patch.
Comment #11
dwwLooks fine to me, but maybe I shouldn't RTBC since I wrote the original patch?
Comment #12
quietone commented@ayushmishra206, np. thanks for updating the patch.
Off we go!
Comment #13
catchThis looks to me like it's returning an iterator of route object arrays - should we be dropping the [] at the end?
Comment #18
pooja saraah commentedAddressed the comment #13
Attached patch against Drupal 10.1.x
Comment #19
aziza_a commentedApplied patch given in comment #18 in Drupal 10.1.x successfully, image attached
Comment #20
andypostThere's https://www.drupal.org/u/needs-review-queue-bot to check patch applicability
@Aziza Anwari please don;t hide working patches
Comment #21
andypostAccording to https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iter... it should be
@return \Iterator<string, \Symfony\Component\Routing\Route>Comment #22
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #23
andypostok, no spaces
Comment #24
andypostadded links to summary phpstan blog and https://github.com/JetBrains/phpstorm-stubs/commit/89f6911ddcc89dd6497ab...
Comment #25
andypostNW for coder issue, it should start work with generics
Comment #26
andypostThat's how it looks in PhpStorm
Comment #27
pooja saraah commentedFixed failed commands on #23
Attached patch against Drupal 10.1.x
Comment #28
pooja saraah commentedHere is the interdiff patch against #23 and #27
Comment #29
akram khanTRY to Fixed CCF #27
Comment #30
andypostWhy are you posting wrong patches?
Did you read comment 24 and articles from it?
Comment #31
andypostPsalm using the same, added to summary https://psalm.dev/docs/annotating_code/templated_annotations/#builtin-te...
It smells like coding standards issue
Comment #32
rohan-sinha commentedCreated a patch according to the article.
Comment #33
_pratik_Fixed phpcs failure in #33
Comment #34
anchal_gupta commentedI have fixed the CCF error.
Comment #35
ameymudras commentedComment #36
andypostit could be simplified to
if (!$routes)Comment #37
andypostthat's wrong change
Comment #38
dwwRemoving credit from all the non-productive patches/files that folks have uploaded here in the last few weeks. Please read the comments in the issue and meaningfully contribute to moving this issue forward if you want to be credited with helping fix this.
Thanks,
-Derek
Comment #39
rohan-sinha commentedAdding patch after considering all senerios.
Comment #40
dwwAgain: this is completely invalid syntax. I believe it's a bug in coder and the core "sniffs" that suggests this broken thing as "the right way".
Probably this issue needs to be postponed on a fix to the coder sniffs so that the legitimate phpdoc syntax for this is allowed:
@return \Iterator<string, \Symfony\Component\Routing\Route>Comment #41
dwwOpened #3347758: [PP-1] Support the recommended phpdoc syntax for documenting Iterator return types in the coder queue to actually address what @andypost is saying in #25.
No one should upload any more patches in this issue until coder is fixed to handle the valid syntax.
Thanks,
-Derek
Comment #42
rohan-sinha commentedokay sure, i was too confused why phpcs showing this syntax, as I first tried to follow the article, thanks for raising the issue.
Comment #43
dwwComment #44
dwwAfter discussion in #coding-standards in Slack, we also need a coding_standards issue about this change. So this is now blocked on #3348310: Adopt the phpdoc standard for documenting collection (eg Iterator) key and value types, too. 😅
Comment #45
quietone commentedAdding tag.
Comment #46
andypostonly coding standards issue left and upgrade of coder to 8.3.18
Comment #47
andypostComment #48
andypostComment #49
andypostUsing to upgrade coder to 8.3.18 - the patch passing locally
here's 2 patches - final one and fix + coder upgrade
Comment #50
andypost