-
-
Notifications
You must be signed in to change notification settings - Fork 8
Description
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:
vendor/bin/phpcs -s --standard=Universal --sniffs=Universal.CodeAnalysis.ConstructorDestructorReturn test.php test2.php
... over a file containing this code:
<?php
class Foo {
public function __construct() {
echo __METHOD__ . "\n";
}
public function foo() {
echo __METHOD__ . "\n";
return true;
}
}
... and over a second file containing this code:
<?php
namespace NS;
class Foo {
public function foo() {
echo __METHOD__ . "\n";
return true;
}
}
I'd expect the following behaviour
No errors or warnings
Instead this happened
FILE: /tmp/test.php
--------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------------------------------------
10 | WARNING | A PHP 4-style constructor can not return a value. Found: "return true;"
| | (Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound)
--------------------------------------------------------------------------------------------------------------------------------------------------------------
FILE: /tmp/test2.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------------------------------
8 | WARNING | A PHP 4-style constructor can not return a value. Found: "return true;"
| | (Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound)
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Environment
Environment | Answer |
---|---|
PHP version | 8.0.26 |
PHP_CodeSniffer version | 3.7.1 |
PHPCSExtra version | dev-develop 0f55c12 |
PHPCSUtils version | 1.0.0 |
Install type | Composer project local |
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?
- I have verified the issue still exists in the
develop
branch of PHPCSExtra.