Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft Add HookConvert #308

Draft
wants to merge 42 commits into
base: main
Choose a base branch
from

Conversation

nlighteneddesign
Copy link

Description

Explain the technical implementation of the work done.

To Test

  • Add steps to test this feature

Drupal.org issue

Provide a link to the issue from https://www.drupal.org/project/rector/issues. If no issue exists, please create one and link to this PR.

@bbrala
Copy link
Collaborator

bbrala commented Oct 20, 2024

Thanks for working on this. A few things.

We don't need the whole config and setlist changes. We just need the rule, and we need some unit tests to cover the fule (hopefully those will work).

If this wont work, we need to add somefunctional testing, but lets try and avoid that. Unit tests are superior.

I'll push a few changes to help out (hopefully you allowed maintainer edits on the fork)

@bbrala
Copy link
Collaborator

bbrala commented Oct 20, 2024

I think we will need a new way to test this, since this also creates extra files. Not sure yet how, but i have some ideas.

@bbrala
Copy link
Collaborator

bbrala commented Oct 20, 2024

What you could do it create a few before and after files somewhere and share those. Perhaps a simple module would do with a few hooks that would be converted. I have a plan for functionally testing that. :)

@chx chx force-pushed the rectorOOPHooks branch 4 times, most recently from ac2f9cc to e0c637e Compare October 20, 2024 22:37
@nlighteneddesign
Copy link
Author

How would I run this locally? Since the last three commits it does not execute against core locally now. I think it's from 5fdfdbb

@bbrala
Copy link
Collaborator

bbrala commented Oct 21, 2024

You need to do the following steps:

composer config repositories.nlighteneddesign vcs https://github.com/nlighteneddesign/drupal-rector
composer require palantirnet/drupal-rector:dev-rectorOOPHooks

Then create a file rector.php with:

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
  // Adjust the set lists to be more granular to your Drupal requirements.
  // @todo find out how to only load the relevant rector rules.
  //   Should we try and load \Drupal::VERSION and check?
  $rectorConfig->rule(\DrupalRector\Rector\Convert\HookConvertRector::class);

  if (class_exists('DrupalFinder\DrupalFinderComposerRuntime')) {
    $drupalFinder = new \DrupalFinder\DrupalFinderComposerRuntime();
  } else {
    $drupalFinder = new \DrupalFinder\DrupalFinder();
    $drupalFinder->locateRoot(__DIR__);
  }
  $drupalRoot = $drupalFinder->getDrupalRoot();
  $rectorConfig->autoloadPaths([
    $drupalRoot . '/core',
    $drupalRoot . '/modules',
    $drupalRoot . '/profiles',
    $drupalRoot . '/themes'
  ]);

  $rectorConfig->skip(['*/upgrade_status/tests/modules/*']);
  $rectorConfig->fileExtensions(['php', 'module', 'theme', 'install', 'profile', 'inc', 'engine']);
  $rectorConfig->importNames(true, false);
  $rectorConfig->importShortClasses(false);
};

Then just run rector

vendor/bin/rector process core/modules/user/ --dry-run

@bbrala
Copy link
Collaborator

bbrala commented Oct 21, 2024

Magic ^^

image

@bbrala
Copy link
Collaborator

bbrala commented Oct 21, 2024

You could also use $rectorConfig->importShortClasses(true); so it will use imports for class names, but this will also mean you will get extra changes, like \Drupal::Xxx becomes Drupal::Xxx with an import.

@nlighteneddesign
Copy link
Author

Thanks! I'll try that.

@nlighteneddesign
Copy link
Author

That worked, but it's expanding all class names even with $rectorConfig->importShortClasses(true);

public function help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match)

Any other changes? The older version was importing them as expected if you review the user conversion issue: https://git.drupalcode.org/project/drupal/-/merge_requests/9890/diffs

@bbrala
Copy link
Collaborator

bbrala commented Oct 21, 2024

importNames might help there. Che k the docs of that method.

@bbrala
Copy link
Collaborator

bbrala commented Oct 25, 2024

A rebase would be great since that would fixup the tests.

@bbrala
Copy link
Collaborator

bbrala commented Oct 26, 2024

Rebased and fixed codestyle and some glaring phpstan issues.

@chx
Copy link

chx commented Oct 26, 2024

phpstan is wrong this is a php-parser v4 codebase not a v5 one. It doesn't work with v5 , I tried already.

@nlighteneddesign
Copy link
Author

@bbrala we had to revert your last two commits cause it broke the core vs contrib detection as well as some conversions

@bbrala
Copy link
Collaborator

bbrala commented Oct 26, 2024

Sorry :) thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants