Skip to content

Commit 2270b33

Browse files
committed
Merge branch 'develop' of github.com:magento/magento-coding-standard into AC-1314
2 parents 2c54b32 + b8d2c96 commit 2270b33

File tree

59 files changed

+7682
-838
lines changed

Some content is hidden

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

59 files changed

+7682
-838
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

Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,17 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
5757
$tokens = $phpcsFile->getTokens();
5858

5959
$commentEnd = $phpcsFile->findPrevious($this->ignoreTokens, ($stackPtr - 1), null, true);
60-
if ($commentEnd === false
60+
61+
if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_STRING) {
62+
$commentEnd = $phpcsFile->findPrevious($this->ignoreTokens, ($commentEnd - 1), null, true);
63+
} elseif ($commentEnd === false
6164
|| ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
6265
&& $tokens[$commentEnd]['code'] !== T_COMMENT)
6366
) {
6467
$phpcsFile->addWarning('Missing PHP DocBlock for class property.', $stackPtr, 'Missing');
6568
return;
6669
}
70+
6771
$commentStart = $tokens[$commentEnd]['comment_opener'];
6872
$foundVar = null;
6973
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {

Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,14 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
229229
'^strval$' => '(string) construction',
230230
'^htmlspecialchars$' => '\Magento\Framework\Escaper->escapeHtml',
231231
'^getimagesize$' => 'getimagesizefromstring',
232+
'^file_exists$' => 'Magento\Framework\Filesystem\DriverInterface::isExists()',
233+
'^file_get_contents$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetContents()',
234+
'^file_put_contents$' => 'Magento\Framework\Filesystem\DriverInterface::filePutContents()',
235+
'^fgetcsv$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetCsv()',
236+
'^fputcsv$' => 'Magento\Framework\Filesystem\DriverInterface::filePutCsv()',
237+
'^ftell$' => 'Magento\Framework\Filesystem\DriverInterface::fileTell()',
238+
'^fseek$' => 'Magento\Framework\Filesystem\DriverInterface::fileSeek()',
239+
'^feof$' => 'Magento\Framework\Filesystem\DriverInterface::endOfFile()',
240+
'^flock$' => 'Magento\Framework\Filesystem\DriverInterface::fileLock()',
232241
];
233242
}

Magento2/Sniffs/Legacy/InstallUpgradeSniff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public function process(File $phpcsFile, $stackPtr)
6464
}
6565
}
6666

67-
if (preg_match('/(sql|data)/', $fileInfo->getPath()) === 1) {
67+
$folders = array_filter(explode('/', $fileInfo->getPath()));
68+
$folderName = array_pop($folders);
69+
70+
if ($folderName === 'data' || $folderName === 'sql') {
6871
$phpcsFile->addError(
6972
$fileInfo->getFilename()." is in an invalid directory ".$fileInfo->getPath().":\n"
7073
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
Lines changed: 62 additions & 0 deletions
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+
}
Lines changed: 67 additions & 0 deletions
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+
}
Lines changed: 91 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)