Skip to content

Commit a56e5bb

Browse files
committed
Merge branch 'develop' of github.com:magento/magento-coding-standard into AC-681
2 parents a7a82f9 + 914dd7a commit a56e5bb

31 files changed

+6467
-794
lines changed

.github/workflows/php.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ jobs:
4242
- name: Install dependencies
4343
run: npm install
4444
- name: Run ESLint
45-
run: npm run eslint -- eslint/rules
45+
run: npm run eslint -- eslint/rules Magento2 --ignore-pattern 'Magento2/Tests/Eslint/*Test.js'
46+
- name: Run JSCS
47+
run: npm run jscs eslint/rules Magento2
4648

4749
- name: Validate composer
4850
run: composer validate
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use PHP_CodeSniffer\Files\File;
12+
13+
/**
14+
* Tests to find usage of restricted code
15+
*/
16+
class RestrictedCodeSniff implements Sniff
17+
{
18+
private const ERROR_MESSAGE = "Class '%s' is restricted in %s. Suggested replacement: %s";
19+
private const ERROR_CODE = "restrictedClass";
20+
21+
/**
22+
* List of fixtures that contain restricted classes and should not be tested
23+
*
24+
* @var array
25+
*/
26+
private $fixtureFiles = [];
27+
28+
/**
29+
* Restricted classes
30+
*
31+
* @var array
32+
*/
33+
private $classes = [];
34+
35+
/**
36+
* RestrictedCodeSniff constructor.
37+
*/
38+
public function __construct()
39+
{
40+
$this->loadData('restricted_classes*.php');
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function register()
47+
{
48+
return [
49+
T_STRING,
50+
T_CONSTANT_ENCAPSED_STRING
51+
];
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function process(File $phpcsFile, $stackPtr)
58+
{
59+
// phpcs:ignore
60+
if (array_key_exists(basename($phpcsFile->getFilename()), $this->fixtureFiles)) {
61+
return;
62+
}
63+
64+
$tokens = $phpcsFile->getTokens();
65+
$token = $tokens[$stackPtr]['content'];
66+
if (array_key_exists($token, $this->classes)) {
67+
if ($this->isExcluded($token, $phpcsFile)) {
68+
return;
69+
}
70+
$phpcsFile->addError(
71+
sprintf(
72+
self::ERROR_MESSAGE,
73+
$token,
74+
$phpcsFile->getFilename(),
75+
$this->classes[$token]['replacement']
76+
),
77+
$stackPtr,
78+
self::ERROR_CODE,
79+
);
80+
}
81+
}
82+
83+
/**
84+
* Checks if currently parsed file should be excluded from analysis
85+
*
86+
* @param string $token
87+
* @param File $phpcsFile
88+
* @return bool
89+
*/
90+
private function isExcluded(string $token, File $phpcsFile): bool
91+
{
92+
if (in_array($phpcsFile->getFilename(), $this->fixtureFiles)) {
93+
return true;
94+
}
95+
foreach ($this->classes[$token]['exclude'] as $exclude) {
96+
if (strpos($phpcsFile->getFilename(), $exclude) !== false) {
97+
return true;
98+
}
99+
}
100+
return false;
101+
}
102+
103+
/**
104+
* Loads and merges data from fixtures
105+
*
106+
* @param string $filePattern
107+
* @return void
108+
*/
109+
private function loadData(string $filePattern)
110+
{
111+
// phpcs:ignore
112+
foreach (glob(__DIR__ . '/_files/' . $filePattern) as $file) {
113+
$relativePath = str_replace(
114+
'\\',
115+
'/',
116+
str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file)
117+
);
118+
array_push($this->fixtureFiles, $relativePath);
119+
$this->classes = array_merge_recursive($this->classes, $this->readList($file));
120+
}
121+
}
122+
123+
/**
124+
* Isolate including a file into a method to reduce scope
125+
*
126+
* @param string $file
127+
* @return array
128+
*/
129+
private function readList($file)
130+
{
131+
// phpcs:ignore
132+
return include $file;
133+
}
134+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Classes that are restricted to use directly.
9+
* A <replacement> will be suggested to be used instead.
10+
* Use <whitelist> to specify files and directories that are allowed to use restricted classes.
11+
*
12+
* Format: array(<class_name>, <replacement>[, array(<whitelist>)]])
13+
*/
14+
return [
15+
'Zend_Db_Select' => [
16+
'replacement' => '\Magento\Framework\DB\Select',
17+
'exclude' => [
18+
'Magento/Framework/DB/Select.php',
19+
'Magento/Framework/DB/Adapter/Pdo/Mysql.php',
20+
'Magento/Framework/Model/ResourceModel/Iterator.php'
21+
]
22+
],
23+
'Zend_Db_Adapter_Pdo_Mysql' => [
24+
'replacement' => '\Magento\Framework\DB\Adapter\Pdo\Mysql',
25+
'exclude' => [
26+
'Magento/Framework/DB/Adapter/Pdo/Mysql.php'
27+
]
28+
],
29+
'Magento\Framework\Serialize\Serializer\Serialize' => [
30+
'replacement' => 'Magento\Framework\Serialize\SerializerInterface',
31+
'exclude' => [
32+
'Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php',
33+
'Magento/Framework/App/Config/ScopePool.php',
34+
'Magento/Framework/App/ObjectManager/ConfigCache.php',
35+
'Magento/Framework/App/ObjectManager/ConfigLoader.php',
36+
'Magento/Framework/DB/Adapter/Pdo/Mysql.php',
37+
'Magento/Framework/DB/DataConverter/SerializedToJson.php',
38+
'Magento/Framework/DB/Test/Unit/DataConverter/SerializedToJsonTest.php',
39+
'Magento/Framework/ObjectManager/Config/Compiled.php',
40+
'Magento/Framework/Interception/Config/Config.php',
41+
'Magento/Framework/Interception/PluginList/PluginList.php',
42+
'Magento/Framework/App/Router/ActionList.php',
43+
'Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php',
44+
'src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php',
45+
'Magento/Sales/Setup/SerializedDataConverter.php',
46+
'Magento/Sales/Test/Unit/Setup/SerializedDataConverterTest.php',
47+
'Magento/Sales/Test/Unit/Setup/SalesOrderPaymentDataConverterTest.php',
48+
'Magento/Framework/Flag.php',
49+
'Magento/Widget/Setup/LayoutUpdateConverter.php',
50+
'Magento/Cms/Setup/ContentConverter.php',
51+
'Magento/Framework/Unserialize/Test/Unit/UnserializeTest.php'
52+
]
53+
],
54+
'ArrayObject' => [
55+
'replacement' => 'Custom class, extended from ArrayObject with overwritten serialize/unserialize methods',
56+
'exclude' => [
57+
'Magento/Theme/Model/Indexer/Design/Config.php',
58+
'Magento/Ui/Model/Manager.php',
59+
'Magento/Ui/Test/Unit/Model/ManagerTest.php',
60+
'Magento/Backend/Model/Menu.php',
61+
'Magento/CatalogSearch/Model/Indexer/Fulltext.php',
62+
'Magento/CatalogSearch/Test/Unit/Model/Indexer/FulltextTest.php',
63+
'Magento/Catalog/Test/Unit/Model/ProductTest.php',
64+
'Magento/CatalogSearch/Model/Indexer/Fulltext.php',
65+
'Magento/Framework/Test/Unit/FlagTest.php',
66+
'Magento/Framework/Validator/Test/Unit/Constraint/PropertyTest.php',
67+
'Magento/Framework/Indexer/Test/Unit/BatchTest.php',
68+
'Magento/Framework/View/Element/UiComponent/ArrayObjectFactory.php',
69+
'Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php',
70+
'Magento/Framework/Indexer/Action/Base.php'
71+
]
72+
],
73+
'Magento\Framework\View\Element\UiComponent\ArrayObjectFactory' => [
74+
'replacement' => 'Factory that creates custom class, extended from ArrayObject with overwritten '
75+
. 'serialize/unserialize methods',
76+
'exclude' => [
77+
'Magento/Ui/Model/Manager.php',
78+
'Magento/Ui/Test/Unit/Model/ManagerTest.php',
79+
'Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php',
80+
]
81+
]
82+
];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Classes that are restricted to use directly.
4+
* A <replacement> will be suggested to be used instead.
5+
* Use <whitelist> to specify files and directories that are allowed to use restricted classes.
6+
*
7+
* Format: array(<class_name>, <replacement>[, array(<whitelist>)]])
8+
*
9+
* Copyright © Magento, Inc. All rights reserved.
10+
* See COPYING.txt for license details.
11+
*/
12+
return [
13+
'Zend_Db_Select' => [
14+
'exclude' => [
15+
'Magento/ResourceConnections/DB/Adapter/Pdo/MysqlProxy.php'
16+
],
17+
],
18+
'Magento\Framework\Serialize\Serializer\Serialize' => [
19+
'exclude' => [
20+
'Magento/Framework/Test/Unit/FlagTest.php',
21+
'Magento/Staging/Test/Unit/Model/Update/FlagTest.php',
22+
'Magento/Logging/Test/Unit/Setup/ObjectConverterTest.php'
23+
]
24+
],
25+
'ArrayObject' => [
26+
'exclude' => [
27+
'Magento/MultipleWishlist/Test/Unit/Model/Search/Strategy/EmailTest.php',
28+
'Magento/Rma/Test/Unit/Model/RmaRepositoryTest.php',
29+
'Magento/Rma/Test/Unit/Model/Status/HistoryRepositoryTest.php'
30+
]
31+
]
32+
];

Magento2/Tests/Eslint/AndSelfTest.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
$(document).ready(function() {
1+
$(document).ready(function () {
22
'use strict';
3-
$("div").find("p").andSelf().addClass("border");
4-
});
3+
4+
$('div').find('p').andSelf().addClass('border');
5+
});
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
$(document).ready(function(){
1+
$(document).ready(function () {
22
'use strict';
3-
$(".btn1").bind("click");
4-
});
3+
4+
$('.btn1').bind('click');
5+
});

Magento2/Tests/Eslint/ClickEventShorthand.js

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$(document).ready(function () {
2+
'use strict';
3+
4+
$('input').blur();
5+
});

Magento2/Tests/Eslint/ClickEventShorthandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ClickEventShorthandTest extends AbstractEslintTestCase
1717
public function testExecute(): void
1818
{
1919
$this->assertFileContainsError(
20-
'ClickEventShorthand.js',
20+
'ClickEventShorthandTest.js',
2121
['Instead of .blur(fn) use .on("blur", fn). Instead of .blur() use .trigger("blur")']
2222
);
2323
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
$(document).ready(function(){
1+
$(document).ready(function () {
22
'use strict';
3-
$( "table" ).delegate( "td", "click", function() {
4-
$( this ).toggleClass( "chosen" );
3+
4+
$('table').delegate('td', 'click', function () {
5+
$(this).toggleClass('chosen');
56
});
6-
});
7+
});

0 commit comments

Comments
 (0)