Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 94684c7

Browse filesBrowse files
author
Lars Roettig
authored
Merge branch 'develop' into 21-system-resources
2 parents 5f8ca0e + 73a7b7f commit 94684c7
Copy full SHA for 94684c7
Expand file treeCollapse file tree

20 files changed

+174
-61
lines changed

‎Magento2/Sniffs/Classes/AbstractApiSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Classes/AbstractApiSniff.php
+3Lines 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

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php
+1Lines 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

Copy file name to clipboardExpand all lines: Magento2/Sniffs/PHP/LiteralNamespacesSniff.php
+1-17Lines 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
}
+26Lines 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

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php
+4Lines 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

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Security/InsecureFunctionSniff.php
+2-8Lines 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

Copy file name to clipboardExpand all lines: Magento2/Tests/Classes/AbstractApiUnitTest.1.inc
-1Lines 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
*/
+6Lines 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

Copy file name to clipboardExpand all lines: Magento2/Tests/Classes/AbstractApiUnitTest.php
+9-5Lines 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

Copy file name to clipboardExpand all lines: Magento2/Tests/Functions/DiscouragedFunctionUnitTest.inc
+2Lines 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');

‎Magento2/Tests/Functions/DiscouragedFunctionUnitTest.php

Copy file name to clipboardExpand all lines: Magento2/Tests/Functions/DiscouragedFunctionUnitTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ public function getWarningList()
249249
458 => 1,
250250
460 => 1,
251251
462 => 1,
252+
466 => 1
252253
];
253254
}
254255
}
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class Fixture
4+
{
5+
public function ok()
6+
{
7+
$this->create(Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class);
8+
}
9+
10+
public function notOk()
11+
{
12+
$this->create('Magento\CustomerSegment\Model\Segment\Condition\Customer\Address');
13+
}
14+
}
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\PHP;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class LiteralNamespacesUnitTest
12+
*/
13+
class LiteralNamespacesUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function getWarningList()
27+
{
28+
return [
29+
12 => 1,
30+
];
31+
}
32+
}

‎Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc

Copy file name to clipboardExpand all lines: Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ foreach ([] as $collection) {
4141
$selectBuilder->setColumns(array_merge($selectBuilder->getColumns(), $source));
4242
}
4343
}
44+
45+
foreach ([] as $item)
46+
// inline foreach
47+
$a = array_merge([], []);
48+
49+
foreach ([] as $item)
50+
// array_merge after inline foreach
51+
$a = array_merge([], []);

‎Magento2/Tests/Security/InsecureFunctionUnitTest.inc

Copy file name to clipboardExpand all lines: Magento2/Tests/Security/InsecureFunctionUnitTest.inc
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ pcntl_exec('path/goes/here');
2727
srand();
2828

2929
mt_srand();
30+
31+
mt_rand();

‎Magento2/Tests/Security/InsecureFunctionUnitTest.php

Copy file name to clipboardExpand all lines: Magento2/Tests/Security/InsecureFunctionUnitTest.php
+10-8Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento2\Tests\Security;
78

89
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
@@ -16,14 +17,6 @@ class InsecureFunctionUnitTest extends AbstractSniffUnitTest
1617
* @inheritdoc
1718
*/
1819
public function getErrorList()
19-
{
20-
return [];
21-
}
22-
23-
/**
24-
* @inheritdoc
25-
*/
26-
public function getWarningList()
2720
{
2821
return [
2922
3 => 1,
@@ -40,6 +33,15 @@ public function getWarningList()
4033
25 => 1,
4134
27 => 1,
4235
29 => 1,
36+
31 => 1,
4337
];
4438
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function getWarningList()
44+
{
45+
return [];
46+
}
4547
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.