Skip to content

Commit

Permalink
Merge pull request #210 from PHPCSStandards/develop
Browse files Browse the repository at this point in the history
Release PHPCSExtra 1.0.2
  • Loading branch information
jrfnl committed Jan 9, 2023
2 parents 0f55c12 + b36dd99 commit a077c4a
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 21 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses

_Nothing yet._

## [1.0.2] - 2023-01-10

### Changed

#### Universal

* `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]

[#207]: https://github.com/PHPCSStandards/PHPCSExtra/issues/207
[#208]: https://github.com/PHPCSStandards/PHPCSExtra/pull/208


## [1.0.1] - 2023-01-05

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

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

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

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

[Unreleased]: https://github.com/PHPCSStandards/PHPCSExtra/compare/stable...HEAD
[1.0.2]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-rc1...1.0.0
[1.0.0-RC1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha3...1.0.0-rc1
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ The sniff will make a distinction between keys which will be duplicate in all PH
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.

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

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

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

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.
In effect, this means that the sniff will only report on PHP4-style constructors if the configured PHP version is less than 8.0.

#### `Universal.CodeAnalysis.ForeachUniqueAssignment` :wrench: :books:

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.
Expand Down Expand Up @@ -478,3 +480,5 @@ This code is released under the GNU Lesser General Public License (LGPLv3). For
[phpcs-gh]: https://github.com/squizlabs/PHP_CodeSniffer
[phpcsutils-gh]: https://github.com/PHPCSStandards/PHPCSUtils
[composer-installer-gh]: https://github.com/PHPCSStandards/composer-installer

[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\BCFile;
use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\FunctionDeclarations;
use PHPCSUtils\Utils\GetTokensAsString;
Expand Down Expand Up @@ -67,7 +68,12 @@ public function process(File $phpcsFile, $stackPtr)
if ($functionNameLC === '__construct' || $functionNameLC === '__destruct') {
$functionType = \sprintf('A "%s()" magic method', $functionNameLC);
} else {
// This may be a PHP 4-style constructor.
// If the PHP version is explicitly set to PHP 8.0 or higher, ignore PHP 4-style constructors.
if ((int) Helper::getConfigData('php_version') >= 80000) {
return;
}

// This may be a PHP 4-style constructor which should be handled.
$OOName = ObjectDeclarations::getName($phpcsFile, $scopePtr);

if (empty($OOName) === true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This test file is run with php_version set to a value of 80000 or higher.
*
* The PHP4-style constructor should no longer be recognized as a constructor.
* No errors should be thrown for it, nor any auto-fixes made.
*/
class ReturnsAValue {
public function __construct(): self {
return $this;
}

public function __destruct():string {
return 'destructed';
}

function returnsavalue(): string
{
return 'php4style';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This test file is run with php_version set to a value of 80000 or higher.
*
* The PHP4-style constructor should no longer be recognized as a constructor.
* No errors should be thrown for it, nor any auto-fixes made.
*/
class ReturnsAValue {
public function __construct() {
return $this;
}

public function __destruct() {
return 'destructed';
}

function returnsavalue(): string
{
return 'php4style';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This test file is run with php_version set to a value of 70999 or lower.
*
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
*/

class ReturnsAValue {
public function __construct(): self {
return $this;
}

public function __destruct():string {
return 'destructed';
}

function returnsavalue(): string
{
return 'php4style';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This test file is run with php_version set to a value of 70999 or lower.
*
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
*/

class ReturnsAValue {
public function __construct() {
return $this;
}

public function __destruct() {
return 'destructed';
}

function returnsavalue()
{
return 'php4style';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This test file is only here to reset the value of the php_version configuration option.
*
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
*/

class ReturnsAValue {
function returnsavalue(): string
{
return 'php4style';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This test file is only here to reset the value of the php_version configuration option.
*
* The PHP4-style constructor should be recognized as a constructor and handled as usual.
*/

class ReturnsAValue {
function returnsavalue()
{
return 'php4style';
}
}
114 changes: 96 additions & 18 deletions Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace PHPCSExtra\Universal\Tests\CodeAnalysis;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
use PHPCSUtils\BackCompat\Helper;

/**
* Unit test class for the ConstructorDestructorReturn sniff.
Expand All @@ -22,37 +23,114 @@
final class ConstructorDestructorReturnUnitTest extends AbstractSniffUnitTest
{

/**
* Set CLI values before the file is tested.
*
* @param string $testFile The name of the file being tested.
* @param \PHP_CodeSniffer\Config $config The config data for the test run.
*
* @return void
*/
public function setCliValues($testFile, $config)
{
switch ($testFile) {
case 'ConstructorDestructorReturnUnitTest.2.inc':
Helper::setConfigData('php_version', '80025', true, $config); // PHP 8.0.25.
break;

case 'ConstructorDestructorReturnUnitTest.3.inc':
Helper::setConfigData('php_version', '70313', true, $config); // PHP 7.3.13.
break;

default:
Helper::setConfigData('php_version', null, true, $config); // No PHP version set.
break;
}
}

/**
* Returns the lines where errors should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array <int line number> => <int number of errors>
*/
public function getErrorList()
public function getErrorList($testFile = '')
{
return [
85 => 1,
89 => 1,
101 => 1,
116 => 1,
118 => 1,
122 => 1,
124 => 1,
];
switch ($testFile) {
case 'ConstructorDestructorReturnUnitTest.1.inc':
return [
85 => 1,
89 => 1,
101 => 1,
116 => 1,
118 => 1,
122 => 1,
124 => 1,
];

case 'ConstructorDestructorReturnUnitTest.2.inc':
return [
10 => 1,
14 => 1,
];

case 'ConstructorDestructorReturnUnitTest.3.inc':
return [
10 => 1,
14 => 1,
18 => 1,
];

case 'ConstructorDestructorReturnUnitTest.4.inc':
return [
10 => 1,
];

default:
return [];
}
}

/**
* Returns the lines where warnings should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array <int line number> => <int number of warnings>
*/
public function getWarningList()
public function getWarningList($testFile = '')
{
return [
86 => 1,
90 => 1,
95 => 1,
103 => 1,
107 => 1,
];
switch ($testFile) {
case 'ConstructorDestructorReturnUnitTest.1.inc':
return [
86 => 1,
90 => 1,
95 => 1,
103 => 1,
107 => 1,
];

case 'ConstructorDestructorReturnUnitTest.2.inc':
return [
11 => 1,
15 => 1,
];

case 'ConstructorDestructorReturnUnitTest.3.inc':
return [
11 => 1,
15 => 1,
20 => 1,
];

case 'ConstructorDestructorReturnUnitTest.4.inc':
return [
12 => 1,
];

default:
return [];
}
}
}

0 comments on commit a077c4a

Please sign in to comment.