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

ConstructorNameSniff false negative on PHP 8+ #3822

Open
gsteel opened this issue May 16, 2023 · 7 comments
Open

ConstructorNameSniff false negative on PHP 8+ #3822

gsteel opened this issue May 16, 2023 · 7 comments

Comments

@gsteel
Copy link

gsteel commented May 16, 2023

PHP4 style constructors are no longer possible on PHP 8+

This sniff therefore yields false negative for a class such as:

class Something {
   public function something(): void
   {
   }
}

if ($methodName === $className) {
if (in_array('__construct', $this->functionList, true) === false) {
$error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
$phpcsFile->addError($error, $stackPtr, 'OldStyle');
}
} else if ($methodName !== '__construct') {
// Not a constructor.
return;
}

@jrfnl
Copy link
Contributor

jrfnl commented May 16, 2023

When I run PHPCS over that code I see the following error:

 3 | ERROR | PHP4 style constructors are not allowed; use "__construct()" instead
   |       | (Generic.NamingConventions.ConstructorName.OldStyle)

In other words, it is unclear what you are reporting (there is no false negative) and what you expect to be done about it.

Please clarify what behaviour you expected.

@gsteel
Copy link
Author

gsteel commented May 16, 2023

Sorry @jrfnl
I got my true/false negative/positive all messed up.

On PHP 8, I'd expect no errors. On 7.x and below the error would remain present.

@jrfnl
Copy link
Contributor

jrfnl commented May 16, 2023

@gsteel Thanks for getting back to me, but I'm still not sure what you expect.

The PHP version on which PHPCS is being run does not always have a direct correlation to the PHP version the code under scan will run on.

And as that sniff has only one function - "ban PHP4 style constructors" -, if the code under scan is intended to be run on PHP 8.x, why are you including this sniff ?

This seems to me more like a configuration issue which can be solved via the project ruleset:

<exclude name="Generic.NamingConventions.ConstructorName"/>

@gsteel
Copy link
Author

gsteel commented May 16, 2023

Yes, I've disabled the sniff in the relevant project.

I guess I was expecting some magic here and thought that it might be worth implementing; Assuming this kind of version detection logic is not appropriate for phpcs then please consider this a non-issue!

Thanks for your time today :)

@jrfnl
Copy link
Contributor

jrfnl commented May 16, 2023

I guess I was expecting some magic here and thought that it might be worth implementing

Well, there is some magic which could be implemented, but the feature that "magic" relies on is very rarely used, so I'm not sure how much effect it would have and if it wouldn't lead to more reports about actual false negatives, because the sniff suddenly doesn't always report issues anymore.

@gsteel
Copy link
Author

gsteel commented May 16, 2023

TIL! If you think it's worth pursuing, I'd be happy to submit a patch if you could point me at a sniff you know of that uses a version check.

@jrfnl
Copy link
Contributor

jrfnl commented May 17, 2023

I'm honestly not sure how much grief it will cause with support requests.

However, you can find an example of it being used here: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php - though in this case, I would personally not presume that if no php_version is passed, the PHP version on which PHPCS is run should be used.

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

No branches or pull requests

2 participants