Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Since we are now using PHP 7+ we can use List (array destructuring) assignment using the array syntax. Requires PHP >= 7.1. In the related issue it was suggested to make a new issue for this change.
-list($sample) = $array;
+[$sample] = $array;
How to reproduce
php-cs-fixer
Added a config this time so it will do everything at once.
git clone --branch '9.3.x' https://git.drupalcode.org/project/drupal.git
cd drupal
mkdir --parents tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
Now create the config file at tools/php-cs-fixer/.php-cs-fixer.dist.php
.
<?php
/*
* This document has been generated with
* https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0.0|configurator
* you can change this configuration by importing this file.
*/
$config = new PhpCsFixer\Config();
$finder = PhpCsFixer\Finder::create()
->in(['core', 'composer'])
->exclude('vendor')
->name('*.php')
->name('*.sh')
->name('*.theme')
->name('*.module')
->name('*.inc');
return $config
->setRules([
'list_syntax' => true,
])
->setFinder($finder);
Run the fixer
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix . -v --diff --config tools/php-cs-fixer/.php-cs-fixer.dist.php --path-mode=intersection
git add core
There is some manual work to fix some style issues like: ([$var,] and indentation faults).
Issue fork drupal-3222769
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
bbralaMR is incoming in a bit.
Comment #3
longwaveBig +1 to this as PHPStorm loves to make this change for you, that you then have to manually revert before posting patches.
Comment #5
longwaveSymfony ships a php-cs-fixer config, I wonder if we should consider doing the same and running it as part of our commit checks: https://github.com/symfony/symfony/blob/5.4/.php-cs-fixer.dist.php
Comment #6
bbralaDrupal uses php codesniffer right now for that. But yeah, Symfony does use php-cs-config.
Comment #7
bbralaTo be honest, extending the ruleset of the Drupal PHP CodeSniffer with these rules should the way to go, otherwise its quite the change we are looking at.
Comment #8
longwaveNothing to stop us using both, I think they both have value and do slightly different things.
Comment #9
bbralaYeah but would mean 2 different tools would start scanning the whole codebase, which might do conflicting things. But that is 'but but'-me talking. :)
Comment #10
bbralaComment #11
bbralaComment #12
bbralaPing :)
What would you need here?
Comment #13
longwaveLet's at least roll a patch that fixes all cases in core, not sure we should introduce PHP CS fixer here as well, that should probably be a separate issue.
Comment #14
bbralai'll update the mr tonight, reproducing is easy enough ;)
Guessing target should be 9.4.x?
Comment #15
longwaveIt will need to be committed to both branches, hopefully the same MR will apply cleanly to both.
Comment #19
bbralaI'll update the branches with proper codestyle later. Those are currently failing.
Comment #21
bbralaSeems 9.3.x diff is literally the same as the 9.4.x diff, so closing the separate MR
Comment #22
bbralaEverything is green, changes where automated for 95% setting to needs review. ^^
Comment #23
bbralaComment #24
longwaveThis looks close but there are a few more cases we should fix up:
Comment #26
alexpottI ran the following regex on core's codebase and the composer directory
(\W)list\(([^\)]*)\)
and replace$1\[$2\]
Comment #27
longwaveNo instances of
\Wlist\(
left in core so to me this is complete.Comment #28
MatroskeenI already asked in Slack, but I want to document it here:
1) Should we create a follow-up before it's committed?
2) Is it fine to introduce this change before having the actual sniff that will catch new changes coming with the old style? (I'm just wondering about the policy so I can proceed in the same way here #2960522: Use root-namespaced native functions for better opcache'd performance)
Thanks!
Comment #29
alexpottI don't think we should go out of our way to have a rule that says
[$var] = explode()
and notlist($var) = explode()
. At best it is preference. PHPStorm complicates the issue because it automatically converts this on save so you get unrelated change which why this definitely should go in and any discussion about PHPCS sniffs should go later.If we want we could add a sniff that extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP\DiscouragedFunctionsSniff and uses the same logic to report usage of the list() function.
Comment #30
alexpottDiscussed #28 and #29 with @Matroskeen on slack and he said
Comment #31
bbralaIn order to get that reviewed by some of the core ppl, a RTBC is a good status to be in ;) Perhaps setting it back is in order?
Edit: this comment was posted before reading the above on slack and here.
Comment #32
bbralaUpdate config and command to invlude .sh scripts and also the composer directory. This will minimize any fix work after the command is run. (for future reference).
Comment #33
larowlanSaving issue credits
Comment #35
larowlanReviewed this locally using git diff --color-words
Whilst it didn't apply cleanly after #3222251: [November 8, 2021] Replace all isset constructs with the null coalescing operator, it did apply with a three way merge.
Got to say it was much nicer to review than #3222251: [November 8, 2021] Replace all isset constructs with the null coalescing operator.
Committed 6d39cde and pushed to 9.4.x. Thanks!
To maximise the possibility of backporting applying cleanly, backported to 9.3.x
Wrote and published a change record.
Comment #37
xjmWe should add a phpcs rule enforcing this style choice.
Per @alexpott #3010032: Add dependency on slevomat/coding-standard includes a rule for this change.
Comment #38
bbralaNice one xjm :)