Problem/Motivation
Core supports OOP hooks
We can automate webform
Steps to reproduce
Checkout that branch in an 11.x version of drupal in ddev
Add this script https://gitlab.com/-/snippets/4769802 to ddev and run it
yes to both questions
Commit rector changes
Find and replace // phpcs:ignore with something easily found (since it breaks codefix) I used // elephant
Run codefix in ddev
./vendor/bin/phpcbf \
--standard="Drupal,DrupalPractice" -n \
--extensions="php,module,inc,install,test,profile,theme" \
web/modules/contrib/webform
Find and replace // elephant for // phpcs:ignore to swap it back
Commit
ddev composer require --dev friendsofphp/php-cs-fixer
#!/bin/bash
## Description: Sniff code
## Usage: cdu
## Example: cdu
./vendor/bin/php-cs-fixer fix \
web/modules/contrib/webform \
--rules=no_unused_imports
Commit and push
Proposed resolution
Approach 1
Run rector
Check tests
Add patches to fix any testing / functional issues
Pick date for merge
Merge
Remaining tasks
Questions for maintainers
- Are you willing to commit a conversion in one issue, it can be less disruptive for a one time conversion like we achieved with core.
- If you prefer several smaller issues how would you like to break this up.
- I'm willing to do the legwork on rector and patching, would you be able to help track down some test failures
Other notes, when there are a lot of failures like in the latest test run it's usually something that can be fixed with Rector, but I want to settle on a process before investing too much time on this.
Review process
Review conversion
Review any test failures
Find out why LegacyHook is being reported as missing
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Issue fork webform-3487957
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:
- 3487957-rectorv4
changes, plain diff MR !589
- 3487957-convert-to-oop
changes, plain diff MR !548
- 3487957-convert2.0
changes, plain diff MR !549
- 3487957-print
changes, plain diff MR !550
- 3487957-rectorOnly
changes, plain diff MR !582
- 3487957-rectorv2
changes, plain diff MR !583
- 3487957-rectorv3
changes, plain diff MR !584
- rector4b
changes, plain diff MR !590
- 6.3.x
compare
Comments
Comment #5
nicxvan commentedComment #6
nicxvan commentedFor some reason some use statements are being duplicated.
Comment #7
nicxvan commentedComment #10
nicxvan commentedComment #11
nicxvan commentedSet to needs review for discussing the process with the maintainers.
Comment #12
nicxvan commentedComment #13
jrockowitz commentedI think we need an MR to 6.3.x that sees what tests are failing. Depending on the number of tests failing we could commit this to 6.3.x or wait until 6.4.x.
Comment #14
nicxvan commentedI can work on this again, the main thing is I wanted to confirm if you were willing to help track down the issues and do a single merge.
It might be a couple of days though.
Comment #15
jrockowitz commentedLet's see what the issues are before we manually fix them. I am assuming you are using Drupal Rector.
Comment #16
nicxvan commentedYes, more specifically the script here: https://gitlab.com/-/snippets/4769802
Comment #18
nicxvan commentedI've done no code sniffing or manual changes, here is just running rector.
https://git.drupalcode.org/project/webform/-/merge_requests/582/diffs
Comment #19
nicxvan commentedI think I have a fix for the comments being deleted thanks to @ghost of drupal past
Comment #24
nicxvan commentedSteps I took for rectorv3
Checkout that branch in an 11.x version of drupal in ddev
Add this script https://gitlab.com/-/snippets/4769802 to ddev and run it
yes to both questions
Commit rector changes
Find and replace
// phpcs:ignorewith something easily found (since it breaks codefix) I used// elephantRun codefix in ddev
Find and replace
// elephantfor// phpcs:ignoreto swap it backCommit and push
3 known codesniff issues
1. unused use statements, rector expands arguments - not sure why codefix isn't fixing these
2. t() in classes
3. \Drupal in classes, rector doesn't do injection
Comment #25
nicxvan commentedComment #29
nicxvan commentedrectorv4 is the MR to review just waiting on tests. I updated the script to handle the unused use statements too.
Comment #31
jrockowitz commentedThis looks very promising. If you are able to get an MR that has all the tests passing and phpcs is okay(ish). We should make the commit.
Things like dependency injection can be fixed later (or never).
Comment #32
nicxvan commentedSounds great! The only failures I see other than code sniffing are deprecations that are on HEAD so I assume those can be ignored.
I can look at all of the code sniff failures besides t() and dependency injection.
I can create a manual patch for those I think this week.
Comment #33
liam morlandThe phpcs checks were passing two days ago on 6.3.x, so they ought to keep passing.
Comment #34
nicxvan commentedThe issue is twofold.
t() in .module doesn't throw phpcs errors but it does in classes.
Drupal service calls are the same.
Rector doesn't address these so they have to be manually addressed.
I need to still review the other cs failures.
Would you be willing to having a temporary skip rule for those two?
Comment #35
nicxvan commentedI spent a couple of hours going through, still got a lot to go.
I'd like confirmation on whether we can skip the t() and injections.
Comment #37
liam morlandI'm inclined that we should not go backwards on our coding standards compliance.
Why the new merge request? What is different?
Comment #38
nicxvan commentedI am testing a different tool for addressing the arrays.
As I mentioned it's not going backwards.
These are patterns that exist in the procedural code but get flagged in classes.
Comment #39
liam morlandWhat I mean by going backwards is not that the code is getting worse (it's not). I just mean that currently phpcs passes and with this merge request it doesn't.
Comment #41
nicxvan commentedI just noticed we already have an ignore rule for \Drupal calls, it's just not taking effect, let me look at that. If that were in place this would be far more manageable.
Comment #42
nicxvan commentedJust to add some context here, this is a bulk rector pass, conflicts for auto generated commits like this need to be just scrapped and redone, they are not like normal conflicts where you can resolve them since it's bulk copying functions to methods. The more manual work on top of that the more fragile it becomes.
I'm willing to commit to resolving these codesniff rules in follow ups, they are generally not complex to fix and once this is in those issues could be rebased with a normal workflow.
The risk of conflict is much lower.
CS is passing now with rules addressing t() \Drupal and one DateFormat calls.
Comment #43
jrockowitz commentedI completely understand that we must skip fixing the dependency injections.
If you are saying in a later commit, we can automatically fix the t() calls since all that is required is the trait and then a search and replace, this is good enough to be committed.
Comment #44
nicxvan commentedNeither of those will be fixed automatically, but I am committing to working on both of those follow ups if this gets committed here.
The thing is I can manage and rebase those two fixes in a follow up (or more likely separate followups) in a more standard manner once this is in.
Created:
#3500968: Address t() in new hook classes so they use StringTranslationTrait
#3500969: Add Dependency injection to Hook classes
Comment #45
liam morlandThis is a very big patch and I am feeling some pressure to have full release for 6.3.0. Perhaps we should put this onto a 6.4.x branch.
Comment #46
nicxvan commentedWhile it is a big patch, this is the same process used for core to convert, and we can spot check that the conversions are accurate. Since this is rector it's really just copying the hooks to a hook class an method.
Webform also has more tests than most modules so we can have more confidence than most modules would have that this won't introduce systemic issues.
Comment #47
nicxvan commentedOk code sniff is fixed and I fixed all php stan issues for the new hook classes (Except one for bootstrap because bootstrap isn't installed in that case)
This should be ready to go.
Comment #48
jrockowitz commentedI completely agree with you that this should not introduce systemic issues and merging it would nudge more contrib modules to use OOP hooks.
Comment #49
nicxvan commentedI checked the merge and saw no changes to the hooks, I pushed a fix for the sniff errors.
Comment #51
jrockowitz commentedYour persistence has paid off. I am at DrupalCon is time to merge this. Thank you!!!
Comment #52
nicxvan commentedThanks! Glad I could help!