Skip to content

Commit 2b58f92

Browse files
authored
Merge pull request #92 from magento-commerce/develop
Develop to Master Version 13
2 parents 67926bd + aa0c657 commit 2b58f92

Some content is hidden

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

43 files changed

+6963
-834
lines changed

.github/workflows/php.yml

+3-1
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Legacy;
7+
8+
use DOMDocument;
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
12+
/**
13+
* Test to find obsolete acl declaration
14+
*/
15+
class ObsoleteAclSniff implements Sniff
16+
{
17+
private const WARNING_OBSOLETE_ACL_STRUCTURE = 'ObsoleteAclStructure';
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function register(): array
23+
{
24+
return [
25+
T_INLINE_HTML
26+
];
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function process(File $phpcsFile, $stackPtr)
33+
{
34+
if ($stackPtr > 0) {
35+
return;
36+
}
37+
38+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
39+
$foundElements = $xml->xpath('/config/acl/*[boolean(./children) or boolean(./title)]');
40+
foreach ($foundElements as $element) {
41+
$phpcsFile->addWarning(
42+
'Obsolete acl structure detected in line ' . dom_import_simplexml($element)->getLineNo(),
43+
dom_import_simplexml($element)->getLineNo() - 1,
44+
self::WARNING_OBSOLETE_ACL_STRUCTURE
45+
);
46+
}
47+
}
48+
49+
/**
50+
* Format the incoming XML to avoid tags split into several lines.
51+
*
52+
* @param File $phpcsFile
53+
* @return false|string
54+
*/
55+
private function getFormattedXML(File $phpcsFile)
56+
{
57+
$doc = new DomDocument('1.0');
58+
$doc->formatOutput = true;
59+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
60+
return $doc->saveXML();
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Legacy;
7+
8+
use DOMDocument;
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
12+
/**
13+
* Test to find obsolete acl declaration
14+
*/
15+
class ObsoleteMenuSniff implements Sniff
16+
{
17+
private const WARNING_OBSOLETE_MENU_STRUCTURE = 'ObsoleteMenuStructure';
18+
19+
/**
20+
* @var string
21+
*/
22+
private $xpath = '/config/menu/*[boolean(./children) or boolean(./title) or boolean(./action)]';
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function register(): array
28+
{
29+
return [
30+
T_INLINE_HTML
31+
];
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function process(File $phpcsFile, $stackPtr)
38+
{
39+
if ($stackPtr > 0) {
40+
return;
41+
}
42+
43+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
44+
$foundElements = $xml->xpath($this->xpath);
45+
foreach ($foundElements as $element) {
46+
$phpcsFile->addWarning(
47+
'Obsolete menu structure detected in line ' . dom_import_simplexml($element)->getLineNo(),
48+
dom_import_simplexml($element)->getLineNo() - 1,
49+
self::WARNING_OBSOLETE_MENU_STRUCTURE
50+
);
51+
}
52+
}
53+
54+
/**
55+
* Format the incoming XML to avoid tags split into several lines.
56+
*
57+
* @param File $phpcsFile
58+
* @return false|string
59+
*/
60+
private function getFormattedXML(File $phpcsFile)
61+
{
62+
$doc = new DomDocument('1.0');
63+
$doc->formatOutput = true;
64+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
65+
return $doc->saveXML();
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use DOMDocument;
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
14+
class ObsoleteSystemConfigurationSniff implements Sniff
15+
{
16+
private const ERROR_CODE_XML = 'WrongXML';
17+
private const WARNING_CODE_OBSOLETE = 'FoundObsoleteSystemConfiguration';
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function register(): array
23+
{
24+
return [
25+
T_INLINE_HTML
26+
];
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function process(File $phpcsFile, $stackPtr)
33+
{
34+
if ($stackPtr > 0) {
35+
return;
36+
}
37+
38+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
39+
40+
if ($xml === false) {
41+
$this->invalidXML($phpcsFile, $stackPtr);
42+
return;
43+
}
44+
45+
$foundElements = $xml->xpath('/config/tabs|/config/sections');
46+
47+
if ($foundElements === false) {
48+
return;
49+
}
50+
51+
foreach ($foundElements as $element) {
52+
$phpcsFile->addWarning(
53+
"Obsolete system configuration structure detected in file.",
54+
dom_import_simplexml($element)->getLineNo() - 1,
55+
self::WARNING_CODE_OBSOLETE
56+
);
57+
}
58+
}
59+
60+
/**
61+
* Adds an invalid XML error
62+
*
63+
* @param File $phpcsFile
64+
* @param int $stackPtr
65+
*/
66+
private function invalidXML(File $phpcsFile, int $stackPtr): void
67+
{
68+
$phpcsFile->addError(
69+
sprintf(
70+
"Couldn't parse contents of '%s', check that they are in valid XML format.",
71+
$phpcsFile->getFilename(),
72+
),
73+
$stackPtr,
74+
self::ERROR_CODE_XML
75+
);
76+
}
77+
78+
/**
79+
* Format the incoming XML to avoid tags split into several lines.
80+
*
81+
* @param File $phpcsFile
82+
* @return false|string
83+
*/
84+
private function getFormattedXML(File $phpcsFile)
85+
{
86+
$doc = new DomDocument('1.0');
87+
$doc->formatOutput = true;
88+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
89+
return $doc->saveXML();
90+
}
91+
}
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+
}

0 commit comments

Comments
 (0)