Comments (3)
PHP_INT_MAX
I do not like the usage of PHP_INT_MAX for the hook order, because it is not possible to remove them with another plugin
Why not? Actually, having PHP_INT_MAX
ensures you can remove the hook, because that's the latest possible hook.
In a plugin:
add_action('foo', 'bar', PHP_INT_MAX);
In another plugin:
remove_action('foo', 'bar', PHP_INT_MAX);
it works :) But, there's and issue: the remove_action
needs to happen after the add_action
. How do you ensures that? Sometimes that's easy sometimes not. Something that works pretty much always, is to use the same hook to remove the call, but with a earlier priority:
add_action('foo', fn() => remove_action('foo', 'bar', PHP_INT_MAX));
Thanks to the fact that PHP_INT_MAX
is very late, having something that runs before was pretty easy, and did not required a custom priority, considering the default 10
is much lower than PHP_INT_MAX
.
Besides that, removing an action always requires the exact same priority used to add the hook. if the hook is added with priority 32
, the removal has to use the 32
as well. And it isn't better to use a constant instead of an hardcoed value like 999
often used when people whant a later hook?
Regarding the fact that PHP_INT_MAX
depends on the PHP installation: that is totally unrelevant. The PHP binary that will process the add_action
will always be the same PHP binary that process the remove_action
, so in every given context the value of PHP_INT_MAX
is the same. (So, for example, if both add_action
and remove_action
uses PHP_INT_MAX
that will always work). And if the actual value in my machine is different than in your machine... how is that a problem?
The only issue with PHP_INT_MAX
is not that is not possible to remove hook that uses it, but that is not possible to execute something after that given callaback for the same hook. That is especially relevant for filters. For actions it is possible to use a later action to solve the problem. But imagine something like:
add_filter('the_title', 'edit_title', `PHP_INT_MAX`);
It will never be possible to reliably filter the title with something different, because the filter above will run as last one.
Even if there're cases in which that make sense, those are usually when the developer is aware of the whole system (website), single themes and plugins should always offer a way to change the behavior, considering the wider context is not known.
So I guess that a warning when PHP_INT_MAX
is used in add_filter
could make sense.
PHP_INT_MIN
What said above for PHP_INT_MAX
does not apply to PHP_INT_MIN
. An hook added with PHP_INT_MIN
could actually be hard to remove, because the trick of using the same hook won't work. E.g.:
add_action('foo', fn() => remove_action('foo', 'bar', PHP_INT_MIN));
There's no way to remove an hook"foo"
using the same "foo"
hook, if PHP_INT_MIN
was used as priority. Which means that we need to ensure in some other way that remove_action
is called after add_action
, and that might not always be possible.
SUMMARY
IMO we could:
- Raise a warning if
PHP_INT_MAX
is used foradd_filter
- Raise a warning if
PHP_INT_MIN
is used foradd_action
andadd_filter
from php-coding-standards.
I started to work on this, I ahve a part of the sniff done but nothing to commit yet. I have no time to complete it today.
from php-coding-standards.
I am going to close this. It has been implemented in #75.
from php-coding-standards.
Related Issues (20)
- Closure Turned Static Breaks Code HOT 1
- [Feature Request]: Add check for Emacs UTF-8 file header HOT 1
- [Question] When using phpcbf there is a bug for the rule that leaves the first line with opening php tag HOT 3
- Review Squiz.Scope.MethodScope.Missing rule in interfaces and PhpAnnotator conflict HOT 1
- [Feature Request]: Add check for whitespace after type casting HOT 3
- [Feature Request]: Bump squizlabs/php_codesniffer HOT 1
- [Feature Request]: Add a rule against unused imports HOT 2
- [Feature Request]: Add rules for arrow functions
- [Feature Request]: Add more tests for array spread operator
- [Feature Request]: Add a sniff for unused services HOT 2
- [Feature Request]: Update dependencies to be compatible with PHP 8
- [Feature Request]: Get rid of Neutron PHP Standard
- [Feature Request]: Configure Dependabot HOT 2
- [Feature Request]: Evaluate which "Slevomat" coding standard sniffs to use HOT 2
- [Feature Request]: Embrace PHPCSUtils
- [Upgrade]: Modernize existing sniffs
- [Feature Request]: Add a sniff to enforce property type HOT 2
- [Feature Request]: Replace cyclomatic complexity with Cognitive Complexity
- Having trouble finding a working composer.json configuration HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-coding-standards.