Skip to content

Commit 6bd92be

Browse files
committed
ACP2E-1946: Move PHPCompatibility/PHPCompatibility sniffs into separate namespace
1 parent df39772 commit 6bd92be

File tree

56 files changed

+3077
-9716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3077
-9716
lines changed

Magento2/Internal/FileProxy.php

Lines changed: 465 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,14 @@
11
<?php
22
/**
3-
* PHPCompatibility, an external standard for PHP_CodeSniffer.
4-
*
5-
* @package PHPCompatibility
6-
* @copyright 2012-2020 PHPCompatibility Contributors
7-
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8-
* @link https://github.com/PHPCompatibility/PHPCompatibility
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
95
*/
106

117
namespace Magento2\Sniffs\PHPCompatibility;
128

13-
use PHP_CodeSniffer\Files\File;
14-
use PHP_CodeSniffer\Util\Tokens;
15-
use PHPCompatibility\AbstractFunctionCallParameterSniff;
16-
use PHPCSUtils\BackCompat\BCTokens;
17-
189
/**
19-
* Detect calls to functions where the expected type of a parameter has been changed from integer to boolean.
20-
*
21-
* Throws an error when a hard-coded numeric value is passed.
22-
*
23-
* PHP version 8.0+
24-
*
25-
* @since 10.0.0
10+
* @inheritdoc
2611
*/
27-
class ChangedIntToBoolParamTypeSniff extends AbstractFunctionCallParameterSniff
12+
class ChangedIntToBoolParamTypeSniff extends \PHPCompatibility\Sniffs\ParameterValues\ChangedIntToBoolParamTypeSniff
2813
{
29-
30-
/**
31-
* Functions to check for.
32-
*
33-
* @since 10.0.0
34-
*
35-
* @var array
36-
*/
37-
protected $targetFunctions = [
38-
'ob_implicit_flush' => [
39-
1 => [
40-
'name' => 'flag',
41-
'since' => '8.0',
42-
],
43-
],
44-
'sem_get' => [
45-
4 => [
46-
'name' => 'auto_release',
47-
'since' => '8.0',
48-
],
49-
],
50-
];
51-
52-
/**
53-
* Do a version check to determine if this sniff needs to run at all.
54-
*
55-
* Checks against the first PHP version listed in the above array.
56-
*
57-
* @since 10.0.0
58-
*
59-
* @return bool
60-
*/
61-
protected function bowOutEarly()
62-
{
63-
return ($this->supportsAbove('8.0') === false);
64-
}
65-
66-
/**
67-
* Process the parameters of a matched function.
68-
*
69-
* @since 10.0.0
70-
*
71-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
72-
* @param int $stackPtr The position of the current token in the stack.
73-
* @param string $functionName The token content (function name) which was matched.
74-
* @param array $parameters Array with information about the parameters.
75-
*
76-
* @return int|void Integer stack pointer to skip forward or void to continue
77-
* normal file processing.
78-
*/
79-
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
80-
{
81-
static $search;
82-
83-
if (isset($search) === false) {
84-
$search = [
85-
\T_LNUMBER => \T_LNUMBER,
86-
\T_DNUMBER => \T_DNUMBER,
87-
];
88-
$search += BCTokens::arithmeticTokens();
89-
$search += Tokens::$emptyTokens;
90-
}
91-
92-
$functionLC = \strtolower($functionName);
93-
$functionInfo = $this->targetFunctions[$functionLC];
94-
foreach ($functionInfo as $offset => $paramInfo) {
95-
if (isset($parameters[$offset]) === false) {
96-
continue;
97-
}
98-
99-
if ($this->supportsAbove($paramInfo['since']) === false) {
100-
continue;
101-
}
102-
103-
$target = $parameters[$offset];
104-
105-
$hasNonNumeric = $phpcsFile->findNext($search, $target['start'], ($target['end'] + 1), true);
106-
if ($hasNonNumeric !== false) {
107-
// Not a purely numerical value. Ignore.
108-
continue;
109-
}
110-
111-
$error = 'The $%s parameter of %s() expects a boolean value instead of an integer since PHP %s. Found: %s';
112-
$code = $this->stringToErrorCode($functionName . '_' . $paramInfo['name']) . 'NumericFound';
113-
$data = [
114-
$paramInfo['name'],
115-
$functionLC,
116-
$paramInfo['since'],
117-
$target['raw'],
118-
];
119-
120-
$phpcsFile->addError($error, $target['start'], $code, $data);
121-
}
122-
}
12314
}
Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,36 @@
11
<?php
22
/**
3-
* PHPCompatibility, an external standard for PHP_CodeSniffer.
4-
*
5-
* @package PHPCompatibility
6-
* @copyright 2012-2020 PHPCompatibility Contributors
7-
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8-
* @link https://github.com/PHPCompatibility/PHPCompatibility
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
95
*/
106

