Drupal version

Not in yet

MR on Github

https://github.com/palantirnet/drupal-rector/pull/308

Drupal Rector version

Not in yet

Problem / Motivation

When #3442009: OOP hooks using attributes and event dispatcher lands it would be nice to have a rector rule to convert the procedural hooks into OOP attributes.

I've moved the rule @ghostofdrupalpast wrote to the PR.

I need some advice on how to include this more properly as for now for testing I've just replaced all rules with only this rule.

This is probably fine for a one time or per module run for core. Once core deprecates procedural hooks we can move this to a normal deprecation.

This does need some work from #3461148: Drupal.WhiteSpace.ScopeIndent gets confused by attributes on a function. as well, if you look at the gist below you can see there are still some spacing issues: https://gitlab.com/-/snippets/3759543

Specifically look at the indentation of the function declarations after the attribute.
Also note that the attributes are fully expanded.

Issue fork rector-3481215

Command icon 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

    nicxvan created an issue. See original summary.

    nicxvan’s picture

    For local testing @bbrala mentioned this in slack:

    The test structure has a configured rule file which configures which rules to run. Check out the other tests for the structure. This is the same default structure as rector has itself.
    For locally, you could copy the default rector.php file and remove the sets and only add your rule.

    nicxvan’s picture

    I'm having trouble running this locally against announcements_feed.

    I am running this:

    ./vendor/bin/phpcbf \
      --standard="Drupal,DrupalPractice" -n \
      --extensions="php,module,inc,install,test,profile,theme" \
      core/modules/announcements_feed
    

    Here is a draft pr: https://github.com/palantirnet/drupal-rector/pull/308/files

    Locally I commented out the other rules just to test this one.

    I just get this:

    No fixable errors were found
    
    nicxvan’s picture

    Ok I got this working with the PR here:

    I need some advice on how to include this since it's not a deprecation yet, we can use this just for core for now.
    My Draft comments out all of the drupal deprecations just for the hook convert rule.

    I had to read the README here: https://github.com/palantirnet/drupal-rector-sandbox

    How I got this working locally in ddev:
    First require drupal-rector
    ddev composer require --dev palantirnet/drupal-rector
    I then copied my changes from the PR: https://github.com/palantirnet/drupal-rector/pull/308/file

    Create rector web command in ddev:

    #!/bin/bash
    
    ## Description: Fix code
    ## Usage: rector
    ## Example: rector
    
    ./vendor/bin/rector \
      --config ./vendor/palantirnet/drupal-rector/rector.php \
      process \
      core/modules/announcements_feed
    

    Run ddev rector

    Create codefix command in ddev:

    #!/bin/bash
    
    ## Description: Sniff code
    ## Usage: cdf
    ## Example: cdf
    
    ./vendor/bin/phpcbf \
      --standard="Drupal,DrupalPractice" -n \
      --extensions="php,module,inc,install,test,profile,theme" \
      core/modules/announcements_feed/src/Hook \
      core/modules/announcements_feed/announcements_feed.module
    

    See output:
    https://gitlab.com/-/snippets/3759543

    nicxvan’s picture

    Issue summary: View changes
    nicxvan’s picture

    Issue summary: View changes
    nicxvan’s picture

    You can also see a more full test against the user module here: #3481582: [ignore] Convert User module and submodules to use OOP hooks.

    nicxvan’s picture

    You can also run ddev composer phpcbf

    nicxvan’s picture

    Status: Active » Needs review

    I could use some advice on how to inject this as a separate convert command for core.

    We will likely want to add this for contrib too once procedural hooks are deprecated, but we have some time for that.

    It's obviously not ready for merging, but it's probably close enough for an approach review.

    There are several warnings in the rule that I can look at separately.

    bbrala’s picture

    Issue summary: View changes
    nicxvan’s picture

    Thanks for taking a look, the extra changes were so I could run it locally until I got your advice.

    If you want to see it run against the user module you can see it here: https://git.drupalcode.org/project/drupal/-/merge_requests/9890/diffs

    joachim’s picture

    Should this be closed? The PR at https://github.com/palantirnet/drupal-rector/pull/308 says it's been merged.

    bbrala’s picture

    Status: Needs review » Fixed

    You're right! Thanks

    Now that this issue is closed, review the contribution record.

    As a contributor, attribute any organization that helped you, or if you volunteered your own time.

    Maintainers, credit people who helped resolve this issue.

    Status: Fixed » Closed (fixed)

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