8000 AC-201: Move phpcs checks from magento2 to magento-coding-standard repo · svera/magento-coding-standard@8054864 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8054864

Browse files
committed
AC-201: Move phpcs checks from magento2 to magento-coding-standard repo
- Move files from Less folder
1 parent 73181d9 commit 8054864

19 files changed

+1616
-0
lines changed

Magento2/Sniffs/Less/AvoidIdSniff.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Less;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Class AvoidIdSniff
13+
*
14+
* Ensure that id selector is not used
15+
*
16+
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#types
17+
*/
18+
class AvoidIdSniff implements Sniff
19+
{
20+
/**
21+
* A list of tokenizers this sniff supports.
22+
*
23+
* @var array
24+
*/
25+
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
26+
27+
/**
28+
* Tokens that can appear in a selector
29+
*
30+
* @var array
31+
*/
32+
private $selectorTokens = [
33+
T_HASH,
34+
T_WHITESPACE,
35+
T_STRING_CONCAT,
36+
T_OPEN_PARENTHESIS,
37+
T_CLOSE_PARENTHESIS,
38+
T_OPEN_SQUARE_BRACKET,
39+
T_CLOSE_SQUARE_BRACKET,
40+
T_DOUBLE_QUOTED_STRING,
41+
T_CONSTANT_ENCAPSED_STRING,
42+
T_DOUBLE_COLON,
43+
T_COLON,
44+
T_EQUAL,
45+
T_MUL_EQUAL,
46+
T_OR_EQUAL,
47+
T_STRING,
48+
T_NONE,
49+
T_DOLLAR,
50+
T_GREATER_THAN,
51+
T_PLUS,
52+
T_NS_SEPARATOR,
53+
T_LNUMBER,
54+
];
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function register()
60+
{
61+
return [T_HASH];
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*
67+
* Will flag any selector that looks like the following:
68+
* #foo[bar],
69+
* #foo[bar=bash],
70+
* #foo[bar~=bash],
71+
* #foo[bar$=bash],
72+
* #foo[bar*=bash],
73+
* #foo[bar|=bash],
74+
* #foo[bar='bash'],
75+
* #foo:hover,
76+
* #foo:nth-last-of-type(n),
77+
* #foo::before,
78+
* #foo + div,
79+
* #foo > div,
80+
* #foo ~ div,
81+
* #foo\3Abar ~ div,
82+
* #foo\:bar ~ div,
83+
* #foo.bar .baz,
84+
* div#foo {
85+
* blah: 'abc';
86+
* }
87+
*/
88+
public function process(File $phpcsFile, $stackPtr)
89+
{
90+
$tokens = $phpcsFile->getTokens();
91+
92+
// Find the next non-selector token
93+
$nextToken = $phpcsFile->findNext($this->selectorTokens, $stackPtr + 1, null, true);
94+
95+
// Anything except a { or a , means this is not a selector
96+
if ($nextToken !== false && in_array($tokens[$nextToken]['code'], [T_OPEN_CURLY_BRACKET, T_COMMA])) {
97+
$phpcsFile->addError('Id selector is used', $stackPtr, 'IdSelectorUsage');
98+
}
99+
}
100+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Less;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Util\Tokens;
11+
12+
/**
13+
* Class BracesFormattingSniff
14+
*
15+
* Ensure there is a single blank line after the closing brace of a class definition
16+
*
17+
* @see Squiz_Sniffs_CSS_ClassDefinitionClosingBraceSpaceSniff
18+
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#braces
19+
*/
20+
class BracesFormattingSniff implements Sniff
21+
{
22+
/**
23+
* A list of tokenizers this sniff supports.
24+
*
25+
* @var array
26+
*/
27+
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function register()
33+
{
34+
return [T_OPEN_CURLY_BRACKET, T_CLOSE_CURLY_BRACKET];
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function process(File $phpcsFile, $stackPtr)
41+
{
42+
$tokens = $phpcsFile->getTokens();
43+
44+
if (T_OPEN_CURLY_BRACKET === $tokens[$stackPtr]['code']) {
45+
if (TokenizerSymbolsInterface::WHITESPACE !== $tokens[$stackPtr - 1]['content']) {
46+
$phpcsFile->addError('Space before opening brace is missing', $stackPtr, 'SpacingBeforeOpen');
47+
}
48+
49+
return;
50+
}
51+
52+
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
53+
if ($next === false) {
54+
return;
55+
}
56+
57+
if (!in_array($tokens[$next]['code'], [T_CLOSE_TAG, T_CLOSE_CURLY_BRACKET])) {
58+
$found = (($tokens[$next]['line'] - $tokens[$stackPtr]['line']) - 1);
59+
if ($found !== 1) {
60+
$error = 'Expected one blank line after closing brace of class definition; %s found';
61+
$data = [$found];
62+
// Will be implemented in MAGETWO-49778
63+
//$phpcsFile->addWarning($error, $stackPtr, 'SpacingAfterClose', $data);
64+
}
65+
}
66+
67+
// Ignore nested style definitions from here on. The spacing before the closing brace
68+
// (a single blank line) will be enforced by the above check, which ensures there is a
69+
// blank line after the last nested class.
70+
$found = $phpcsFile->findPrevious(
< F42D /td>
71+
T_CLOSE_CURLY_BRACKET,
72+
($stackPtr - 1),
73+
$tokens[$stackPtr]['bracket_opener']
74+
);
75+
76+
if ($found !== false) {
77+
return;
78+
}
79+
80+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
81+
if ($prev !== false && $tokens[$prev]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
82+
$num = ($tokens[$stackPtr]['line'] - $tokens[$prev]['line'] - 1);
83+
$error = 'Expected 0 blank lines before closing brace of class definition; %s found';
84+
$data = [$num];
85+
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeClose', $data);
86+
}
87+
}
88+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Less;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
11+
/**
12+
* Class ClassNamingSniff
13+
*
14+
* Ensure that class name responds to the following requirements:
15+
*
16+
* - names should be lowercase;
17+
* - start with a letter (except helper classes);
18+
* - words should be separated with dash '-';
19+
*
20+
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#standard-classes
21+
*/
22+
class ClassNamingSniff implements Sniff
23+
{
24+
25+
const STRING_HELPER_CLASSES_PREFIX = '_';
26+
27+
const STRING_ALLOWED_UNDERSCORES = '__';
28+
29+
/**
30+
* A list of tokenizers this sniff supports.
31+
*
32+
* @var array
33+
*/
34+
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function register()
40+
{
41+
return [T_STRING_CONCAT];
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function process(File $phpcsFile, $stackPtr)
48+
{
49+
$tokens = $phpcsFile->getTokens();
50+
51+
if (T_WHITESPACE !== $tokens[$stackPtr - 1]['code']
52+
&& !in_array(
53+
$tokens[$stackPtr - 1]['content'],
54+
[
55+
TokenizerSymbolsInterface::INDENT_SPACES,
56+
TokenizerSymbolsInterface::NEW_LINE,
57+
]
58+
)
59+
) {
60+
return;
61+
}
62+
63+
$className = $tokens[$stackPtr + 1]['content'];
64+
if (preg_match_all('/[^a-z0-9\-_]/U', $className, $matches)) {
65+
$phpcsFile->addError('Class name contains not allowed symbols', $stackPtr, 'NotAllowedSymbol', $matches);
66+
}
67+
}
68+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Less;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Util\Tokens;
11+
12+
/**
13+
* Class ColonSpacingSniff
14+
*
15+
* Ensure that single quotes are used
16+
*
17+
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#properties-colon-indents
18+
*/
19+
class ColonSpacingSniff implements Sniff
20+
{
21+
/**
22+
* A list of tokenizers this sniff supports.
23+
*
24+
* @var array
25+
*/
26+
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function register()
32+
{
33+
return [T_COLON];
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function process(File $phpcsFile, $stackPtr)
40+
{
41+
$tokens = $phpcsFile->getTokens();
42+
43+
if ($this->needValidateSpaces($phpcsFile, $stackPtr, $tokens)) {
44+
$this->validateSpaces($phpcsFile, $stackPtr, $tokens);
45+
}
46+
}
47+
48+
/**
49+
* Check is it need to check spaces
50+
*
51+
* @param File $phpcsFile
52+
* @param int $stackPtr
53+
* @param array $tokens
54+
*
55+
* @return bool
56+
*/
57+
private function needValidateSpaces(File $phpcsFile, $stackPtr, $tokens)
58+
{
59+
$nextSemicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
60+
61+
if (false === $nextSemicolon
62+
|| ($tokens[$nextSemicolon]['line'] !== $tokens[$stackPtr]['line'])
63+
|| TokenizerSymbolsInterface::BITWISE_AND === $tokens[$stackPtr - 1]['content']
64+
) {
65+
return false;
66+
}
67+
68+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
69+
if ($tokens[$prev]['code'] !== T_STYLE) {
70+
// The colon is not part of a style definition.
71+
return false;
72+
}
73+
74+
if ($tokens[$prev]['content'] === 'progid') {
75+
// Special case for IE filters.
76+
return false;
77+
}
78+
79+
return true;
80+
}
81+
82+
/**
83+
* Validate Colon Spacing according to requirements
84+
*
85+
* @param File $phpcsFile
86+
* @param int $stackPtr
87+
* @param array $tokens
88+
*
89+
* @return void
90+
*/
91+
private function validateSpaces(File $phpcsFile, $stackPtr, array $tokens)
92+
{
93+
if (T_WHITESPACE === $tokens[($stackPtr - 1)]['code']) {
94+
$phpcsFile->addError('There must be no space before a colon in a style definition', $stackPtr, 'Before');
95+
}
96+
97+
if (T_WHITESPACE !== $tokens[($stackPtr + 1)]['code']) {
98+
$phpcsFile->addError('Expected 1 space after colon in style definition; 0 found', $stackPtr, 'NoneAfter');
99+
} else {
100+
$content = $tokens[($stackPtr + 1)]['content'];
101+
if (false === strpos($content, $phpcsFile->eolChar)) {
102+
$length = strlen($content);
103+
if ($length !== 1) {
104+
$error = 'Expected 1 space after colon in style definition; %s found';
105+
$phpcsFile->addError($error, $stackPtr, 'After');
106+
}
107+
} else {
108+
$error = 'Expected 1 space after colon in style definition; newline found';
109+
$phpcsFile->addError($error, $stackPtr, 'AfterNewline');
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)
0