Skip to content

Implement a rule to check deprecated hook implementations #634

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

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

Kingdutch
Copy link
Contributor

This will analyse hook implementations in .inc and .module files. It detects a hook by looking at the pattern mymodule_[hook] which is detected from the file-name the function is in.

It will replace mymodule with hook and then try to find a function with that name. For this to work PHPStan Drupal's DrupalAutoloader class must be adjusted to load .api.php files as well so that the hooks can be discovered. In testing on the Open Social code-base this did not cause any issues.

The rule will provide the deprecation message in the error if it's provided, if no deprecation message was provided (i.e. a lonesome @deprecated tag) then the hook will simply be shown as deprecated without advice.

Fixes #126

@Kingdutch Kingdutch force-pushed the feature/hook-deprecations branch from 732c9a7 to 39183e8 Compare November 11, 2023 10:57
Copy link
Owner

@mglaman mglaman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! This looks great

public function processNode(Node $node, Scope $scope) : array
{
assert($node instanceof Function_);
if (!str_ends_with($scope->getFile(), ".module") && !str_ends_with($scope->getFile(), ".inc")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan-drupal still supports PHP 7.4 technically for Drupal 9. But we can trust the PHP 8 polyfill exists for D9, so it should be okay right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, Drupal 9 officially doesn't support 7.4 anymore ;)

Copy link
Owner

@mglaman mglaman Nov 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"officially" – or wait, does it actually have breaking code? Or still "supports" it via polyfill?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably no breaking code, haven't noticed, just bo promised I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used str_ends_with and str_starts_with for pretty much the entirety of Drupal 9's lifetime :D The symfony/polyfill-php80 package takes care of the functions (and Drupal depends on it).

Not sure if we need to add a dependency on that package explicitly in phpstan-drupal?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I think we're OK and I should probably just drop PHP 7.4. I was waiting for PHPStan to drop it, but the Drupal community has pretty much moved onto PHP 8.1 (it seems.)

Nothing to do here, though.

@mglaman
Copy link
Owner

mglaman commented Nov 11, 2023

I am surprised this works given

        excludePaths:
                - '*.api.php'

@mglaman
Copy link
Owner

mglaman commented Nov 11, 2023

@Kingdutch

For this to work PHPStan Drupal's DrupalAutoloader class must be adjusted to load .api.php files as well so that the hooks can be discovered. In testing on the Open Social code-base this did not cause any issues.

I don't see these changes? We'd have to add something here https://github.com/mglaman/phpstan-drupal/blob/main/src/Drupal/DrupalAutoloader.php#L110-L113 which is how we load post update files

@Kingdutch Kingdutch force-pushed the feature/hook-deprecations branch from 39183e8 to 90ae00c Compare November 11, 2023 18:26
@Kingdutch
Copy link
Contributor Author

Kingdutch commented Nov 11, 2023

Modified the DrupalAutoloader for .api.php discovery (I suspect I had that modified locally but it went missing because it wasn't in the open_social repo where I originally committed this change).

Moved the hook declarations to the fixture's .api.php file to prove that works :)

Edit: removed the stray dependencies: - from the .info.yml file since the entire fixture is a single module.

This will analyse hook implementations in `.inc` and `.module` files. It
detects a hook by looking at the pattern `mymodule_[hook]` which is
detected from the file-name the function is in.

It will replace `mymodule` with `hook` and then try to find a function
with that name. For this to work PHPStan Drupal's `DrupalAutoloader`
class must be adjusted to load `.api.php` files as well so that the
hooks can be discovered. In testing on the Open Social code-base
this did not cause any issues.

The rule will provide the deprecation message in the error if it's
provided, if no deprecation message was provided (i.e. a lonesome
`@deprecated` tag) then the hook will simply be shown as deprecated
without advice.
@Kingdutch Kingdutch force-pushed the feature/hook-deprecations branch from 90ae00c to ad2b929 Compare November 11, 2023 18:27
Copy link
Owner

@mglaman mglaman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing! I'll merge later today.

public function processNode(Node $node, Scope $scope) : array
{
assert($node instanceof Function_);
if (!str_ends_with($scope->getFile(), ".module") && !str_ends_with($scope->getFile(), ".inc")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I think we're OK and I should probably just drop PHP 7.4. I was waiting for PHPStan to drop it, but the Drupal community has pretty much moved onto PHP 8.1 (it seems.)

Nothing to do here, though.

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.

Deprecated hooks not flagged
3 participants