-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathClassNamingSniff.php
83 lines (75 loc) · 2.14 KB
/
ClassNamingSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Copyright 2021 Adobe
* All Rights Reserved.
*/
namespace Magento2\Sniffs\Less;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Class ClassNamingSniff
*
* Ensure that class name responds to the following requirements:
*
* - names should be lowercase;
* - start with a letter (except helper classes);
* - words should be separated with dash '-';
*
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#standard-classes
*/
class ClassNamingSniff implements Sniff
{
private const STRING_HELPER_CLASSES_PREFIX = '_';
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
/**
* @inheritdoc
*/
public function register()
{
return [T_STRING_CONCAT];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (
T_WHITESPACE !== $tokens[$stackPtr - 1]['code']
&& !in_array(
$tokens[$stackPtr - 1]['content'],
[
TokenizerSymbolsInterface::INDENT_SPACES,
TokenizerSymbolsInterface::NEW_LINE,
]
)
) {
return;
}
$className = $tokens[$stackPtr + 1]['content'];
if (preg_match_all('/[^a-z0-9\-_]/U', $className, $matches)) {
$phpcsFile->addError(
'CSS class name does not follow class naming requirements: %s',
$stackPtr,
'NotAllowedSymbol',
[implode("", $matches[0])]
);
}
if (
strlen($className) > 1
&& strpos($className, self::STRING_HELPER_CLASSES_PREFIX, 2) !== false
&& !str_starts_with($className, 'admin__')
) {
$phpcsFile->addError(
'CSS class names should be separated with "-" (dash) instead of "_" (underscore)',
$stackPtr,
'NotAllowedSymbol'
);
}
}
}