Skip to content

Commit 94684c7

Browse files
author
Lars Roettig
authored
Merge branch 'develop' into 21-system-resources
2 parents 5f8ca0e + 73a7b7f commit 94684c7

20 files changed

+174
-61
lines changed

Magento2/Sniffs/Classes/AbstractApiSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function process(File $phpcsFile, $stackPtr)
4848
}
4949

5050
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
51+
if ($commentStartPtr === false) {
52+
return;
53+
}
5154
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
5255

5356
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {

Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,6 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
218218
'^is_null$' => 'strict comparison "=== null"',
219219
'^intval$' => '(int) construction',
220220
'^strval$' => '(string) construction',
221+
'^htmlspecialchars$' => '\Magento\Framework\Escaper->escapeHtml',
221222
];
222223
}

Magento2/Sniffs/PHP/LiteralNamespacesSniff.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,12 @@ public function process(File $sourceFile, $stackPtr)
5252
$content = preg_replace('|\\\{2,}|', '\\', $content);
5353
}
5454

55-
if (preg_match($this->literalNamespacePattern, $content) === 1 && $this->classExists($content)) {
55+
if (preg_match($this->literalNamespacePattern, $content) === 1) {
5656
$sourceFile->addWarning(
5757
"Use ::class notation instead.",
5858
$stackPtr,
5959
'LiteralClassUsage'
6060
);
6161
}
6262
}
63-
64-
/**
65-
* Checks if class or interface exists.
66-
*
67-
* ToDo: get rig of this check https://github.com/magento/magento-coding-standard/issues/9
68-
*
69-
* @param string $className
70-
* @return bool
71-
*/
72-
private function classExists($className)
73-
{
74-
if (!isset($this->classNames[$className])) {
75-
$this->classNames[$className] = class_exists($className) || interface_exists($className);
76-
}
77-
return $this->classNames[$className];
78-
}
7963
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Rule: array_merge(...) is used in a loop and is a resources greedy construction
2+
3+
## Reason
4+
Merging arrays in a loop is slow and causes high CPU usage.
5+
6+
## How to Fix
7+
Typical example when `array_merge` is being used in the loop:
8+
``` php
9+
$options = [];
10+
foreach ($configurationSources as $source) {
11+
// code here
12+
$options = array_merge($options, $source->getOptions());
13+
}
14+
```
15+
16+
In order to reduce execution time `array_merge` can be called only once:
17+
``` php
18+
$options = [[]];
19+
foreach ($configurationSources as $source) {
20+
// code here
21+
$options[] = $source->getOptions();
22+
}
23+
24+
// PHP 5.6+
25+
$options = array_merge(...$options);
26+
```

Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function process(File $phpcsFile, $stackPtr)
4747
{
4848
$tokens = $phpcsFile->getTokens();
4949

50+
// If it's inline control structure we do nothing. PSR2 issue will be raised.
51+
if (!array_key_exists('scope_opener', $tokens[$stackPtr])) {
52+
return;
53+
}
5054
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
5155
$scopeCloser = $tokens[$stackPtr]['scope_closer'];
5256

Magento2/Sniffs/Security/InsecureFunctionSniff.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
*/
1313
class InsecureFunctionSniff extends ForbiddenFunctionsSniff
1414
{
15-
/**
16-
* If true, an error will be thrown; otherwise a warning.
17-
*
18-
* @var boolean
19-
*/
20-
public $error = false;
21-
2215
/**
2316
* List of patterns for forbidden functions.
2417
*
@@ -38,6 +31,7 @@ class InsecureFunctionSniff extends ForbiddenFunctionsSniff
3831
'system' => null,
3932
'unserialize' => '\Magento\Framework\Serialize\SerializerInterface::unserialize',
4033
'srand' => null,
41-
'mt_srand'=> null,
34+
'mt_srand' => null,
35+
'mt_rand' => 'random_int',
4236
];
4337
}

Magento2/Tests/Classes/AbstractApiUnitTest.inc renamed to Magento2/Tests/Classes/AbstractApiUnitTest.1.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class FooBar implements FooInterface
2828

2929
}
3030

31-
3231
/**
3332
* Class Bar
3433
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
abstract class FooNoComment implements FooInterface
4+
{
5+
6+
}

Magento2/Tests/Classes/AbstractApiUnitTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ public function getErrorList()
2323
/**
2424
* @inheritdoc
2525
*/
26-
public function getWarningList()
26+
public function getWarningList($testFile = '')
2727
{
28-
return [
29-
14 => 1,
30-
23 => 1
31-
];
28+
if ($testFile === 'AbstractApiUnitTest.1.inc') {
29+
return [
30+
14 => 1,
31+
23 => 1
32+
];
33+
}
34+
35+
return [];
3236
}
3337
}

Magento2/Tests/Functions/DiscouragedFunctionUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,5 @@ $int = intval($int);
462462
$str = chop($text, 'ttt');
463463

464464
md5($text);
465+
466+
htmlspecialchars('text');

0 commit comments

Comments
 (0)