Skip to content

Commit a077c4a

Browse files
authored
Merge pull request #210 from PHPCSStandards/develop
Release PHPCSExtra 1.0.2
2 parents 0f55c12 + b36dd99 commit a077c4a

12 files changed

+237
-21
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses
1414

1515
_Nothing yet._
1616

17+
## [1.0.2] - 2023-01-10
18+
19+
### Changed
20+
21+
#### Universal
22+
23+
* `Universal.CodeAnalysis.ConstructorDestructorReturn`: the sniff will now respect a potentially set [`php_version` configuration option][php_version-config] and only report on PHP4-style constructors when the `php_version` is below `'80000'`. Thanks [@anomiex] for reporting! [#207], [#208]
24+
25+
[#207]: https://github.com/PHPCSStandards/PHPCSExtra/issues/207
26+
[#208]: https://github.com/PHPCSStandards/PHPCSExtra/pull/208
27+
1728

1829
## [1.0.1] - 2023-01-05
1930

@@ -221,7 +232,6 @@ The upgrade to PHPCSUtils 1.0.0-alpha4 took care of a number of bugs, which pote
221232

222233
[php-manual-dirname]: https://www.php.net/function.dirname
223234
[php-rfc-negative_array_index]: https://wiki.php.net/rfc/negative_array_index
224-
[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version
225235
[ESLint "no lonely if"]: https://eslint.org/docs/rules/no-lonely-if
226236
[PHPCSUtils 1.0.0-alpha4]: https://github.com/PHPCSStandards/PHPCSUtils/releases/tag/1.0.0-alpha4
227237

@@ -385,8 +395,10 @@ This initial alpha release contains the following sniffs:
385395
Individual sub-types can be allowed by excluding specific error codes.
386396

387397
[Composer PHPCS plugin]: https://github.com/PHPCSStandards/composer-installer
398+
[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version
388399

389400
[Unreleased]: https://github.com/PHPCSStandards/PHPCSExtra/compare/stable...HEAD
401+
[1.0.2]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.1...1.0.2
390402
[1.0.1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0...1.0.1
391403
[1.0.0]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-rc1...1.0.0
392404
[1.0.0-RC1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha3...1.0.0-rc1

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ The sniff will make a distinction between keys which will be duplicate in all PH
177177
If a [`php_version` configuration option][php_version-config] has been passed to PHPCS using either `--config-set` or `--runtime-set`, it will be respected by the sniff and only report duplicate keys for the configured PHP version.
178178

179179
[php-rfc-negative_array_index]: https://wiki.php.net/rfc/negative_array_index
180-
[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version
181180

182181
#### `Universal.Arrays.MixedArrayKeyTypes` :books:
183182

@@ -218,6 +217,9 @@ Require a consistent modifier keyword order for class declarations.
218217
* Disallows return type declarations on constructor/destructor methods - error code: `ReturnTypeFound`, auto-fixable.
219218
* Discourages constructor/destructor methods returning a value - error code: `ReturnValueFound`.
220219

220+
If a [`php_version` configuration option][php_version-config] has been passed to PHPCS using either `--config-set` or `--runtime-set`, it will be respected by the sniff.
221+
In effect, this means that the sniff will only report on PHP4-style constructors if the configured PHP version is less than 8.0.
222+
221223
#### `Universal.CodeAnalysis.ForeachUniqueAssignment` :wrench: :books:
222224

223225
Detects `foreach` control structures which use the same variable for both the key as well as the value assignment as this will lead to unexpected - and most likely unintended - behaviour.
@@ -478,3 +480,5 @@ This code is released under the GNU Lesser General Public License (LGPLv3). For
478480
[phpcs-gh]: https://github.com/squizlabs/PHP_CodeSniffer
479481
[phpcsutils-gh]: https://github.com/PHPCSStandards/PHPCSUtils
480482
[composer-installer-gh]: https://github.com/PHPCSStandards/composer-installer
483+
484+
[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version

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+
}

0 commit comments

Comments
 (0)