Closed (fixed)
Project:
Drupal core
Version:
9.4.x-dev
Component:
other
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
14 Jan 2020 at 05:05 UTC
Updated:
15 Mar 2023 at 08:42 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
hardik_patel_12 commentedKindly review a patch.
Comment #3
hardik_patel_12 commentedComment #4
longwaveThis is safe to remove. This test was refactored in #2895271: Convert web tests to JTB and KTB tests for user module part-3 but the now-unused private function was not spotted at the time.
Comment #5
catchThis is often done intentionally for readbility. Let's remove it from this patch and open a coding standards issue to discuss if we want to.
Comment #6
avpadernoAs side note, Drupal 9.0.x cannot be tested with PHP 7.2, since it requires PHP 7.3.
Comment #7
avpadernoComment #8
avpadernoNever mind, I misread the code:
$account->access('create')is verifying the currently logged-in user is allowed to create$account, which is exactly what it's supposed to do.I apologize.
Comment #9
hash6 commentedthanks @Hardik_Patel_12 for the patch. Reviewed the patch, applied successfully.
Comment #10
hash6 commentedComment #11
alexpottThank 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, stage 1. A good place to start is the child issues of #2572645: [Meta] Fix 'Drupal.Commenting.FunctionComment' coding standard.
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.
As unused variables can be detecting via static analysis of the code potentially we could use https://github.com/sirbrillig/phpcs-variable-analysis + PHPCS to automatically check for unused variables and ensure that core doesn't have any. Doing this will be a bit of a big task because they are quite few patterns Drupal adopts that makes this tricky - for example hooks - even if the argument is not used it is best if it is declared on the hook implementation.
If someone want to take on the task of adding the above library as a dev dependency and configuring and getting it to run on core that'd be great.
I'm turning this issue into a plan as doing this requires a few steps.
If someone can update the issue summary with these steps and link this issue in the parent and open the issue for coder that'd be a great start. Also if someone can work on the upstream bugfix that'd be awesome.
Comment #12
alexpottI've done step one and improved the upstream library - see https://github.com/sirbrillig/phpcs-variable-analysis/pull/128.
Now we need to do step 2.
Comment #13
hash6 commentedComment #14
avpadernoI opened #3119378: Use PHP_CodeSniffer VariableAnalysis for static analysis, replace Coder unused variable rule with sirbrillig/phpcs-variable-analysis, but do not enable it, as described in comment #11.
Comment #15
xjmSince unused variables are dead code, this could probably be backported to the patch release branch. (Assuming they're actually unused and we don't introduce regressions.)
I actually consider unused variable removal to be a bit different from most codestyle cleanups, because they can point to other bugs. I usually like to use
git blameto figure out why the unused variable is there in the first place. And of course there's a minimum requirement of verifying the variable is actually unused, which has to be done on a case-by-case basis for each variable.Comment #16
avpaderno@xjm Do you mean a patch can be provided to remove the unused variables?
Comment #17
alexpott#3119378: Use PHP_CodeSniffer VariableAnalysis for static analysis, replace Coder unused variable rule with sirbrillig/phpcs-variable-analysis, but do not enable it has now updated coder to use the third party library - so we could start experimenting here by upgrading coder to the dev version and enabling the rule in core/phpcs.xml.dist
Comment #19
alexpottTools can determine where a variable is actually used. We've now update the tool used for this in coder so now we can try and apply it to core.
Comment #20
alexpottHere's a start of all non-test unused variables being fixed. Unfortunately I've identified another thing to fix - https://github.com/sirbrillig/phpcs-variable-analysis/issues/183 - before we can adopted this for core. As this is a coding standard change moving to the dev branch for now. It's likely we'll backport some of this to make cross branch patches simpler.
I think we can fix tests in a follow-up issue.
Comment #21
alexpottChanges like this are nice because assignments in ifs are head-scratchers.
Originally I was going to file an issue against the github project because an unused key in a foreach doesn't feel like an unused variable. But the more conversions I did the more convinced I became the removing them is correct. It adds to things for a developer to hold in their head whilst reviewing the code. And reducing this load is a good thing.
Also some long lines get a little bit shorter which is also good from a cognitive load perspective.
Comment #22
alexpottSo next steps here are:
Comment #23
alexpottComment #25
longwaveI've rerolled this and fixed a few more instances along with ignoring all the transliteration data as they are include files.
I also ignored DrupalPractice.CodeAnalysis.VariableAnalysis.UndefinedUnsetVariable as it generated a false positive for the unset() in:
There are three false positives remaining with this patch:
color.inc is an include file, maybe we just have to explicitly ignore these. FieldUiTable looks like valid code to me and I think there must be still bugs in the analyser around references:
FormErrorHandler is also deliberate, perhaps we also have to ignore DrupalPractice.CodeAnalysis.VariableAnalysis.VariableRedeclaration.
While there is some value here I can't help but think there are too many false positives with this method. I think exploring more advanced static analysis tools like PHPStan or Psalm is probably a better option.
Comment #26
avpadernoThe patch seems not to pass a PHPCS check.
Comment #27
spokje// phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis.VariableRedeclarationshould take care of the above.Comment #28
spokjeTeaching
cspellabout the English language...Comment #29
avpadernoI apologize: Comment #25 made clear that was a false positive. There isn't anything to do to fix that, as it's a false positive.
Comment #30
spokjeReverted erroneous change.
Comment #31
spokjeIf that's the case, I would expect 2 more false positives after I've made the first one ignored.
Anyway, I've managed to get a few issues with the patch out of the way. I'm leaving this to await the return of @longwave now.
Comment #32
longwaveThanks for picking this up, fixed the remaining issues (some need ignoring) and also fixed up the
list()syntax as we don't need trailing commas.Comment #33
spokjeComment #34
bhumikavarshney commentedPatch #32, Working fine and Remove the unused variables from core
Adding after patch screenshot for reference.
Comment #35
spokjeThanks @BhumikaVarshney for checking, but the fact that TestBot returns with a green, all test passed message already confirmed that.
Moving 3106216-32.patch into a MR, for easier (at least for me... 😇) reviewing and commenting.
Since I myself created a patch for this issue I can't RTBC it, but some extra eyes never hurts?
Comment #37
spokje1 Remark about an out of scope issue, and 3 about something we _might_ wanna do.
Not bad :)
Comment #39
spokjeComment #40
daffie commentedGreat to see that we can remove so many unused variables! Really great. Makes core easier to understand!
Comment #41
spokjeComment #42
spokjeComment #43
spokjeComment #44
spokjeMerged latest commits from
9.4.x-devinto MR.Comment #45
daffie commentedAll the code changes look good to me.
The sniff gets activated and the testbot goes green.
For me it is RTBC.
Comment #46
alexpottCommitted 2f1c716 and pushed to 10.0.x and 9.4.x. Thanks!
Comment #51
jonathan1055 commentedChanging the parent issue, because the fixes here relate to a DrupalPractice sniff not a Drupal coding standard sniff
Comment #52
quietone commentedChanging parent again to a new parent for the relevant sniff.