In core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php, testCheckNamedRouteWithUpcastedValues() and testCheckNamedRouteWithDefaultValue() a variable is initialized to an empty array before adding items to the array.

On any PHP versions, the first line of the following code is not necessary.

$map = [];
$map[] = [
  'test_route_1',
  [],
  '/test-route-1',
];
$map[] = [
  'test_route_2',
  [],
  '/test-route-2',
];

There is no need to initialize the variable to an empty array and then adding items to the array.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Hardik_Patel_12 created an issue. See original summary.

Hardik_Patel_12’s picture

Status: Active » Needs review
FileSize
656 bytes

Kindly review a patch.

Hardik_Patel_12’s picture

Title: Unused local variables in testCheckNamedRouteWithUpcastedValues() » Unused local variables in testCheckNamedRouteWithUpcastedValues() and testCheckNamedRouteWithDefaultValue()
Issue summary: View changes
FileSize
1015 bytes
559 bytes
siddhant.bhosale’s picture

Assigned: Unassigned » siddhant.bhosale
siddhant.bhosale’s picture

Assigned: siddhant.bhosale » Unassigned
Status: Needs review » Reviewed & tested by the community

Hi have tested the patch and the test cases run as expected. The output of the test run is below:

vendor/bin/phpunit -c core/ --filter AccessManagerTest::testCheckNamedRouteWithUpcastedValues
PHPUnit 8.5.5 by Sebastian Bergmann and contributors.

Testing
.                                                                   1 / 1 (100%)

Time: 23.57 seconds, Memory: 833.00 MB

OK (1 test, 5 assertions)
vendor/bin/phpunit -c core/ --filter AccessManagerTest::testCheckNamedRouteWithDefaultValue
PHPUnit 8.5.5 by Sebastian Bergmann and contributors.

Testing
.                                                                   1 / 1 (100%)

Time: 24.29 seconds, Memory: 833.00 MB

OK (1 test, 5 assertions)

Looks good to be merged.

alexpott’s picture

Status: Reviewed & tested by the community » Closed (duplicate)
Related issues: +#3106216: Remove unused variables from core

Thank you for your work on cleaning up Drupal core's code style!

In order to fix core coding standards in a maintainable way, all our coding standards issues should be done on a per-rule basis across all of core, rather than fixing standards in individual modules or files. We should also separate fixes where we need to write new documentation from fixes where we need to correct existing standards. This all should be done as part of #2571965: [meta] Fix PHP coding standards in core. A good place to for unused variables is #3106216: Remove unused variables from core.

For background information on why we usually will not commit coding standards fixes that aren't scoped in that way, see the core issue scope guidelines, especially the note about coding standards cleanups. That document also includes numerous suggestions for scoping issues including documentation coding standards cleanups.

Contributing to the overall plan above will help ensure that your fixes for core's coding standards remain in core the long term.

alexpott’s picture

Status: Closed (duplicate) » Needs work

In discussion with xim, catch and larowlan, my earlier comment is incorrect. We should handle each unused variable on its own merit and do the work to work out why it is not used.

This can point to broken code or incomplete testing see #3157369: Use unused variable $filters from DateTimeSchemaTest for example. A useful tool for this is git log -S “SOME TEXT” which will search git commits for matching text to find out when the variable might have become unused. Without doing the work to show why the variable is unused the patch will not be committed. Also git blame can be useful as well.

apaderno’s picture

Issue summary: View changes
apaderno’s picture

Issue summary: View changes

I edit the IS, since there aren't unused variables, but a variable that is first initialized to an empty array before adding items to the array.

apaderno’s picture

Title: Unused local variables in testCheckNamedRouteWithUpcastedValues() and testCheckNamedRouteWithDefaultValue() » Avoid initializing a local variable to an empty array before adding items to that array
apaderno’s picture

Issue summary: View changes
paulocs’s picture

Status: Needs work » Reviewed & tested by the community

I spent a little time to find when the variables was inserted and they were committed in the issue #2181293: AccessManager::checkNamedRoute() is not passing all route defaults (or building a complete route request).
It is no problem to remove them and it would be nice to clean the code.

Patch #3 looks good to me, so I'll set to RTBC.

Cheers, Paulo.

alexpott’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 63c85acafa to 9.1.x and 4d4753ff35 to 9.0.x and 103a1dc599 to 8.9.x. Thanks!

As this is a test backported to 8.9.x to keep the tests aligned.

  • alexpott committed 63c85ac on 9.1.x
    Issue #3156040 by Hardik_Patel_12, kiamlaluno, siddhant.bhosale, paulocs...

  • alexpott committed 4d4753f on 9.0.x
    Issue #3156040 by Hardik_Patel_12, kiamlaluno, siddhant.bhosale, paulocs...

  • alexpott committed 103a1dc on 8.9.x
    Issue #3156040 by Hardik_Patel_12, kiamlaluno, siddhant.bhosale, paulocs...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.