Skip to content

AC-1549: Create codes for each type of returned error / warning #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 26, 2021
Merged
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
13 changes: 11 additions & 2 deletions Magento2/Sniffs/Legacy/DiConfigSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@

class DiConfigSniff implements Sniff
{
private const WARNING_CODE = 'FoundObsoleteAttribute';
/**
* @var string[]
*/
private $obsoleteDiNodesWarningCodes = [
'<param' => 'FoundObsoleteParamNode',
'<instance' => 'FoundObsoleteInstanceNode',
'<array' => 'FoundObsoleteArrayNode',
'<item key=' => 'FoundObsoleteItemNode',
'<value' => 'FoundObsoleteValueNode',
];

/**
* @var string[] Associative array containing the obsolete nodes and the message to display when they are found.
Expand Down Expand Up @@ -46,7 +55,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addWarning(
$message,
$stackPtr,
self::WARNING_CODE
$this->obsoleteDiNodesWarningCodes[$element]
);
}
}
Expand Down
10 changes: 8 additions & 2 deletions Magento2/Sniffs/Legacy/EmailTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ class EmailTemplateSniff implements Sniff
'/\{\{escapehtml.*?\}\}/i' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
];

private const ERROR_CODE = 'FoundObsoleteEmailDirective';
/**
* @var string[]
*/
private $errorCodes = [
'/\{\{htmlescape.*?\}\}/i' => 'FoundObsoleteHtmlescapeDirective',
'/\{\{escapehtml.*?\}\}/i' => 'FoundObsoleteEscapehtmlDirective'
];

/**
* @inheritdoc
Expand All @@ -42,7 +48,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addError(
$errorMessage,
$stackPtr,
self::ERROR_CODE
$this->errorCodes[$directiveRegex]
);
}
}
Expand Down
21 changes: 18 additions & 3 deletions Magento2/Sniffs/Legacy/InstallUpgradeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class InstallUpgradeSniff implements Sniff
{
private const ERROR_CODE = 'obsoleteScript';
private const ERROR_CODE = 'invalidDirectory';

/**
* @var string[]
Expand All @@ -30,13 +30,28 @@ class InstallUpgradeSniff implements Sniff
. '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. '
'UpgradeData' => 'UpgradeData 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',
];

/**
* @var string[]
*/
private $wrongPrefixesErrorCodes = [
'install-' => 'obsoleteInstallScript',
'InstallSchema' => 'obsoleteInstallSchemaScript',
'InstallData' => 'obsoleteInstallDataScript',
'data-install-' => 'obsoleteDataInstallScript',
'upgrade-' => 'obsoleteUpgradeScript',
'UpgradeSchema' => 'obsoleteUpgradeSchemaScript',
'UpgradeData' => 'obsoleteUpgradeDataScript',
'data-upgrade-' => 'obsoleteDataUpgradeScript',
'recurring' => 'obsoleteRecurringScript',
];

/**
* @inheritdoc
*/
Expand All @@ -60,7 +75,7 @@ public function process(File $phpcsFile, $stackPtr)

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

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
Loading