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

Update wp hooks dependency renamed wp 6.0 and aliases #37

Conversation

kkmuffme
Copy link
Collaborator

This also partially adds the dynamic hook checks requested in #12

It's possible to infer all dynamic hooks easily, but this requires a bit more changes I can only PR once my other PRs are merged to avoid having to rebase my PR and fixing tons of conflicts

@kkmuffme kkmuffme force-pushed the update-wp-hooks-dependency-renamed-WP-6.0-and-aliases branch from ea9914d to 7b1c7ab Compare July 11, 2022 11:25
@ntwb
Copy link
Contributor

ntwb commented Oct 28, 2022

Let's get #41 merged and then update this PR to merge

* update wp-hooks dependency to new name and new version
* new wp-hooks version includes hooks for WP 6.0
* new wp-hooks include non-dynamic aliases for documented dynamic hooks, which allows for checking functions hooked on those dynamic hooks
new wp-hooks include non-dynamic aliases for documented dynamic hooks, which allows for checking functions hooked on those dynamic hooks
@kkmuffme kkmuffme force-pushed the update-wp-hooks-dependency-renamed-WP-6.0-and-aliases branch from 5a998e8 to c010c79 Compare November 10, 2022 19:00
@kkmuffme
Copy link
Collaborator Author

@mcaskill currently waits for php-stubs/wp-cli-stubs#11, will merge once released there

@kkmuffme kkmuffme merged commit 8b88f94 into psalm:master Nov 10, 2022
@kkmuffme kkmuffme deleted the update-wp-hooks-dependency-renamed-WP-6.0-and-aliases branch November 10, 2022 19:28
@mcaskill mcaskill linked an issue Nov 14, 2022 that may be closed by this pull request
@paulshryock
Copy link

@kkmuffme what's the state of dynamic hooks? Either from WordPress core or from third-party plugins. Is there some way I can stub dynamic hooks?

In my psalm.xml, I have a block like this:

<plugins>
	<pluginClass class="PsalmWordPress\Plugin">
		<hooks>
			<directory name="stubs/hooks" recursive="true" />
		</hooks>
	</pluginClass>
</plugins>

And then I have stubs/hooks/actions.json and stubs/hooks/filters.json, which look like this:

{
	"$schema": "https://raw.githubusercontent.com/wp-hooks/generator/0.9.0/schema.json",
	"hooks": []
}

So I'm familiar with how to stub out individual hooks, but curious what is the recommended approach for dynamic hooks specifically. Or if that's not yet implemented, just looking for a status update.

Thanks!

@kkmuffme
Copy link
Collaborator Author

kkmuffme commented Dec 2, 2023

@paulshryock just fyi dynamic hooks work now with #49
in both directions (the hook name itself is dynamic and/or the add action/filter hook name is dynamic)
If you could test/check that PR if it works for your hook and give feedback there please.

@paulshryock
Copy link

paulshryock commented Dec 2, 2023

Hi @kkmuffme, I've just tested #49 and created a small git repo.

https://github.com/paulshryock/test-psalm-wordpress-latest/blob/main/src/functions.php

composer show | grep psalm
humanmade/psalm-plugin-wordpress      dev-v5-compatibility-and-features faab6d5 WordPress stubs and plugin for Psalm.
vimeo/psalm                           5.16.0                                    A static analysis tool for finding errors in PHP applications

I added several dynamic hooks:

  • WordPress core hooks
  • Third-party plugin hooks
  • First-party hooks

And none of these throw a Psalm error! ❤️

Screenshot 2023-12-02 at 3 17 31 PM

But then I realized that it seems like dynamic hooks are not checked by Psalm at all, regardless of whether or not they are stubbed.

For example, this third-party hook did throw a Psalm error (HookNotFound) until I defined a stub for it (as expected 👍).

But this dynamic third-party hook which is not stubbed does not throw a Psalm error, and this dynamic first-party hook which is not stubbed does not throw a Psalm error.

Is this expected behavior?

@kkmuffme
Copy link
Collaborator Author

kkmuffme commented Dec 4, 2023

@paulshryock

  • it didn't support dynamic hooks for add_action, as that originally didn't make sense - fixed now
  • encapsed strings weren't supported, as we never used those, fixed now
  • non-stubbed didn't support dynamic hooks at all, as we have all stubbed, so I didn't implement that at the time (but did now)

Please check again now and let me know if it works as expected for you now.

@paulshryock
Copy link

paulshryock commented Dec 4, 2023

Hi @kkmuffme, I upgraded to the latest commit.

composer upgrade humanmade/psalm-plugin-wordpress

composer.lock:

-                "reference": "faab6d5e5ae4b1d7ed374ea040524121ed62ab29"
+                "reference": "2d71d096ac6e47354e381b2d110d754a8c311c47"

🥳 Now I get those two failures I was expecting:

vendor/bin/psalm
Target PHP version: 8.1 (inferred from current PHP version).
Scanning files...
Analyzing files...



ERROR: HookNotFound - src/functions.php:52:1 - Hook "ep_delete_{$slug}" not found
/**
 * Dynamic third-party action with no stub defined.
 *
 * This should probably throw a Psalm error, but it does not.
 */
add_action(
  "ep_delete_{$slug}",
  function(int $object_id, string $slug): void {
    print json_encode(['object_id' => $object_id, 'slug' => $slug]);
  },
  10,
  2,
);


ERROR: HookNotFound - src/functions.php:69:1 - Hook "{$verb}_some_{$noun}" not found
/*
 * First-party dynamic hook with no stub defined.
 *
 * This should probably throw a Psalm error, but it does not.
 */
add_filter(
  "{$verb}_some_{$noun}",
  function(string $value): string {
    return "hello {$value}!";
  },
  10,
  1,
);


------------------------------
2 errors found
------------------------------

Checks took 0.02 seconds and used 1.949MB of memory
No files analyzed
Psalm was able to infer types for 100% of the codebase

And here's what I see in my text editor (as expected):

Screenshot 2023-12-04 at 5 56 35 PM

This looks great! Thank you for the quick work on this; I feel this is an improved user experience.

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.

Dynamic hooks trigger HookNotFound
3 participants