Community Update: All Aboard the Property Hooks Train! #12337
ondrejmirtes
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The release of PHPStan 2.1 is imminent. It brings support for flagship PHP 8.4 features like property hooks, asymmetric visibility, and the
#[Deprecated]attribute.Property hooks and
#[Deprecated]have been implemented in this pull request. Asymmetric visibility has been mostly implemented in this commit.Every time someone asked me when PHPStan is finally going to support PHP 8.4, I told them "later this year". And it looks like we're going to make it! This has been a massive effort that started all the way back in September. In order to support PHP 8.4 syntax, I had to upgrade to PHP-Parser v5. In order to upgrade to PHP-Parser v5, I had to release PHPStan 2.0. Which I did on November 11th, alongside the cute and also long-awaited PHPStan elephpant. PHPStan fans ordered 750 of those! They will be manufactured in China and sent on a ship to Europe which is going to take some time. I hope they're going to find themselves in the hands of their happy owners in May-June 2025, as promised in the order confirmation emails.
Back to PHP 8.4. After PHPStan 2.0 was released, I was finally able to work on PHP 8.4 support. In order to support and analyse property hooks, I had to make some choices which is why I'm posting this community update. If you contribute to PHPStan, or if you write custom rules, this is for you.
Adding support for property hooks was no easy feat. They add something similar to class methods to the PHP language. A lot of PHPStan's code had to be reviewed and adjusted, as can be seen in the linked PR. It's likely you will have to adjust some of your rules, and also write new rules, to properly cover property hooks.
Here's what you should know about PHPStan's handling of property hooks:
InPropertyHookNodeis processed. This is similar toInFunctionNodeandInClassMethodNode.Function_andClassMethodare not going to be invoked for property hooks. But you should look at these rules and decide if you should make a new one forPropertyHookto cover the same topic.FunctionLikeinterface are going to be invoked for property hooks. This is because PHP-Parser'sPropertyHooknode implements FunctionLike. You should write new tests with property hooks for these rules and possibly adjust the error messages.Scope::getFunction()returnsPhpMethodFromParserNodeReflectioninside property hooks. This class has new methods:isPropertyHook,getHookedPropertyName(returns property name),getPropertyHookName(returnsgetorset). Existing rule that ask about$scope->getFunction()should be reviewed. You should write new tests with property hooks for these rules and possibly adjust the error messages.PropertyHookReturnStatementsNodeis processed. This is similar toMethodReturnStatementsNodeand contains information about property hook body after its analysis is finished.ExtendedPropertyReflectiongets new methods: isAbstract, isFinal, isVirtual, hasHook, getHookLet me know in the comments what you think and whether you have extensions that need adjusting!
Also feel free to check out #12336 to get a list of property hooks checks that could still be added to PHPStan even after PHPStan 2.1 is released.
Thank you until next time.
Beta Was this translation helpful? Give feedback.
All reactions