Skip to content

File::getMemberProperties(): removed parse error warning #991

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

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Apr 14, 2025

Description

Tests: rename various test case files

... to allow for adding additional test case files containing parse errors.

File::getMemberProperties(): removed parse error warning

The File::getMemberProperties() method used to be inconsistent in how it handled variable tokens which were not property declarations.

  • In most cases ("normal" variable, function parameter), it would throw a RuntimeException "'$stackPtr is not a class member var'".
  • However, for "properties" declared in an enum or interface construct, it would register a warning about a possible parse error and return an empty array.

This parse error warning has now been removed.

As, as of PHP 8.4, declaring (hooked) properties in an interface is no longer a parse error (see the Property Hooks RFC), properties declared in interfaces will now be analyzed by the function, like any other property, and will return an array of information about the property.

For "properties" declared in enums, which is still not allowed in PHP, the method will throw the RuntimeException "'$stackPtr is not a class member var'".

Includes updated unit tests for the File::getMemberProperties() method.

Includes a review of all uses of the File::getMemberProperties() method in PHPCS native sniffs and updating the code where necessary.
Includes adding/updating tests with properties in interfaces and enums for each of those sniffs.

Suggested changelog entry

Added:

  • Added support for PHP 8.4 properties in interfaces to File::getMemberProperties().
    • Note: properties in interfaces is not fully supported yet, it is just this one method which handles them correctly at this moment.

Changed:

  • The File::getMemberProperties() method will no longer add warnings about possible parse errors.
    • This means the Internal.ParseError.InterfaceHasMemberVar and the Internal.ParseError.EnumHasMemberVar error codes have been removed.
    • The method will now throw a RuntimeException - "$stackPtr is not a class member var" - for properties declared in enums (parse error).
    • Properties declared in interfaces will be analyzed like all other properties, as these are allowed since PHP 8.4.

Related issues/external references

Closes squizlabs/PHP_CodeSniffer#2455

Also related to #734

jrfnl and others added 2 commits April 14, 2025 22:01
... to allow for adding additional test case files containing parse errors.
The `File::getMemberProperties()` method used to be inconsistent in how it handled variable tokens which were not property declarations.
* In most cases ("normal" variable, function parameter), it would throw a `RuntimeException` _"'$stackPtr is not a class member var'"_.
* However, for "properties" declared in an `enum` or `interface` construct, it would register a warning about a possible parse error and return an empty array.

This parse error warning has now been removed.

As, as of PHP 8.4, declaring (hooked) properties in an interface is no longer a parse error (see the [Property Hooks RFC](https://wiki.php.net/rfc/property-hooks)), properties declared in interfaces will now be analyzed by the function, like any other property, and will return an array of information about the property.

For "properties" declared in enums, which is still not allowed in PHP, the method will throw the `RuntimeException` _"'$stackPtr is not a class member var'"_.

Includes updated unit tests for the `File::getMemberProperties()` method.

Includes a review of all uses of the `File::getMemberProperties()` method in PHPCS native sniffs and updating the code where necessary.
Includes adding/updating tests with properties in interfaces and enums for each of those sniffs.

Closes squizlabs/PHP_CodeSniffer 2455

Also related to 734

Co-authored-by: Greg Sherwood <gsherwood@squiz.net>
@jrfnl jrfnl merged commit 1870844 into 4.x Apr 15, 2025
54 checks passed
@jrfnl jrfnl deleted the phpcs-4.0/feature/sq-2455-file-getmemberproperties-remove-parse-error-warnings branch April 15, 2025 10:33
@jrfnl jrfnl mentioned this pull request Apr 15, 2025
43 tasks
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