Skip to content

Commit 67a798c

Browse files
committed
Merge branch 'develop' of github.com:magento/magento-coding-standard into update-from-public
2 parents 409fd33 + 8f6ddba commit 67a798c

15 files changed

+170
-99
lines changed

Magento2/Sniffs/Legacy/AbstractBlockSniff.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ class AbstractBlockSniff implements Sniff
1515
private const CHILD_HTML_METHOD = 'getChildHtml';
1616
private const CHILD_CHILD_HTML_METHOD = 'getChildChildHtml';
1717

18-
/**
19-
* Error violation code.
20-
*
21-
* @var string
22-
*/
23-
protected $errorCode = 'FoundCountOfParametersIncorrect';
18+
private const ERROR_CODE_THIRD_PARAMETER = 'ThirdParameterNotNeeded';
19+
private const ERROR_CODE_FOURTH_PARAMETER = 'FourthParameterNotNeeded';
2420

2521
/**
2622
* @inheritdoc
@@ -52,14 +48,14 @@ public function process(File $phpcsFile, $stackPtr)
5248
$phpcsFile->addError(
5349
'3rd parameter is not needed anymore for getChildHtml()',
5450
$stackPtr,
55-
$this->errorCode
51+
self::ERROR_CODE_THIRD_PARAMETER
5652
);
5753
}
5854
if ($content === self::CHILD_CHILD_HTML_METHOD && $paramsCount >= 4) {
5955
$phpcsFile->addError(
6056
'4th parameter is not needed anymore for getChildChildHtml()',
6157
$stackPtr,
62-
$this->errorCode
58+
self::ERROR_CODE_FOURTH_PARAMETER
6359
);
6460
}
6561
}

Magento2/Sniffs/Legacy/DiConfigSniff.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@
1111

1212
class DiConfigSniff implements Sniff
1313
{
14-
private const WARNING_CODE = 'FoundObsoleteAttribute';
15-
16-
/**
17-
* @var string[] Associative array containing the obsolete nodes and the message to display when they are found.
18-
*/
19-
private $obsoleteDiNodes = [
20-
'<param' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="...">',
21-
'<instance' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object">',
22-
'<array' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array">',
23-
'<item key=' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="...">',
24-
'<value' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal.'
14+
private const OBSOLETE_NODES = [
15+
'FoundObsoleteParamNode' => [
16+
'pattern' => '<param',
17+
'message' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="...">'
18+
],
19+
'FoundObsoleteInstanceNode' => [
20+
'pattern' => '<instance',
21+
'message' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object">>'
22+
],
23+
'FoundObsoleteArrayNode' => [
24+
'pattern' => '<array',
25+
'message' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array">'
26+
],
27+
'FoundObsoleteItemNode' => [
28+
'pattern' => '<item key=',
29+
'message' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="...">'
30+
],
31+
'FoundObsoleteValueNode' => [
32+
'pattern' => '<value',
33+
'message' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal'
34+
],
2535
];
2636

2737
/**
@@ -41,12 +51,12 @@ public function process(File $phpcsFile, $stackPtr)
4151
{
4252
$lineContent = $phpcsFile->getTokensAsString($stackPtr, 1);
4353

44-
foreach ($this->obsoleteDiNodes as $element => $message) {
45-
if (strpos($lineContent, $element) !== false) {
54+
foreach (self::OBSOLETE_NODES as $code => $data) {
55+
if (strpos($lineContent, $data['pattern']) !== false) {
4656
$phpcsFile->addWarning(
47-
$message,
57+
$data['message'],
4858
$stackPtr,
49-
self::WARNING_CODE
59+
$code
5060
);
5161
}
5262
}

Magento2/Sniffs/Legacy/EmailTemplateSniff.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
class EmailTemplateSniff implements Sniff
1616
{
1717
private const OBSOLETE_EMAIL_DIRECTIVES = [
18-
'/\{\{htmlescape.*?\}\}/i' => 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.',
19-
'/\{\{escapehtml.*?\}\}/i' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
18+
'FoundObsoleteHtmlescapeDirective' => [
19+
'pattern' => '/\{\{htmlescape.*?\}\}/i',
20+
'message' => 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.',
21+
],
22+
'FoundObsoleteEscapehtmlDirective' => [
23+
'pattern' => '/\{\{escapehtml.*?\}\}/i',
24+
'message' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
25+
],
2026
];
2127

22-
private const ERROR_CODE = 'FoundObsoleteEmailDirective';
23-
2428
/**
2529
* @inheritdoc
2630
*/
@@ -37,12 +41,12 @@ public function register(): array
3741
public function process(File $phpcsFile, $stackPtr)
3842
{
3943
$content = $phpcsFile->getTokens()[$stackPtr]['content'];
40-
foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $directiveRegex => $errorMessage) {
41-
if (preg_match($directiveRegex, $content)) {
44+
foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $code => $data) {
45+
if (preg_match($data['pattern'], $content)) {
4246
$phpcsFile->addError(
43-
$errorMessage,
47+
$data['message'],
4448
$stackPtr,
45-
self::ERROR_CODE
49+
$code
4650
);
4751
}
4852
}

Magento2/Sniffs/Legacy/InstallUpgradeSniff.php

+52-22
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,58 @@
1313

1414
class InstallUpgradeSniff implements Sniff
1515
{
16-
private const ERROR_CODE = 'obsoleteScript';
16+
private const WRONG_PREFIXES = [
17+
'ObsoleteInstallScript' => [
18+
'pattern' => 'install-',
19+
'message' => 'Install scripts are obsolete. '
20+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
21+
],
22+
'ObsoleteInstallSchemaScript' => [
23+
'pattern' => 'InstallSchema',
24+
'message' => 'InstallSchema scripts are obsolete. '
25+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
26+
],
27+
'ObsoleteInstallDataScript' => [
28+
'pattern' => 'InstallData',
29+
'message' => 'InstallData scripts are obsolete. '
30+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
31+
],
32+
'ObsoleteDataInstallScript' => [
33+
'pattern' => 'data-install-',
34+
'message' => 'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder',
35+
],
36+
'ObsoleteUpgradeScript' => [
37+
'pattern' => 'upgrade-',
38+
'message' => 'Upgrade scripts are obsolete. '
39+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
40+
],
41+
'ObsoleteUpgradeSchemaScript' => [
42+
'pattern' => 'UpgradeSchema',
43+
'message' => 'UpgradeSchema scripts are obsolete. '
44+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
45+
],
46+
'ObsoleteUpgradeDataScript' => [
47+
'pattern' => 'UpgradeData',
48+
'message' => 'UpgradeData scripts are obsolete. '
49+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
50+
],
51+
'ObsoleteDataUpgradeScript' => [
52+
'pattern' => 'data-upgrade',
53+
'message' => 'Upgrade scripts are obsolete. '
54+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
55+
],
56+
'ObsoleteRecurringScript' => [
57+
'pattern' => 'recurring',
58+
'message' => 'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder'
59+
]
60+
];
1761

1862
/**
1963
* @var string[]
2064
*/
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',
65+
private const INVALID_DIRECTORIES_ERROR_CODES = [
66+
'data' => 'DataInvalidDirectory',
67+
'sql' => 'SqlInvalidDirectory'
3868
];
3969

4070
/**
@@ -58,9 +88,9 @@ public function process(File $phpcsFile, $stackPtr)
5888

5989
$fileInfo = new SplFileInfo($phpcsFile->getFilename());
6090

61-
foreach ($this->wrongPrefixes as $prefix => $errorMessage) {
62-
if (strpos($fileInfo->getFilename(), $prefix) === 0) {
63-
$phpcsFile->addError($errorMessage, 0, self::ERROR_CODE);
91+
foreach (self::WRONG_PREFIXES as $code => $data) {
92+
if (strpos($fileInfo->getFilename(), $data['pattern']) === 0) {
93+
$phpcsFile->addError($data['message'], 0, $code);
6494
}
6595
}
6696

@@ -73,7 +103,7 @@ public function process(File $phpcsFile, $stackPtr)
73103
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
74104
. "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes.",
75105
0,
76-
self::ERROR_CODE
106+
self::INVALID_DIRECTORIES_ERROR_CODES[$folderName]
77107
);
78108
}
79109
}

Magento2/Sniffs/Legacy/LayoutSniff.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class LayoutSniff implements Sniff
1919
{
2020
private const ERROR_CODE_XML = 'WrongXML';
2121
private const ERROR_CODE_NOT_ALLOWED = 'NotAllowed';
22-
private const ERROR_CODE_OBSOLETE = 'Obsolete';
22+
private const ERROR_CODE_OBSOLETE_BLOCK = 'ObsoleteBlock';
2323
private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass';
24+
private const ERROR_CODE_OBSOLETE_TOHTML_ATTRIBUTE = 'ObsoleteToHtmlAttribute';
2425
private const ERROR_CODE_METHOD_NOT_ALLOWED = 'MethodNotAllowed';
2526
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED = 'CharacterNotAllowedInAttribute';
2627
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED = 'CharacterExpectedInAttribute';
@@ -254,7 +255,7 @@ private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFil
254255
$phpcsFile->addError(
255256
'The block being referenced is removed.',
256257
dom_import_simplexml($reference)->getLineNo()-1,
257-
self::ERROR_CODE_OBSOLETE
258+
self::ERROR_CODE_OBSOLETE_BLOCK
258259
);
259260
}
260261
}
@@ -317,7 +318,7 @@ private function testOutputAttribute(SimpleXMLElement $layout, File $phpcsFile):
317318
$phpcsFile->addError(
318319
'output="toHtml" is obsolete. Use output="1"',
319320
dom_import_simplexml($elements[0])->getLineNo()-1,
320-
self::ERROR_CODE_OBSOLETE
321+
self::ERROR_CODE_OBSOLETE_TOHTML_ATTRIBUTE
321322
);
322323
};
323324
}

Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
class ObsoleteConnectionSniff implements Sniff
1414
{
15-
private const ERROR_CODE_METHOD = 'FoundObsoleteMethod';
16-
1715
/**
1816
* @var string[]
1917
*/
@@ -28,6 +26,8 @@ class ObsoleteConnectionSniff implements Sniff
2826
'getWriteAdapter',
2927
];
3028

29+
private const OBSOLETE_METHOD_ERROR_CODE = 'ObsoleteMethodFound';
30+
3131
/**
3232
* @inheritdoc
3333
*/
@@ -63,7 +63,7 @@ private function validateObsoleteMethod(File $phpcsFile, int $stackPtr)
6363
$phpcsFile->addWarning(
6464
sprintf("Contains obsolete method: %s. Please use getConnection method instead.", $method),
6565
$stackPtr,
66-
self::ERROR_CODE_METHOD
66+
self::OBSOLETE_METHOD_ERROR_CODE
6767
);
6868
}
6969
}

Magento2/Sniffs/Legacy/ObsoleteResponseSniff.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
class ObsoleteResponseSniff implements Sniff
1414
{
15-
private const WARNING_CODE_METHOD = 'FoundObsoleteResponseMethod';
16-
1715
/**
1816
* @var string[]
1917
*/
@@ -29,6 +27,22 @@ class ObsoleteResponseSniff implements Sniff
2927
'_addJs' => 'Please use \Magento\Backend\Model\View\Result\Page::addJs instead.',
3028
'_moveBlockToContainer' => 'Please use \Magento\Backend\Model\View\Result\Page::moveBlockToContainer instead.',
3129
];
30+
31+
/**
32+
* @var string[]
33+
*/
34+
private $obsoleteResponseWarningCodes = [
35+
'loadLayout' => 'LoadLayoutResponseMethodFound',
36+
'renderLayout' => 'RenderLayoutResponseMethodFound',
37+
'_redirect' => 'RedirectResponseMethodFound',
38+
'_forward' => 'ForwardResponseMethodFound',
39+
'_setActiveMenu' => 'SetActiveMenuResponseMethodFound',
40+
'_addBreadcrumb' => 'AddBreadcrumbResponseMethodFound',
41+
'_addContent' => 'AddContentResponseMethodFound',
42+
'_addLeft' => 'AddLeftResponseMethodFound',
43+
'_addJs' => 'AddJsResponseMethodFound',
44+
'_moveBlockToContainer' => 'MoveBlockToContainerResponseMethodFound',
45+
];
3246

3347
/**
3448
* @inheritdoc
@@ -54,7 +68,7 @@ public function process(File $phpcsFile, $stackPtr)
5468
$phpcsFile->addWarning(
5569
sprintf('%s method is deprecated. %s', $method, $errorMessage),
5670
$stackPtr,
57-
self::WARNING_CODE_METHOD
71+
$this->obsoleteResponseWarningCodes[$method]
5872
);
5973
}
6074
}

Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php

+23-15
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@
1212

1313
class PhtmlTemplateSniff implements Sniff
1414
{
15-
private const WARNING_CODE = 'PhtmlTemplateObsolete';
15+
private const WARNING_CODE_TEXT_JAVASCRIPT = 'TextJavascriptTypeFound';
16+
private const WARNING_CODE_THIS_USAGE = 'ThisUsageObsolete';
17+
private const WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS = 'ProtectedPrivateBlockAccess';
1618

17-
private const OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES = [
18-
'/(["\'])jquery\/ui\1/' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ' .
19-
'ui widget instead.',
20-
'/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/' => 'Please do not initialize JS component in php. Do ' .
21-
'it in template.',
22-
'@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i' => 'Please do not initialize JS component ' .
23-
'in php. Do it in template.',
19+
private const OBSOLETE_REGEX = [
20+
'FoundJQueryUI' => [
21+
'pattern' => '/(["\'])jquery\/ui\1/',
22+
'message' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ui widget instead'
23+
],
24+
'FoundDataMageInit' => [
25+
'pattern' => '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/',
26+
'message' => 'Please do not initialize JS component in php. Do it in template'
27+
],
28+
'FoundXMagentoInit' => [
29+
'pattern' => '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i',
30+
'message' => 'Please do not initialize JS component in php. Do it in template'
31+
],
2432
];
2533

2634
/**
@@ -77,7 +85,7 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token
7785
'Access to protected and private members of Block class is ' .
7886
'obsolete in phtml templates. Use only public members.',
7987
$stringPos,
80-
self::WARNING_CODE
88+
self::WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS
8189
);
8290
}
8391
}
@@ -101,7 +109,7 @@ private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens
101109
'Access to members and methods of Block class through $this is ' .
102110
'obsolete in phtml templates. Use only $block instead of $this.',
103111
$stringPos,
104-
self::WARNING_CODE
112+
self::WARNING_CODE_THIS_USAGE
105113
);
106114
}
107115
}
@@ -120,7 +128,7 @@ private function checkHtml(File $phpcsFile, int $stackPtr): void
120128
$phpcsFile->addWarning(
121129
'Please do not use "text/javascript" type attribute.',
122130
$stackPtr,
123-
self::WARNING_CODE
131+
self::WARNING_CODE_TEXT_JAVASCRIPT
124132
);
125133
}
126134
}
@@ -135,12 +143,12 @@ private function checkHtmlSpecificFiles(File $phpcsFile, int $stackPtr): void
135143
{
136144
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
137145

138-
foreach (self::OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES as $obsoleteRegex => $errorMessage) {
139-
if (preg_match($obsoleteRegex, $content)) {
146+
foreach (self::OBSOLETE_REGEX as $code => $data) {
147+
if (preg_match($data['pattern'], $content)) {
140148
$phpcsFile->addWarning(
141-
$errorMessage,
149+
$data['message'],
142150
$stackPtr,
143-
self::WARNING_CODE
151+
$code
144152
);
145153
}
146154
}

0 commit comments

Comments
 (0)