Skip to content

Commit 39ac431

Browse files
authored
Merge pull request #208 from PHPCSStandards/universal/207-constructordestructorreturn
ConstructorDestructorReturn: respect a potentially set `php_version` config value
2 parents 0f55c12 + eff67ec commit 39ac431

10 files changed

+219
-19
lines changed

Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHP_CodeSniffer\Sniffs\Sniff;
1515
use PHP_CodeSniffer\Util\Tokens;
1616
use PHPCSUtils\BackCompat\BCFile;
17+
use PHPCSUtils\BackCompat\Helper;
1718
use PHPCSUtils\Tokens\Collections;
1819
use PHPCSUtils\Utils\FunctionDeclarations;
1920
use PHPCSUtils\Utils\GetTokensAsString;
@@ -67,7 +68,12 @@ public function process(File $phpcsFile, $stackPtr)
6768
if ($functionNameLC === '__construct' || $functionNameLC === '__destruct') {
6869
$functionType = \sprintf('A "%s()" magic method', $functionNameLC);
6970
} else {
70-
// This may be a PHP 4-style constructor.
71+
// If the PHP version is explicitly set to PHP 8.0 or higher, ignore PHP 4-style constructors.
72+
if ((int) Helper::getConfigData('php_version') >= 80000) {
73+
return;
74+
}
75+
76+
// This may be a PHP 4-style constructor which should be handled.
7177
$OOName = ObjectDeclarations::getName($phpcsFile, $scopePtr);
7278

7379
if (empty($OOName) === true) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This test file is run with php_version set to a value of 80000 or higher.
5+
*
6+
* The PHP4-style constructor should no longer be recognized as a constructor.
7+
* No errors should be thrown for it, nor any auto-fixes made.
8+
*/
9+
class ReturnsAValue {
10+
public function __construct(): self {
11+
return $this;
12+
}
13+
14+
public function __destruct():string {
15+
return 'destructed';
16+
}
17+
18+
function returnsavalue(): string
19+
{
20+
return 'php4style';
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This test file is run with php_version set to a value of 80000 or higher.
5+
*
6+
* The PHP4-style constructor should no longer be recognized as a constructor.
7+
* No errors should be thrown for it, nor any auto-fixes made.
8+
*/
9+
class ReturnsAValue {
10+
public function __construct() {
11+
return $this;
12+
}
13+
14+
public function __destruct() {
15+
return 'destructed';
16+
}
17+
18+
function returnsavalue(): string
19+
{
20+
return 'php4style';
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This test file is run with php_version set to a value of 70999 or lower.
5+
*
6+
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
7+
*/
8+
9+
class ReturnsAValue {
10+
public function __construct(): self {
11+
return $this;
12+
}
13+
14+
public function __destruct():string {
15+
return 'destructed';
16+
}
17+
18+
function returnsavalue(): string
19+
{
20+
return 'php4style';
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This test file is run with php_version set to a value of 70999 or lower.
5+
*
6+
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
7+
*/
8+
9+
class ReturnsAValue {
10+
public function __construct() {
11+
return $this;
12+
}
13+
14+
public function __destruct() {
15+
return 'destructed';
16+
}
17+
18+
function returnsavalue()
19+
{
20+
return 'php4style';
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/*
4+
* This test file is only here to reset the value of the php_version configuration option.
5+
*
6+
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
7+
*/
8+
9+
class ReturnsAValue {
10+
function returnsavalue(): string
11+
{
12+
return 'php4style';
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/*
4+
* This test file is only here to reset the value of the php_version configuration option.
5+
*
6+
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
7+
*/
8+
9+
class ReturnsAValue {
10+
function returnsavalue()
11+
{
12+
return 'php4style';
13+
}
14+
}

Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.php

Lines changed: 96 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHPCSExtra\Universal\Tests\CodeAnalysis;
1212

1313
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
14+
use PHPCSUtils\BackCompat\Helper;
1415

1516
/**
1617
* Unit test class for the ConstructorDestructorReturn sniff.
@@ -22,37 +23,114 @@
2223
final class ConstructorDestructorReturnUnitTest extends AbstractSniffUnitTest
2324
{
2425

26+
/**
27+
* Set CLI values before the file is tested.
28+
*
29+
* @param string $testFile The name of the file being tested.
30+
* @param \PHP_CodeSniffer\Config $config The config data for the test run.
31+
*
32+
* @return void
33+
*/
34+
public function setCliValues($testFile, $config)
35+
{
36+
switch ($testFile) {
37+
case 'ConstructorDestructorReturnUnitTest.2.inc':
38+
Helper::setConfigData('php_version', '80025', true, $config); // PHP 8.0.25.
39+
break;
40+
41+
case 'ConstructorDestructorReturnUnitTest.3.inc':
42+
Helper::setConfigData('php_version', '70313', true, $config); // PHP 7.3.13.
43+
break;
44+
45+
default:
46+
Helper::setConfigData('php_version', null, true, $config); // No PHP version set.
47+
break;
48+
}
49+
}
50+
2551
/**
2652
* Returns the lines where errors should occur.
2753
*
54+
* @param string $testFile The name of the file being tested.
55+
*
2856
* @return array <int line number> => <int number of errors>
2957
*/
30-
public function getErrorList()
58+
public function getErrorList($testFile = '')
3159
{
32-
return [
33-
85 => 1,
34-
89 => 1,
35-
101 => 1,
36-
116 => 1,
37-
118 => 1,
38-
122 => 1,
39-
124 => 1,
40-
];
60+
switch ($testFile) {
61+
case 'ConstructorDestructorReturnUnitTest.1.inc':
62+
return [
63+
85 => 1,
64+
89 => 1,
65+
101 => 1,
66+
116 => 1,
67+
118 => 1,
68+
122 => 1,
69+
124 => 1,
70+
];
71+
72+
case 'ConstructorDestructorReturnUnitTest.2.inc':
73+
return [
74+
10 => 1,
75+
14 => 1,
76+
];
77+
78+
case 'ConstructorDestructorReturnUnitTest.3.inc':
79+
return [
80+
10 => 1,
81+
14 => 1,
82+
18 => 1,
83+
];
84+
85+
case 'ConstructorDestructorReturnUnitTest.4.inc':
86+
return [
87+
10 => 1,
88+
];
89+
90+
default:
91+
return [];
92+
}
4193
}
4294

4395
/**
4496
* Returns the lines where warnings should occur.
4597
*
98+
* @param string $testFile The name of the file being tested.
99+
*
46100
* @return array <int line number> => <int number of warnings>
47101
*/
48-
public function getWarningList()
102+
public function getWarningList($testFile = '')
49103
{
50-
return [
51-
86 => 1,
52-
90 => 1,
53-
95 => 1,
54-
103 => 1,
55-
107 => 1,
56-
];
104+
switch ($testFile) {
105+
case 'ConstructorDestructorReturnUnitTest.1.inc':
106+
return [
107+
86 => 1,
108+
90 => 1,
109+
95 => 1,
110+
103 => 1,
111+
107 => 1,
112+
];
113+
114+
case 'ConstructorDestructorReturnUnitTest.2.inc':
115+
return [
116+
11 => 1,
117+
15 => 1,
118+
];
119+
120+
case 'ConstructorDestructorReturnUnitTest.3.inc':
121+
return [
122+
11 => 1,
123+
15 => 1,
124+
20 => 1,
125+
];
126+
127+
case 'ConstructorDestructorReturnUnitTest.4.inc':
128+
return [
129+
12 => 1,
130+
];
131+
132+
default:
133+
return [];
134+
}
57135
}
58136
}

0 commit comments

Comments
 (0)