Skip to content

Commit 6558e29

Browse files
committed
Merge branch 'develop' of github.com:magento/magento-coding-standard into AC-663_phpcs-ClassesTest
2 parents 34169b2 + b8d2c96 commit 6558e29

File tree

95 files changed

+11046
-52
lines changed

Some content is hidden

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

95 files changed

+11046
-52
lines changed

.github/workflows/php.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ jobs:
2525
name: Tests with PHP ${{ matrix.php-version }} and ${{ matrix.dependencies }} dependencies
2626

2727
steps:
28+
- name: Setup node
29+
uses: actions/setup-node@v2
30+
with:
31+
node-version: '16'
32+
2833
- uses: actions/checkout@v2
2934

3035
- name: Setup PHP
@@ -34,6 +39,13 @@ jobs:
3439
env:
3540
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3641

42+
- name: Install dependencies
43+
run: npm install
44+
- name: Run ESLint
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
48+
3749
- name: Validate composer
3850
run: composer validate
3951

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/vendor/*
22
/bin/*
3+
/node_modules
34

45
# IDE files
56
.idea/

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
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento2\Sniffs\Html;
10+
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use PHP_CodeSniffer\Files\File;
13+
14+
/**
15+
* Sniff for self-closing tags
16+
*/
17+
class HtmlSelfClosingTagsSniff implements Sniff
18+
{
19+
/**
20+
* List of void elements
21+
*
22+
* https://www.w3.org/TR/html51/syntax.html#writing-html-documents-elements
23+
*
24+
* @var string[]
25+
*/
26+
private $voidElements = [
27+
'area',
28+
'base',
29+
'br',
30+
'col',
31+
'embed',
32+
'hr',
33+
'img',
34+
'input',
35+
'keygen',
36+
'link',
37+
'menuitem',
38+
'meta',
39+
'param',
40+
'source',
41+
'track',
42+
'wbr',
43+
];
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function register()
49+
{
50+
return [T_INLINE_HTML];
51+
}
52+
53+
/**
54+
* Detect use of self-closing tag with non-void html element
55+
*
56+
* @param File $phpcsFile
57+
* @param int $stackPtr
58+
* @return int|void
59+
*/
60+
public function process(File $phpcsFile, $stackPtr)
61+
{
62+
if ($stackPtr !== 0) {
63+
return;
64+
}
65+
$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()));
66+
67+
if (empty($html)) {
68+
return;
69+
}
70+
71+
if (preg_match_all('$<(\w{2,})\s?[^<]*\/>$', $html, $matches, PREG_SET_ORDER)) {
72+
foreach ($matches as $match) {
73+
if (!in_array($match[1], $this->voidElements)) {
74+
$phpcsFile->addError(
75+
'Avoid using self-closing tag with non-void html element'
76+
. ' - "' . $match[0] . PHP_EOL,
77+
null,
78+
'HtmlSelfClosingNonVoidTag'
79+
);
80+
}
81+
}
82+
}
83+
}
84+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use SplFileInfo;
13+
14+
class InstallUpgradeSniff implements Sniff
15+
{
16+
private const ERROR_CODE = 'obsoleteScript';
17+
18+
/**
19+
* @var string[]
20+
*/
21+
private $wrongPrefixes = [
22+
'install-' => 'Install scripts are obsolete. '
23+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
24+
'InstallSchema' => 'InstallSchema scripts are obsolete. '
25+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
26+
'InstallData' => 'InstallData scripts are obsolete. '
27+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
28+
'data-install-' => 'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder',
29+
'upgrade-' => 'Upgrade scripts are obsolete. '
30+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
31+
'UpgradeSchema' => 'UpgradeSchema scripts are obsolete. '
32+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
33+
'UpgradeData' => 'UpgradeSchema scripts are obsolete. '
34+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
35+
'data-upgrade-' => 'Upgrade scripts are obsolete. '
36+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
37+
'recurring' => 'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder',
38+
];
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function register(): array
44+
{
45+
return [
46+
T_OPEN_TAG
47+
];
48+
}
49+
50+
/**
51+
* @inheritDoc
52+
*/
53+
public function process(File $phpcsFile, $stackPtr)
54+
{
55+
if ($stackPtr > 0) {
56+
return;
57+
}
58+
59+
$fileInfo = new SplFileInfo($phpcsFile->getFilename());
60+
61+
foreach ($this->wrongPrefixes as $prefix => $errorMessage) {
62+
if (strpos($fileInfo->getFilename(), $prefix) === 0) {
63+
$phpcsFile->addError($errorMessage, 0, self::ERROR_CODE);
64+
}
65+
}
66+
67+
$folders = array_filter(explode('/', $fileInfo->getPath()));
68+
$folderName = array_pop($folders);
69+
70+
if ($folderName === 'data' || $folderName === 'sql') {
71+
$phpcsFile->addError(
72+
$fileInfo->getFilename()." is in an invalid directory ".$fileInfo->getPath().":\n"
73+
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
74+
. "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes.",
75+
0,
76+
self::ERROR_CODE
77+
);
78+
}
79+
}
80+
}
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+
}

0 commit comments

Comments
 (0)