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

NamingConventions/ValidFunctionName: implement PHPCSUtils and support modern PHP #2191

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jan 10, 2023

NamingConventions/ValidFunctionName: add extra tests

The handling of function names only consisting of underscore was already special cased in the sniff, but not covered by tests.

Similarly, live coding was not covered.

NamingConventions/ValidFunctionName: implement PHPCSUtils methods

PHP is largely case-insensitive for class and method name, but not completely: the case-insensitivety does not apply to the non-ascii range.

The PHPCSUtils methods recognize this correctly.

Includes minor efficiency tweak by only doing the PHP4 constructor/destructor comparison when it's a real class as a name match can never be made for anonymous classes anyway.

Includes unit tests.

Note: the test would previously pass, but only due to the strtolower() mangling both the class name as well as the method name equally.

NamingConventions/ValidFunctionName: add tests covering PHP 8.1+ enums

The various PHPCSUtils Scopes functions support enums since PHPCSUtils 1.0.0-alpha4, so enums are automatically supported now, but let's safeguard this with a test anyway.

NamingConventions/ValidFunctionName: minor comment fix

The handling of function names only consisting of underscore was already special cased in the sniff, but not covered by tests.

Similarly, live coding was not covered.
PHP is largely case-insensitive for class and method name, but not completely: the case-insensitivety does not apply to the non-ascii range.

The PHPCSUtils methods recognize this correctly.

Includes minor efficiency tweak by only doing the PHP4 constructor/destructor comparison when it's a real class as a name match can never be made for anonymous classes anyway.

Includes unit tests.

Note: the test would previously pass, but only due to the `strtolower()` mangling both the class name as well as the method name equally.
The various PHPCSUtils `Scopes` functions support enums since PHPCSUtils 1.0.0-alpha4, so enums are automatically supported now, but let's safeguard this with a test anyway.
Comment on lines +152 to +155
// PHP4 destructors are allowed to break our rules.
if ( NamingConventions::isEqual( $methodName, '_' . $className ) === true ) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

I thought destructors didn't exist until PHP 5?

I've not seen them suggested with a leading underscore in PHP 4 as:

class Foo {
	function Foo() {} // Constructor.
	function _Foo() {} // Destructor.
}

Was that the de facto approach back then? How did they get triggered?

Copy link
Member Author

@jrfnl jrfnl Jan 10, 2023

Choose a reason for hiding this comment

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

All good questions. To be honest, it's so far back, that this should be redundant and I hope to never come across that type of code again.

For the purposes of this PR, I've just kept the existing functionality, which already handled things in this way (whether correct or not).
Also note that the PHPCS native PEAR.NamingConventions.ValidFunctionName sniff, which this sniff was originally based on, treats _ClassName() methods the same, i.e. as PHP 4-destructors.

I realize this is not an answer to your question, but there's only so much time (research) I'm willing to spend on work arounds for code which shouldn't exist anymore in the first place.

Copy link
Member

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

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

✅ Approving as is, but as WP requires PHP 5.6 minimum, we should reconsider the benefit of WPCS supporting coding practises from the PHP 4 era.

@GaryJones GaryJones merged commit 3a5814c into develop Jan 12, 2023
@GaryJones GaryJones deleted the feature/`validfunctionname-phpcsutils-and-modern-php branch January 12, 2023 12:48
@jrfnl
Copy link
Member Author

jrfnl commented Jan 12, 2023

✅ Approving as is, but as WP requires PHP 5.6 minimum, we should reconsider the benefit of WPCS supporting coding practises from the PHP 4 era.

What about we open an issue to remove those exceptions in WPCS 4.0 ?

@jrfnl
Copy link
Member Author

jrfnl commented Jul 3, 2023

I've opened issue #2279 as a reminder to remove the PHP 4 related exceptions from sniffs for WPCS 4.0.

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.

None yet

3 participants