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

User interface changes

API changes

Data model changes

Release notes snippet

Comments

dww created an issue. See original summary.

jibran’s picture

Issue tags: +Bug Smash Initiative

Tagging, as it was created as a result of BSI issue.

dww’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new672 bytes
new667 bytes

Apparently this is the way to do this:

   * @return ArrayIterator|\Symfony\Component\Routing\Route[]

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:

   * @return iterator|\Symfony\Component\Routing\Route[]

I never use an IDE, so I don't really know (or care). ;) Attaching both for comparison.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

quietone’s picture

Status: Needs review » Needs work

In migrate source plugins initializeIterator returns \ArrayIterator which has a return like this * @return \Iterator in the base class. Therefor, this should be right.

+++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php
@@ -97,7 +97,7 @@ public function getRoutesByPattern($pattern);
+   * @return iterator|\Symfony\Component\Routing\Route[]

s/iterator/\Iterator

ayushmishra206’s picture

Assigned: Unassigned » ayushmishra206
ayushmishra206’s picture

Assigned: ayushmishra206 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new687 bytes
new673 bytes

Made the changes suggested in #5. Please review.

ayushmishra206’s picture

StatusFileSize
new687 bytes

Reuploading the interdiff.txt

quietone’s picture

Status: Needs review » Needs work

@ayushmishra206, close but not quite what I was saying in #5.

+++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php
@@ -97,7 +97,7 @@ public function getRoutesByPattern($pattern);
+   * @return iterator|\Symfony\Component\Routing\Route[]

iterator should be \Iterator, with a backslash and capital I.

ayushmishra206’s picture

StatusFileSize
new674 bytes

Sorry for my lack of understanding. Updated the patch.

dww’s picture

Status: Needs work » Needs review

Looks fine to me, but maybe I shouldn't RTBC since I wrote the original patch?

quietone’s picture

Status: Needs review » Reviewed & tested by the community

@ayushmishra206, np. thanks for updating the patch.

Off we go!

catch’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php
@@ -97,7 +97,7 @@ public function getRoutesByPattern($pattern);
    *
-   * @return \Symfony\Component\Routing\Route[]
+   * @return \Iterator|\Symfony\Component\Routing\Route[]
    *   An iterator of routes keyed by route name.
    */

This looks to me like it's returning an iterator of route object arrays - should we be dropping the [] at the end?

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pooja saraah’s picture

StatusFileSize
new672 bytes
new979 bytes

Addressed the comment #13
Attached patch against Drupal 10.1.x

aziza_a’s picture

StatusFileSize
new1.36 MB

Applied patch given in comment #18 in Drupal 10.1.x successfully, image attached

andypost’s picture

There's https://www.drupal.org/u/needs-review-queue-bot to check patch applicability

@Aziza Anwari please don;t hide working patches

andypost’s picture

StatusFileSize
new604 bytes
new681 bytes

According to https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iter... it should be @return \Iterator<string, \Symfony\Component\Routing\Route>

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.48 KB

The 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.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new697 bytes
new680 bytes

ok, no spaces

/**
 * This class allows objects to work as arrays.
 * @link https://php.net/manual/en/class.arrayobject.php
 * @template TKey
 * @template TValue
 * @template-implements IteratorAggregate<TKey, TValue>
 * @template-implements ArrayAccess<TKey, TValue>
 */
class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable
{

    /**
     * Create a new iterator from an ArrayObject instance
     * @link https://php.net/manual/en/arrayobject.getiterator.php
     * @return ArrayIterator<TKey, TValue> An iterator from an <b>ArrayObject</b>.
     */
    #[TentativeType]
    public function getIterator(): Iterator {}
...
andypost’s picture

Issue summary: View changes
andypost’s picture

Status: Needs review » Needs work

NW for coder issue, it should start work with generics

FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 97 | ERROR | [x] Expected
    |       |     "\Iteratorstring\Symfony\Component\Routing\Route"
    |       |     but found
    |       |     "\Iterator<string,\Symfony\Component\Routing\Route>"
    |       |     for function return type
    |       |     (Drupal.Commenting.FunctionComment.InvalidReturn)
andypost’s picture

StatusFileSize
new42.59 KB

That's how it looks in PhpStorm

pooja saraah’s picture

StatusFileSize
new678 bytes

Fixed failed commands on #23
Attached patch against Drupal 10.1.x

pooja saraah’s picture

StatusFileSize
new609 bytes

Here is the interdiff patch against #23 and #27

akram khan’s picture

StatusFileSize
new1.67 KB
new1.51 KB

TRY to Fixed CCF #27

andypost’s picture

Why are you posting wrong patches?
Did you read comment 24 and articles from it?

andypost’s picture

Issue summary: View changes

Psalm using the same, added to summary https://psalm.dev/docs/annotating_code/templated_annotations/#builtin-te...

It smells like coding standards issue

rohan-sinha’s picture

StatusFileSize
new2.14 KB

Created a patch according to the article.

_pratik_’s picture

StatusFileSize
new2.61 KB
new2.43 KB

Fixed phpcs failure in #33

anchal_gupta’s picture

StatusFileSize
new2.99 KB
new2.99 KB

I have fixed the CCF error.

ameymudras’s picture

Status: Needs work » Needs review
StatusFileSize
new3 KB
new1 KB
andypost’s picture

+++ b/core/lib/Drupal/Core/Routing/RouteProvider.php
@@ -202,7 +202,7 @@
-    if (!empty($routes)) {
+    if (count($routes) == 0) {

+++ b/core/modules/system/src/Tests/Routing/MockRouteProvider.php
@@ -44,7 +44,7 @@
-    if (!empty($routes)) {
+    if (count($routes) == 0) {

it could be simplified to if (!$routes)

andypost’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php
@@ -67,9 +67,9 @@ public function getRouteByName($name);
+   * @return \Symfony\Component\Routing\Route

@@ -94,7 +94,7 @@ public function getRoutesByPattern($pattern);
-   * @return \Symfony\Component\Routing\Route[]
+   * @return \Symfony\Component\Routing\Route

that's wrong change

dww’s picture

Removing 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

rohan-sinha’s picture

StatusFileSize
new3.44 KB

Adding patch after considering all senerios.

dww’s picture

Title: Fix phpdoc return type for RouteProvider::getAllRoutes() » [PP-1] Fix phpdoc return type for RouteProvider::getAllRoutes()
Issue summary: View changes
Status: Needs work » Postponed
+++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php
@@ -27,9 +27,9 @@
+   * @return \Iteratorstring\Symfony\Component\Routing\RouteCollection

Again: 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>

dww’s picture

Opened #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

rohan-sinha’s picture

okay sure, i was too confused why phpcs showing this syntax, as I first tried to follow the article, thanks for raising the issue.

dww’s picture

Issue summary: View changes
dww’s picture

Title: [PP-1] Fix phpdoc return type for RouteProvider::getAllRoutes() » [PP-2] Fix phpdoc return type for RouteProvider::getAllRoutes()
Issue summary: View changes

After 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. 😅

quietone’s picture

Issue tags: +Coding standards

Adding tag.

andypost’s picture

Title: [PP-2] Fix phpdoc return type for RouteProvider::getAllRoutes() » [PP-1] Fix phpdoc return type for RouteProvider::getAllRoutes()

only coding standards issue left and upgrade of coder to 8.3.18

andypost’s picture

andypost’s picture

Using to upgrade coder to 8.3.18 - the patch passing locally

here's 2 patches - final one and fix + coder upgrade

andypost’s picture

StatusFileSize
new2.71 KB
new681 bytes

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.