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

PHP 8.4 | PSR2/PropertyDeclaration: add support for final properties #950

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Apr 9, 2025

Description

Includes handling the modifier order when final is used. This introduces a new FinalAfterVisibility error code.

Includes tests.

Note: the modifier keyword order checks could probably do with some optimization, but that can be handled later.

Suggested changelog entry

  • Added support for PHP 8.4 final properties to the following sniffs:
    • PSR2.Classes.PropertyDeclaration
  • The PSR2.Classes.PropertyDeclaration will now check that the final modifier keyword is placed before a visibility keyword. Errors will be reported via a new FinalAfterVisibility error code.

Related issues/external references

Related to #734, follow up to #834 and #907

Includes handling the modifier order when `final` is used. This introduces a new `FinalAfterVisibility` error code.

Includes tests.

Note: the modifier keyword order checks could probably do with some optimization, but that can be handled later.
@@ -56,6 +56,11 @@ public function getErrorList()
82 => 1,
84 => 1,
86 => 1,
90 => 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

So something I noticed generally with the tests in phpcs is that the actual error messages used aren't tested as much - you can see how I addressed tests for sniffs at https://github.com/DanielEScherzer/common-phpcs, I test against the output of the report instead of just the line numbers. Would you be interested in me porting that to phpcs so that we can test error messages too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice! I am definitely very aware of that problem and have some ideas about this too, but hadn't gotten round to actioning it yet (priorities, priorities).

If you'd be open to it, I'd be very happy to brainstorm the various options (and consequences of those!) in a call some time after next week. Feel free to ping me on Mastodon or X to set up a day/time.

$scopePtr = $phpcsFile->findPrevious(Tokens::$scopeModifiers, ($stackPtr - 1));
$finalPtr = $phpcsFile->findPrevious(T_FINAL, ($stackPtr - 1));
if ($finalPtr > $scopePtr) {
$error = 'The final declaration must come before the visibility declaration';
Copy link
Contributor

Choose a reason for hiding this comment

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

PSR2 predates final properties, but technically https://www.php-fig.org/psr/psr-2/ just says that final needs to come before visibility and it doesn't limit that to methods, so looks great

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly and yes, I did check PSR-PER before adding this new check 😉

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

Successfully merging this pull request may close these issues.

2 participants