117
namespace Magento2\Sniffs\PHPCompatibility;
128

13-
use PHPCompatibility\Sniff;
9+
use Magento2\Internal\FileProxy;
1410
use PHP_CodeSniffer\Files\File;
15-
use PHPCSUtils\Utils\FunctionDeclarations;
16-
use PHPCSUtils\Utils\Scopes;
1711

1812
/**
19-
* Applying the final modifier on a private method will produce a warning since PHP 8.0
20-
* unless that method is the constructor.
21-
*
22-
* Previously final private methods were allowed and overriding the method in a child class
23-
* would result in a fatal "Cannot override final method". This was inappropriate as
24-
* private methods are not inherited.
25-
*
26-
* > Due to how common the usage of `final private function __construct` is and given that
27-
* > the same results cannot be achieved with a protected visibility, an exception to this rule
28-
* > is made for constructors.
29-
*
30-
* PHP version 8.0
31-
*
32-
* @link https://wiki.php.net/rfc/inheritance_private_methods
33-
*
34-
* @since 10.0.0
13+
* @inheritdoc
3514
*/
36-
class ForbiddenFinalPrivateMethodsSniff extends Sniff
15+
class ForbiddenFinalPrivateMethodsSniff extends \PHPCompatibility\Sniffs\FunctionDeclarations\ForbiddenFinalPrivateMethodsSniff
3716
{
38-
private const MESSAGE_FINAL = 'Private methods should not be declared as final since PHP 8.0';
39-
40-
/**
41-
* Returns an array of tokens this test wants to listen for.
42-
*
43-
* @since 10.0.0
44-
*
45-
* @return array
46-
*/
47-
public function register()
48-
{
49-
return [\T_FUNCTION];
50-
}
51-
5217
/**
53-
* Processes this test, when one of its tokens is encountered.
54-
*
55-
* @since 10.0.0
56-
*
57-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
58-
* @param int $stackPtr The position of the current token
59-
* in the stack passed in $tokens.
60-
*
61-
* @return void
18+
* @inheritdoc
6219
*/
6320
public function process(File $phpcsFile, $stackPtr)
6421
{
65-
if (Scopes::isOOMethod($phpcsFile, $stackPtr) === false) {
66-
// Function, not method.
67-
return;
68-
}
69-
70-
$name = FunctionDeclarations::getName($phpcsFile, $stackPtr);
71-
if (empty($name) === true) {
72-
// Parse error or live coding.
73-
return;
74-
}
75-
76-
if (\strtolower($name) === '__construct') {
77-
// The rule does not apply to constructors. Bow out.
78-
return;
79-
}
80-
81-
$properties = FunctionDeclarations::getProperties($phpcsFile, $stackPtr);
82-
if ($properties['scope'] !== 'private' || $properties['is_final'] === false) {
83-
// Not a private final method.
84-
return;
85-
}
86-
87-
if ($phpcsFile->addFixableWarning(self::MESSAGE_FINAL, $stackPtr, 'Found') === true) {
22+
$phpcsFile->fixer->enabled = true;
23+
$warningCount = $phpcsFile->getWarningCount();
24+
$phpcsFileProxy = new FileProxy(
25+
$phpcsFile,
26+
[
27+
'addWarning' => function ($warning, $stackPtr, $code, $data = [], $severity = 0) use ($phpcsFile) {
28+
$phpcsFile->addFixableWarning($warning, $stackPtr, $code, $data, $severity);
29+
},
30+
]
31+
);
32+
$result = parent::process($phpcsFileProxy, $stackPtr);
33+
if ($warningCount < $phpcsFile->getWarningCount() && $phpcsFile->fixer->enabled === true) {
8834
$phpcsFile->fixer->beginChangeset();
8935
$prev = $phpcsFile->findPrevious(\T_FINAL, ($stackPtr - 1));
9036
$phpcsFile->fixer->replaceToken($prev, null);
@@ -93,5 +39,7 @@ public function process(File $phpcsFile, $stackPtr)
9339
$phpcsFile->fixer->replaceToken($next, null);
9440
$phpcsFile->fixer->endChangeset();
9541
}
42+
43+
return $result;
9644
}
9745
}
Lines changed: 4 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,14 @@
11
<?php
22
/**
3-
* PHPCompatibility, an external standard for PHP_CodeSniffer.
4-
*
5-
* @package PHPCompatibility
6-
* @copyright 2012-2020 PHPCompatibility Contributors
7-
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8-
* @link https://github.com/PHPCompatibility/PHPCompatibility
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
95
*/
106

117
namespace Magento2\Sniffs\PHPCompatibility;
128

13-
use PHP_CodeSniffer\Files\File;
14-
use PHP_CodeSniffer\Util\Tokens;
15-
use PHPCompatibility\AbstractFunctionCallParameterSniff;
16-
179
/**
18-
* Detect the use of assertions passed as a string.
19-
*
20-
* PHP 7.2:
21-
* > Usage of a string as the assertion became deprecated. It now emits an E_DEPRECATED
22-
* > notice when both assert.active and zend.assertions are set to 1.
23-
*
24-
* PHP 8.0:
25-
* > assert() will no longer evaluate string arguments, instead they will be treated
26-
* > like any other argument. `assert($a == $b)` should be used instead of `assert('$a == $b')`.
27-
*
28-
* PHP version 7.2
29-
* PHP version 8.0
30-
*
31-
* @link https://wiki.php.net/rfc/deprecations_php_7_2#assert_with_string_argument
32-
* @link https://github.com/php/php-src/blob/69888c3ff1f2301ead8e37b23ff8481d475e29d2/UPGRADING#L350-L354
33-
* @link https://www.php.net/manual/en/function.assert.php#refsect1-function.assert-changelog
34-
*
35-
* @since 10.0.0
10+
* @inheritdoc
3611
*/
37-
class RemovedAssertStringAssertionSniff extends AbstractFunctionCallParameterSniff
12+
class RemovedAssertStringAssertionSniff extends \PHPCompatibility\Sniffs\ParameterValues\RemovedAssertStringAssertionSniff
3813
{
39-
40-
/**
41-
* Functions to check for.
42-
*
43-
* @since 10.0.0
44-
*
45-
* @var array
46-
*/
47-
protected $targetFunctions = [
48-
'assert' => true,
49-
];
50-
51-
/**
52-
* Target tokens.
53-
*
54-
* If there is anything but any of these tokens in the parameter, we bow out as undetermined.
55-
*
56-
* @since 10.0.0
57-
*
58-
* @var array
59-
*/
60-
private $targetTokens = [];
61-
62-
/**
63-
* Returns an array of tokens this test wants to listen for.
64-
*
65-
* @since 10.0.0
66-
*
67-
* @return array
68-
*/
69-
public function register()
70-
{
71-
// Set $targetTokens only once.
72-
$this->targetTokens = Tokens::$emptyTokens;
73-
$this->targetTokens += Tokens::$stringTokens + Tokens::$heredocTokens;
74-
$this->targetTokens[\T_STRING_CONCAT] = \T_STRING_CONCAT;
75-
76-
return parent::register();
77-
}
78-
79-
/**
80-
* Do a version check to determine if this sniff needs to run at all.
81-
*
82-
* @since 10.0.0
83-
*
84-
* @return bool
85-
*/
86-
protected function bowOutEarly()
87-
{
88-
return ($this->supportsAbove('7.2') === false);
89-
}
90-
91-
/**
92-
* Process the parameters of a matched function.
93-
*
94-
* @since 10.0.0
95-
*
96-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
97-
* @param int $stackPtr The position of the current token in the stack.
98-
* @param string $functionName The token content (function name) which was matched.
99-
* @param array $parameters Array with information about the parameters.
100-
*
101-
* @return int|void Integer stack pointer to skip forward or void to continue
102-
* normal file processing.
103-
*/
104-
public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
105-
{
106-
if (isset($parameters[1]) === false) {
107-
return;
108-
}
109-
110-
$targetParam = $parameters[1];
111-
$hasOther = $phpcsFile->findNext($this->targetTokens, $targetParam['start'], ($targetParam['end'] + 1), true);
112-
if ($hasOther !== false) {
113-
// Some other token was found, unclear whether this is really a text string. Bow out.
114-
return;
115-
}
116-
117-
$error = 'Using a string as the assertion passed to assert() is deprecated since PHP 7.2%s. Found: %s';
118-
$code = 'Deprecated';
119-
$isError = false;
120-
$data = [
121-
'',
122-
$targetParam['raw'],
123-
];
124-
125-
if ($this->supportsAbove('8.0') === true) {
126-
$data[0] = ' and removed since PHP 8.0';
127-
$isError = true;
128-
$code = 'Removed';
129-
}
130-
131-
$this->addMessage($phpcsFile, $error, $targetParam['start'], $isError, $code, $data);
132-
}
13314
}

0 commit comments

Comments
 (0)