-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Incorrect detection of PHP 4-style constructors by Universal.CodeAnalysis.ConstructorDestructorReturn #207
Comments
Hiya @anomiex, thanks again for this report. Very happy to see you stress-testing the sniffs ;-) Looks like you are reporting multiple issues here, so I will respond to each part of the report in turn.
This is a known issue and slated to be fixed in a future release in combination with a namespace tracker becoming available in PHPCSUtils (also see the PHPCSUtils roadmap). I could add a temporary solution for now, but I previously decided against this, as such a temporary solution would have a significant (negative) impact on the performance of the sniff, i.e. the sniff would likely become ~4x slower. Is it acceptable to you to leave this for now until a future release ?
If I remember correctly, there is more nuance to it than that. If a
So, in that regards, I believe the sniff is behaving correctly.
While it is true that this package doesn't have a "minimum PHP version" setting, PHPCS actually has a native Something similar is already done in the This would basically come down to the sniff not throwing errors for PHP4-style constructors if the How does that sound to you as a solution ? Note: this |
Note: I've added both the I consider the first issue - "namespaced classes cannot have PHP 4-style constructors" - a (known) bug. The second issue about (not) reporting on PHP4-style constructors in combination with PHP 8, I consider a potential enhancement as it would add a new feature to the sniff (respecting the |
Sure. It's easy to work around with a
I'm not clear on what you mean by "both are regarded as constructors". The PHP4-style function isn't called for
Cool, I didn't know that. That sounds like it could be a workable option. 👍
Yeah, we use that one too. 🙂 |
Once the other things here are finished, I think it may be good to move that part to a separate issue.
See the wording in the RFC: https://wiki.php.net/rfc/remove_php4_constructors In practice, PHP 4 constructors, when used in conjunction with class Foo {
public function __construct() {
// Do something.
}
public function Foo {
self::__construct();
}
} Now, if the constructor returns something, this will generally be mirrored by the PHP4-style constructor, in which case, both should be flagged IMO. class Foo {
public function __construct() {
return $this;
}
public function Foo {
return self::__construct();
}
}
Want to give PR #208 a whirl to see if that works for you ? |
@anomiex I've merged the PR now. Would be nice if you could try it out still, but I intend to release the fix some time over the next two days. |
Personally I can't say I'd worry about that case unless the intent is to still check PHP 4 code. But I won't push on it.
Confirmed, it works in testing. |
Thanks for testing @anomiex ! I've released the fix in the 1.0.2 version. |
The fix has now been released: https://github.com/PHPCSStandards/PHPCSExtra/releases/tag/1.1.2 |
Bug Description
If a
__construct()
method exists (and you're not still on PHP 4), that is the constructor and any method named like a PHP 4-style constructor is not a constructor. The Universal.CodeAnalysis.ConstructorDestructorReturn sniff treats it as a PHP 4-style constructor anyway.Also, unless you're using PHP between 5.3.0 and 5.3.2, namespaced classes cannot have PHP 4-style constructors. The Universal.CodeAnalysis.ConstructorDestructorReturn sniff treats it as a PHP 4-style constructor anyway.
Also of note is that PHP 4-style constructors were removed in PHP 8. As this package does not seem to have a "minimum PHP version" setting, I'd recommend using a different code for PHP 4-style constructors so a project that doesn't support older versions of PHP can exclude "Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFoundPHP4" (or whatever name you choose) in its configuration.
Given the following reproduction Scenario
The issue happens when running this command:
... over a file containing this code:
... and over a second file containing this code:
I'd expect the following behaviour
No errors or warnings
Instead this happened
Environment
Additional Context (optional)
See https://3v4l.org/3CAYK and https://3v4l.org/dpRB6 for verifications of the described behaviors. Or just require the test files and construct instances of the classes.
Tested Against
develop
branch?develop
branch of PHPCSExtra.The text was updated successfully, but these errors were encountered: