Skip to content
12 changes: 4 additions & 8 deletions Magento2/Sniffs/Legacy/AbstractBlockSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ class AbstractBlockSniff implements Sniff
private const CHILD_HTML_METHOD = 'getChildHtml';
private const CHILD_CHILD_HTML_METHOD = 'getChildChildHtml';

/**
* Error violation code.
*
* @var string
*/
protected $errorCode = 'FoundCountOfParametersIncorrect';
private const ERROR_CODE_THIRD_PARAMETER = 'ThirdParameterNotNeeded';
private const ERROR_CODE_FOURTH_PARAMETER = 'FourthParameterNotNeeded';

/**
* @inheritdoc
Expand Down Expand Up @@ -52,14 +48,14 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addError(
'3rd parameter is not needed anymore for getChildHtml()',
$stackPtr,
$this->errorCode
self::ERROR_CODE_THIRD_PARAMETER
);
}
if ($content === self::CHILD_CHILD_HTML_METHOD && $paramsCount >= 4) {
$phpcsFile->addError(
'4th parameter is not needed anymore for getChildChildHtml()',
$stackPtr,
$this->errorCode
self::ERROR_CODE_FOURTH_PARAMETER
);
}
}
Expand Down
40 changes: 25 additions & 15 deletions Magento2/Sniffs/Legacy/DiConfigSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@

class DiConfigSniff implements Sniff
{
private const WARNING_CODE = 'FoundObsoleteAttribute';

/**
* @var string[] Associative array containing the obsolete nodes and the message to display when they are found.
*/
private $obsoleteDiNodes = [
'<param' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="...">',
'<instance' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object">',
'<array' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array">',
'<item key=' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="...">',
'<value' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal.'
private const OBSOLETE_NODES = [
'FoundObsoleteParamNode' => [
'pattern' => '<param',
'message' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="...">'
],
'FoundObsoleteInstanceNode' => [
'pattern' => '<instance',
'message' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object">>'
],
'FoundObsoleteArrayNode' => [
'pattern' => '<array',
'message' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array">'
],
'FoundObsoleteItemNode' => [
'pattern' => '<item key=',
'message' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="...">'
],
'FoundObsoleteValueNode' => [
'pattern' => '<value',
'message' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal'
],
];

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

foreach ($this->obsoleteDiNodes as $element => $message) {
if (strpos($lineContent, $element) !== false) {
foreach (self::OBSOLETE_NODES as $code => $data) {
if (strpos($lineContent, $data['pattern']) !== false) {
$phpcsFile->addWarning(
$message,
$data['message'],
$stackPtr,
self::WARNING_CODE
$code
);
}
}
Expand Down
20 changes: 12 additions & 8 deletions Magento2/Sniffs/Legacy/EmailTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
class EmailTemplateSniff implements Sniff
{
private const OBSOLETE_EMAIL_DIRECTIVES = [
'/\{\{htmlescape.*?\}\}/i' => 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.',
'/\{\{escapehtml.*?\}\}/i' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
'FoundObsoleteHtmlescapeDirective' => [
'pattern' => '/\{\{htmlescape.*?\}\}/i',
'message' => 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.',
],
'FoundObsoleteEscapehtmlDirective' => [
'pattern' => '/\{\{escapehtml.*?\}\}/i',
'message' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
],
];

private const ERROR_CODE = 'FoundObsoleteEmailDirective';

/**
* @inheritdoc
*/
Expand All @@ -37,12 +41,12 @@ public function register(): array
public function process(File $phpcsFile, $stackPtr)
{
$content = $phpcsFile->getTokens()[$stackPtr]['content'];
foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $directiveRegex => $errorMessage) {
if (preg_match($directiveRegex, $content)) {
foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $code => $data) {
if (preg_match($data['pattern'], $content)) {
$phpcsFile->addError(
$errorMessage,
$data['message'],
$stackPtr,
self::ERROR_CODE
$code
);
}
}
Expand Down
74 changes: 52 additions & 22 deletions Magento2/Sniffs/Legacy/InstallUpgradeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,58 @@

class InstallUpgradeSniff implements Sniff
{
private const ERROR_CODE = 'obsoleteScript';
private const WRONG_PREFIXES = [
'ObsoleteInstallScript' => [
'pattern' => 'install-',
'message' => 'Install scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
],
'ObsoleteInstallSchemaScript' => [
'pattern' => 'InstallSchema',
'message' => 'InstallSchema scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
],
'ObsoleteInstallDataScript' => [
'pattern' => 'InstallData',
'message' => 'InstallData scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
],
'ObsoleteDataInstallScript' => [
'pattern' => 'data-install-',
'message' => 'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder',
],
'ObsoleteUpgradeScript' => [
'pattern' => 'upgrade-',
'message' => 'Upgrade scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
],
'ObsoleteUpgradeSchemaScript' => [
'pattern' => 'UpgradeSchema',
'message' => 'UpgradeSchema scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
],
'ObsoleteUpgradeDataScript' => [
'pattern' => 'UpgradeData',
'message' => 'UpgradeData scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
],
'ObsoleteDataUpgradeScript' => [
'pattern' => 'data-upgrade',
'message' => 'Upgrade scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
],
'ObsoleteRecurringScript' => [
'pattern' => 'recurring',
'message' => 'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder'
]
];

/**
* @var string[]
*/
private $wrongPrefixes = [
'install-' => 'Install scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
'InstallSchema' => 'InstallSchema scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
'InstallData' => 'InstallData scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
'data-install-' => 'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder',
'upgrade-' => 'Upgrade scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
'UpgradeSchema' => 'UpgradeSchema scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
'UpgradeData' => 'UpgradeSchema scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
'data-upgrade-' => 'Upgrade scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
'recurring' => 'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder',
private const INVALID_DIRECTORIES_ERROR_CODES = [
'data' => 'DataInvalidDirectory',
'sql' => 'SqlInvalidDirectory'
];

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

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

foreach ($this->wrongPrefixes as $prefix => $errorMessage) {
if (strpos($fileInfo->getFilename(), $prefix) === 0) {
$phpcsFile->addError($errorMessage, 0, self::ERROR_CODE);
foreach (self::WRONG_PREFIXES as $code => $data) {
if (strpos($fileInfo->getFilename(), $data['pattern']) === 0) {
$phpcsFile->addError($data['message'], 0, $code);
}
}

Expand All @@ -73,7 +103,7 @@ public function process(File $phpcsFile, $stackPtr)
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
. "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes.",
0,
self::ERROR_CODE
self::INVALID_DIRECTORIES_ERROR_CODES[$folderName]
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions Magento2/Sniffs/Legacy/LayoutSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class LayoutSniff implements Sniff
{
private const ERROR_CODE_XML = 'WrongXML';
private const ERROR_CODE_NOT_ALLOWED = 'NotAllowed';
private const ERROR_CODE_OBSOLETE = 'Obsolete';
private const ERROR_CODE_OBSOLETE_BLOCK = 'ObsoleteBlock';
private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass';
private const ERROR_CODE_OBSOLETE_TOHTML_ATTRIBUTE = 'ObsoleteToHtmlAttribute';
private const ERROR_CODE_METHOD_NOT_ALLOWED = 'MethodNotAllowed';
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED = 'CharacterNotAllowedInAttribute';
private const ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED = 'CharacterExpectedInAttribute';
Expand Down Expand Up @@ -254,7 +255,7 @@ private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFil
$phpcsFile->addError(
'The block being referenced is removed.',
dom_import_simplexml($reference)->getLineNo()-1,
self::ERROR_CODE_OBSOLETE
self::ERROR_CODE_OBSOLETE_BLOCK
);
}
}
Expand Down Expand Up @@ -317,7 +318,7 @@ private function testOutputAttribute(SimpleXMLElement $layout, File $phpcsFile):
$phpcsFile->addError(
'output="toHtml" is obsolete. Use output="1"',
dom_import_simplexml($elements[0])->getLineNo()-1,
self::ERROR_CODE_OBSOLETE
self::ERROR_CODE_OBSOLETE_TOHTML_ATTRIBUTE
);
};
}
Expand Down
6 changes: 3 additions & 3 deletions Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

class ObsoleteConnectionSniff implements Sniff
{
private const ERROR_CODE_METHOD = 'FoundObsoleteMethod';

/**
* @var string[]
*/
Expand All @@ -28,6 +26,8 @@ class ObsoleteConnectionSniff implements Sniff
'getWriteAdapter',
];

private const OBSOLETE_METHOD_ERROR_CODE = 'ObsoleteMethodFound';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -63,7 +63,7 @@ private function validateObsoleteMethod(File $phpcsFile, int $stackPtr)
$phpcsFile->addWarning(
sprintf("Contains obsolete method: %s. Please use getConnection method instead.", $method),
$stackPtr,
self::ERROR_CODE_METHOD
self::OBSOLETE_METHOD_ERROR_CODE
);
}
}
Expand Down
20 changes: 17 additions & 3 deletions Magento2/Sniffs/Legacy/ObsoleteResponseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

class ObsoleteResponseSniff implements Sniff
{
private const WARNING_CODE_METHOD = 'FoundObsoleteResponseMethod';

/**
* @var string[]
*/
Expand All @@ -29,6 +27,22 @@ class ObsoleteResponseSniff implements Sniff
'_addJs' => 'Please use \Magento\Backend\Model\View\Result\Page::addJs instead.',
'_moveBlockToContainer' => 'Please use \Magento\Backend\Model\View\Result\Page::moveBlockToContainer instead.',
];

/**
* @var string[]
*/
private $obsoleteResponseWarningCodes = [
'loadLayout' => 'LoadLayoutResponseMethodFound',
'renderLayout' => 'RenderLayoutResponseMethodFound',
'_redirect' => 'RedirectResponseMethodFound',
'_forward' => 'ForwardResponseMethodFound',
'_setActiveMenu' => 'SetActiveMenuResponseMethodFound',
'_addBreadcrumb' => 'AddBreadcrumbResponseMethodFound',
'_addContent' => 'AddContentResponseMethodFound',
'_addLeft' => 'AddLeftResponseMethodFound',
'_addJs' => 'AddJsResponseMethodFound',
'_moveBlockToContainer' => 'MoveBlockToContainerResponseMethodFound',
];

/**
* @inheritdoc
Expand All @@ -54,7 +68,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addWarning(
sprintf('%s method is deprecated. %s', $method, $errorMessage),
$stackPtr,
self::WARNING_CODE_METHOD
$this->obsoleteResponseWarningCodes[$method]
);
}
}
Expand Down
38 changes: 23 additions & 15 deletions Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@

class PhtmlTemplateSniff implements Sniff
{
private const WARNING_CODE = 'PhtmlTemplateObsolete';
private const WARNING_CODE_TEXT_JAVASCRIPT = 'TextJavascriptTypeFound';
private const WARNING_CODE_THIS_USAGE = 'ThisUsageObsolete';
private const WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS = 'ProtectedPrivateBlockAccess';

private const OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES = [
'/(["\'])jquery\/ui\1/' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ' .
'ui widget instead.',
'/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/' => 'Please do not initialize JS component in php. Do ' .
'it in template.',
'@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i' => 'Please do not initialize JS component ' .
'in php. Do it in template.',
private const OBSOLETE_REGEX = [
'FoundJQueryUI' => [
'pattern' => '/(["\'])jquery\/ui\1/',
'message' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ui widget instead'
],
'FoundDataMageInit' => [
'pattern' => '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/',
'message' => 'Please do not initialize JS component in php. Do it in template'
],
'FoundXMagentoInit' => [
'pattern' => '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i',
'message' => 'Please do not initialize JS component in php. Do it in template'
],
];

/**
Expand Down Expand Up @@ -77,7 +85,7 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token
'Access to protected and private members of Block class is ' .
'obsolete in phtml templates. Use only public members.',
$stringPos,
self::WARNING_CODE
self::WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS
);
}
}
Expand All @@ -101,7 +109,7 @@ private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens
'Access to members and methods of Block class through $this is ' .
'obsolete in phtml templates. Use only $block instead of $this.',
$stringPos,
self::WARNING_CODE
self::WARNING_CODE_THIS_USAGE
);
}
}
Expand All @@ -120,7 +128,7 @@ private function checkHtml(File $phpcsFile, int $stackPtr): void
$phpcsFile->addWarning(
'Please do not use "text/javascript" type attribute.',
$stackPtr,
self::WARNING_CODE
self::WARNING_CODE_TEXT_JAVASCRIPT
);
}
}
Expand All @@ -135,12 +143,12 @@ private function checkHtmlSpecificFiles(File $phpcsFile, int $stackPtr): void
{
$content = $phpcsFile->getTokensAsString($stackPtr, 1);

foreach (self::OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES as $obsoleteRegex => $errorMessage) {
if (preg_match($obsoleteRegex, $content)) {
foreach (self::OBSOLETE_REGEX as $code => $data) {
if (preg_match($data['pattern'], $content)) {
$phpcsFile->addWarning(
$errorMessage,
$data['message'],
$stackPtr,
self::WARNING_CODE
$code
);
}
}
Expand Down
Loading