From bedbf6e705d3358005b37d808743d43ef0ee07b9 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 3 Jul 2023 11:45:41 +0100 Subject: [PATCH 1/6] Adhere to coding standard --- ...ClassAndInterfacePHPDocFormattingSniff.php | 2 ++ ...unctionsDeprecatedWithoutArgumentSniff.php | 4 +++- .../Sniffs/Html/HtmlClosingVoidTagsSniff.php | 3 +++ ...assReferencesInConfigurationFilesSniff.php | 16 +++++++++++---- .../Sniffs/Legacy/InstallUpgradeSniff.php | 6 ++++-- Magento2/Sniffs/Legacy/LayoutSniff.php | 20 ++++++++++++------- Magento2/Sniffs/Legacy/ModuleXMLSniff.php | 8 +++++--- Magento2/Sniffs/Legacy/ObsoleteAclSniff.php | 3 +++ .../Legacy/ObsoleteConfigNodesSniff.php | 12 ++++++----- .../Sniffs/Legacy/ObsoleteConnectionSniff.php | 6 ++++-- Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php | 5 ++++- .../ObsoleteSystemConfigurationSniff.php | 7 ++++--- .../Sniffs/Legacy/RestrictedCodeSniff.php | 10 ++++++++-- Magento2/Sniffs/Legacy/TableNameSniff.php | 9 ++++++++- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 3 +++ Magento2/Sniffs/Less/ColonSpacingSniff.php | 2 ++ .../Methods/DeprecatedModelMethodSniff.php | 3 +++ ...utogeneratedClassNotInConstructorSniff.php | 15 +++++++++++++- .../Sniffs/Header/LicenseSniff.php | 7 ++++++- 19 files changed, 108 insertions(+), 33 deletions(-) diff --git a/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php index b23b2858..e4415a7a 100644 --- a/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php +++ b/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php @@ -4,6 +4,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento2\Sniffs\Commenting; use Magento2\Helpers\Commenting\PHPDocFormattingValidator; @@ -107,6 +108,7 @@ public function process(File $phpcsFile, $stackPtr) * @param File $phpcsFile * @param int $commentStartPtr * @param array $tokens + * * @return bool */ private function validateTags(File $phpcsFile, $commentStartPtr, $tokens) diff --git a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php index e410c4d9..2ee408ff 100644 --- a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php +++ b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php @@ -1,8 +1,10 @@ addFixableWarning( sprintf(self::WARNING_MESSAGE, $functionName), $stackPtr, diff --git a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php index 5ce7c20d..e95ae75a 100644 --- a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php @@ -1,8 +1,10 @@ getTokensAsString($stackPtr, count($phpcsFile->getTokens())); if (empty($html)) { diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index f2b5b400..2926ead9 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -1,9 +1,11 @@ addError( self::ERROR_MESSAGE_CONFIG, @@ -105,6 +108,7 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes) * Format the incoming XML to avoid tags split into several lines. * * @param File $phpcsFile + * * @return false|string */ private function getFormattedXML(File $phpcsFile) @@ -119,6 +123,7 @@ private function getFormattedXML(File $phpcsFile) * Parse an XML for references to PHP class names in selected tags or attributes * * @param SimpleXMLElement $xml + * * @return array */ private function collectClassesInConfig(SimpleXMLElement $xml): array @@ -166,6 +171,7 @@ function (array $extendedNode) { * * @param SimpleXMLElement $xml * @param string $xPath + * * @return array */ private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath): array @@ -174,7 +180,7 @@ private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath return array_map(function ($item) { return [ 'value' => (string)$item, - 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, + 'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1, ]; }, $nodes); } @@ -184,6 +190,7 @@ private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath * * @param SimpleXMLElement $xml * @param string $xPath + * * @return array */ private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): array @@ -192,7 +199,7 @@ private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): return array_map(function ($item) { return [ 'value' => $item->getName(), - 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, + 'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1, ]; }, $nodes); } @@ -203,6 +210,7 @@ private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): * @param SimpleXMLElement $xml * @param string $xPath * @param string $attr + * * @return array */ private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPath, string $attr): array @@ -213,7 +221,7 @@ private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPa if (isset($nodeArray['@attributes'][$attr])) { return [ 'value' => $nodeArray['@attributes'][$attr], - 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, + 'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1, ]; } }, $nodes); diff --git a/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php b/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php index 09dbedfb..f45c056e 100644 --- a/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php +++ b/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php @@ -1,9 +1,11 @@ 0) { return; } - + $fileInfo = new SplFileInfo($phpcsFile->getFilename()); foreach (self::WRONG_PREFIXES as $code => $data) { diff --git a/Magento2/Sniffs/Legacy/LayoutSniff.php b/Magento2/Sniffs/Legacy/LayoutSniff.php index 4b2fe240..5a3968c4 100644 --- a/Magento2/Sniffs/Legacy/LayoutSniff.php +++ b/Magento2/Sniffs/Legacy/LayoutSniff.php @@ -1,9 +1,11 @@ obsoleteReferences[$handleAttribute])) { continue; } + foreach ($handle->xpath('//reference | //referenceContainer | //referenceBlock') as $reference) { if (in_array((string)$reference['name'], $this->obsoleteReferences[$handleAttribute]) !== false) { $phpcsFile->addError( 'The block being referenced is removed.', - dom_import_simplexml($reference)->getLineNo()-1, + dom_import_simplexml($reference)->getLineNo() - 1, self::ERROR_CODE_OBSOLETE_BLOCK ); } @@ -264,6 +267,7 @@ private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFil * Format the incoming XML to avoid tags split into several lines. * * @param File $phpcsFile + * * @return false|string */ private function getFormattedXML(File $phpcsFile) @@ -286,7 +290,7 @@ private function testOutputAttribute(SimpleXMLElement $layout, File $phpcsFile): if (!empty($elements)) { $phpcsFile->addError( 'output="toHtml" is obsolete. Use output="1"', - dom_import_simplexml($elements[0])->getLineNo()-1, + dom_import_simplexml($elements[0])->getLineNo() - 1, self::ERROR_CODE_OBSOLETE_TOHTML_ATTRIBUTE ); }; @@ -297,6 +301,7 @@ private function testOutputAttribute(SimpleXMLElement $layout, File $phpcsFile): * * @param SimpleXMLElement $element * @param string $name + * * @return string|null */ private function getAttribute(SimpleXMLElement $element, string $name): string @@ -317,14 +322,15 @@ private function testHelperAttribute(SimpleXMLElement $layout, File $phpcsFile): if (strpos($this->getAttribute($action, 'helper'), '/') !== false) { $phpcsFile->addError( "'helper' attribute contains '/'", - dom_import_simplexml($action)->getLineNo()-1, + dom_import_simplexml($action)->getLineNo() - 1, self::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_NOT_ALLOWED ); } + if (strpos($this->getAttribute($action, 'helper'), '::') === false) { $phpcsFile->addError( "'helper' attribute does not contain '::'", - dom_import_simplexml($action)->getLineNo()-1, + dom_import_simplexml($action)->getLineNo() - 1, self::ERROR_CODE_HELPER_ATTRIBUTE_CHARACTER_EXPECTED ); } @@ -344,7 +350,7 @@ private function testListText(SimpleXMLElement $layout, File $phpcsFile): void $phpcsFile->addError( 'The class \Magento\Framework\View\Element\Text\ListText' . ' is not supposed to be used in layout anymore.', - dom_import_simplexml($elements[0])->getLineNo()-1, + dom_import_simplexml($elements[0])->getLineNo() - 1, self::ERROR_CODE_OBSOLETE_CLASS ); } @@ -366,7 +372,7 @@ private function testActionNodeMethods(SimpleXMLElement $layout, File $phpcsFile 'Call of method "%s" via layout instruction is not allowed.', $attributes['method'] ), - dom_import_simplexml($node)->getLineNo()-1, + dom_import_simplexml($node)->getLineNo() - 1, self::ERROR_CODE_METHOD_NOT_ALLOWED ); } diff --git a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php index 338834a8..5a0e44c0 100644 --- a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php +++ b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php @@ -1,4 +1,5 @@ addWarning( 'The "version" attribute is obsolete. Use "setup_version" instead.', - dom_import_simplexml($element)->getLineNo()-1, + dom_import_simplexml($element)->getLineNo() - 1, self::WARNING_CODE ); } @@ -69,9 +70,9 @@ public function process(File $phpcsFile, $stackPtr) if ($foundElements !== false) { foreach ($foundElements as $element) { $phpcsFile->addWarning( - 'The "active" attribute is obsolete. The list of active modules '. + 'The "active" attribute is obsolete. The list of active modules ' . 'is defined in deployment configuration.', - dom_import_simplexml($element)->getLineNo()-1, + dom_import_simplexml($element)->getLineNo() - 1, self::WARNING_CODE ); } @@ -82,6 +83,7 @@ public function process(File $phpcsFile, $stackPtr) * Format the incoming XML to avoid tags split into several lines. * * @param File $phpcsFile + * * @return false|string */ private function getFormattedXML(File $phpcsFile) diff --git a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php index 58f54910..537e11ce 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php @@ -1,8 +1,10 @@ addError( sprintf( @@ -62,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr) $xpath, $suggestion ), - dom_import_simplexml($match)->getLineNo()-1, + dom_import_simplexml($match)->getLineNo() - 1, self::ERROR_CODE_CONFIG ); } @@ -73,6 +74,7 @@ public function process(File $phpcsFile, $stackPtr) * Format the incoming XML to avoid tags split into several lines. * * @param File $phpcsFile + * * @return false|string */ private function getFormattedXML(File $phpcsFile) @@ -147,7 +149,7 @@ private function getObsoleteNodes(): array '/config/global/dev' => 'This configuration moved to Di configuration of \Magento\Framework\App\Action\Context', '/config/global/webapi' => - 'This configuration moved to Di configuration of '. + 'This configuration moved to Di configuration of ' . ' \Magento\Webapi\Controller\Request\Rest\Interpreter\Factory' . ' and \Magento\Webapi\Controller\Response\Rest\Renderer\Factory', '/config/global/cms' => @@ -156,10 +158,10 @@ private function getObsoleteNodes(): array '/config/global/widget' => 'This configuration moved to Di configuration of \Magento\Cms\Model\Template\FilterProvider', '/config/global/catalog/product/flat/max_index_count' => - 'This configuration moved to Di configuration of '. + 'This configuration moved to Di configuration of ' . '\Magento\Catalog\Model\ResourceModel\Product\Flat\Indexer', '/config/global/catalog/product/flat/attribute_groups' => - 'This configuration moved to Di configuration of '. + 'This configuration moved to Di configuration of ' . '\Magento\Catalog\Model\ResourceModel\Product\Flat\Indexer', '/config/global/catalog/product/flat/add_filterable_attributes' => 'This configuration moved to Di configuration of \Magento\Catalog\Helper\Product\Flat\Indexer', diff --git a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php index 74a74394..feb95fe4 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php @@ -1,9 +1,11 @@ getTokens(); $stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); - + foreach ($this->obsoleteMethods as $method) { if ($tokens[$stringPos]['content'] === $method) { $phpcsFile->addWarning( diff --git a/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php index 1955e2e7..35ec8d14 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php @@ -1,8 +1,10 @@ invalidXML($phpcsFile, $stackPtr); return; } - + $foundElements = $xml->xpath('/config/tabs|/config/sections'); - + if ($foundElements === false) { return; } - + foreach ($foundElements as $element) { $phpcsFile->addWarning( "Obsolete system configuration structure detected in file.", @@ -79,6 +79,7 @@ private function invalidXML(File $phpcsFile, int $stackPtr): void * Format the incoming XML to avoid tags split into several lines. * * @param File $phpcsFile + * * @return false|string */ private function getFormattedXML(File $phpcsFile) diff --git a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php index 5b6c860c..726856b2 100644 --- a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php +++ b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php @@ -1,14 +1,16 @@ isExcluded($token, $phpcsFile)) { return; } + $phpcsFile->addError( sprintf( self::ERROR_MESSAGE, @@ -85,6 +88,7 @@ public function process(File $phpcsFile, $stackPtr) * * @param string $token * @param File $phpcsFile + * * @return bool */ private function isExcluded(string $token, File $phpcsFile): bool @@ -92,11 +96,13 @@ private function isExcluded(string $token, File $phpcsFile): bool if (in_array($phpcsFile->getFilename(), $this->fixtureFiles)) { return true; } + foreach ($this->classes[$token]['exclude'] as $exclude) { if (strpos($phpcsFile->getFilename(), $exclude) !== false) { return true; } } + return false; } } diff --git a/Magento2/Sniffs/Legacy/TableNameSniff.php b/Magento2/Sniffs/Legacy/TableNameSniff.php index 4569f7fc..b25d96fd 100644 --- a/Magento2/Sniffs/Legacy/TableNameSniff.php +++ b/Magento2/Sniffs/Legacy/TableNameSniff.php @@ -1,9 +1,11 @@ argPositionInMethods) === false) { return; } + $firstArgumentPos = $phpcsFile->findNext([T_CONSTANT_ENCAPSED_STRING, T_VARIABLE], $methodNamePos + 1); foreach ($this->argPositionInMethods[$methodName] as $argPosition) { @@ -117,6 +121,7 @@ private function checkOccurrencesInMethods(File $phpcsFile, int $stackPtr, array $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $paramPos + 1) ); } + if (strpos($tokens[$paramPos]['content'], '/') !== false) { $phpcsFile->addError( sprintf( @@ -223,6 +228,7 @@ private function checkOccurrencesInArray(File $phpcsFile, int $stackPtr, array $ * * @param string $haystack * @param string $needle + * * @return bool */ private function endsWith(string $haystack, string $needle): bool @@ -231,6 +237,7 @@ private function endsWith(string $haystack, string $needle): bool if ($length === 0) { return true; } + return substr($haystack, -$length) === $needle; } } diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index aee84dc8..dec42fdf 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -1,4 +1,5 @@ attributes(), 'type')) { continue; } + $type = $element['type']; if (preg_match('/\//', $type)) { $phpcsFile->addError( @@ -103,6 +105,7 @@ protected function invalidXML(File $phpcsFile, int $stackPtr): void * Format the incoming XML to avoid tags split into several lines. * * @param File $phpcsFile + * * @return false|string */ private function getFormattedXML(File $phpcsFile) diff --git a/Magento2/Sniffs/Less/ColonSpacingSniff.php b/Magento2/Sniffs/Less/ColonSpacingSniff.php index 6b68ea65..cf986f04 100644 --- a/Magento2/Sniffs/Less/ColonSpacingSniff.php +++ b/Magento2/Sniffs/Less/ColonSpacingSniff.php @@ -1,8 +1,10 @@ getMethodParameters($stackPtr); } } + return []; } @@ -196,6 +207,7 @@ private function getConstructorParameters(File $phpcsFile): array * * @param File $phpcsFile * @param string $className + * * @return bool */ private function isConstructorParameter(File $phpcsFile, string $className): bool @@ -205,6 +217,7 @@ private function isConstructorParameter(File $phpcsFile, string $className): boo return true; } } + return false; } } diff --git a/Magento2Framework/Sniffs/Header/LicenseSniff.php b/Magento2Framework/Sniffs/Header/LicenseSniff.php index cf7a5479..a36359fb 100644 --- a/Magento2Framework/Sniffs/Header/LicenseSniff.php +++ b/Magento2Framework/Sniffs/Header/LicenseSniff.php @@ -1,9 +1,11 @@ getTokensAsString($stackPtr, 1); } + if ($content !== null) { $this->checkLicense($content, $stackPtr, $phpcsFile); } @@ -66,6 +70,7 @@ private function checkLicense(string $content, int $stackPtr, File $phpcsFile): if (stripos($commentContent, 'copyright') === false) { return; } + foreach (self::LEGACY_TEXTS as $legacyText) { if (stripos($commentContent, $legacyText) !== false) { $phpcsFile->addWarning( From c2efacd919e319dbcb76494bcd4ddbe87d9cf328 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 3 Jul 2023 11:56:33 +0100 Subject: [PATCH 2/6] Let PHP_CodeSniffer run sprintf as needed --- ...ClassAndInterfacePHPDocFormattingSniff.php | 17 ++++---- ...unctionsDeprecatedWithoutArgumentSniff.php | 14 +++++-- .../Sniffs/Html/HtmlClosingVoidTagsSniff.php | 7 +++- ...assReferencesInConfigurationFilesSniff.php | 10 ++--- .../Sniffs/Legacy/InstallUpgradeSniff.php | 8 +++- Magento2/Sniffs/Legacy/LayoutSniff.php | 20 +++++----- Magento2/Sniffs/Legacy/ModuleXMLSniff.php | 10 ++--- .../Legacy/ObsoleteConfigNodesSniff.php | 22 +++++----- .../Sniffs/Legacy/ObsoleteConnectionSniff.php | 7 +++- .../ObsoleteSystemConfigurationSniff.php | 10 ++--- .../Sniffs/Legacy/RestrictedCodeSniff.php | 12 +++--- Magento2/Sniffs/Legacy/TableNameSniff.php | 40 +++++++++---------- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 10 ++--- Magento2/Sniffs/Less/ColonSpacingSniff.php | 10 ++++- .../Methods/DeprecatedModelMethodSniff.php | 7 +++- ...utogeneratedClassNotInConstructorSniff.php | 10 ++--- .../Sniffs/Header/LicenseSniff.php | 7 +++- 17 files changed, 126 insertions(+), 95 deletions(-) diff --git a/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php index e4415a7a..3c83a81e 100644 --- a/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php +++ b/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php @@ -68,12 +68,12 @@ public function process(File $phpcsFile, $stackPtr) if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) { $fix = $phpcsFile->addFixableWarning( - sprintf( - '%s description must contain meaningful information beyond what its name provides or be removed.', - ucfirst($tokens[$stackPtr]['content']) - ), + '%s description must contain meaningful information beyond what its name provides or be removed.', $stackPtr, - 'InvalidDescription' + 'InvalidDescription', + [ + ucfirst($tokens[$stackPtr]['content']), + ] ); if ($fix) { @@ -122,9 +122,12 @@ private function validateTags(File $phpcsFile, $commentStartPtr, $tokens) if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) { $fix = $phpcsFile->addFixableWarning( - sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']), + 'Tag %s MUST NOT be used.', $i, - 'ForbiddenTags' + 'ForbiddenTags', + [ + $tokens[$i]['content'], + ] ); if ($fix) { diff --git a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php index 2ee408ff..2473d78f 100644 --- a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php +++ b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php @@ -73,17 +73,23 @@ public function process(File $phpcsFile, $stackPtr): void if (self::DEPRECATED_FUNCTIONS_AND_FIXES[$functionName] === false) { $phpcsFile->addWarning( - sprintf(self::WARNING_MESSAGE, $functionName), + self::WARNING_MESSAGE, $stackPtr, - self::WARNING_CODE + self::WARNING_CODE, + [ + $functionName, + ] ); return; } $fix = $phpcsFile->addFixableWarning( - sprintf(self::WARNING_MESSAGE, $functionName), + self::WARNING_MESSAGE, $stackPtr, - self::WARNING_CODE + self::WARNING_CODE, + [ + $functionName, + ] ); if ($fix === true) { diff --git a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php index e95ae75a..f4ccd586 100644 --- a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php @@ -88,9 +88,12 @@ public function process(File $phpcsFile, $stackPtr): void foreach ($matches as $match) { if (in_array($match[1], self::HTML_VOID_ELEMENTS)) { $phpcsFile->addWarning( - sprintf(self::WARNING_MESSAGE, $match[0]), + self::WARNING_MESSAGE, null, - self::WARNING_CODE + self::WARNING_CODE, + [ + $match[0], + ] ); } } diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 2926ead9..e6f529a9 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -46,12 +46,12 @@ public function process(File $phpcsFile, $stackPtr) $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), + "Couldn't parse contents of '%s', check that they are in valid XML format", $stackPtr, - self::ERROR_CODE_CONFIG + self::ERROR_CODE_CONFIG, + [ + $phpcsFile->getFilename(), + ] ); } diff --git a/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php b/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php index f45c056e..c24c31b9 100644 --- a/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php +++ b/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php @@ -101,11 +101,15 @@ public function process(File $phpcsFile, $stackPtr) if ($folderName === 'data' || $folderName === 'sql') { $phpcsFile->addError( - $fileInfo->getFilename()." is in an invalid directory ".$fileInfo->getPath().":\n" + "%s is in an invalid directory %s:\n" . "- 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::INVALID_DIRECTORIES_ERROR_CODES[$folderName] + self::INVALID_DIRECTORIES_ERROR_CODES[$folderName], + [ + $fileInfo->getFilename(), + $fileInfo->getPath(), + ] ); } } diff --git a/Magento2/Sniffs/Legacy/LayoutSniff.php b/Magento2/Sniffs/Legacy/LayoutSniff.php index 5a3968c4..c2cc61f8 100644 --- a/Magento2/Sniffs/Legacy/LayoutSniff.php +++ b/Magento2/Sniffs/Legacy/LayoutSniff.php @@ -220,12 +220,12 @@ public function process(File $phpcsFile, $stackPtr) if ($layout === false) { $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), + "Couldn't parse contents of '%s', check that they are in valid XML format", $stackPtr, - self::ERROR_CODE_XML + self::ERROR_CODE_XML, + [ + $phpcsFile->getFilename(), + ] ); return; } @@ -368,12 +368,12 @@ private function testActionNodeMethods(SimpleXMLElement $layout, File $phpcsFile foreach ($layout->xpath('//action[' . $methodFilter . ']') as $node) { $attributes = $node->attributes(); $phpcsFile->addError( - sprintf( - 'Call of method "%s" via layout instruction is not allowed.', - $attributes['method'] - ), + 'Call of method "%s" via layout instruction is not allowed.', dom_import_simplexml($node)->getLineNo() - 1, - self::ERROR_CODE_METHOD_NOT_ALLOWED + self::ERROR_CODE_METHOD_NOT_ALLOWED, + [ + $attributes['method'], + ] ); } } diff --git a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php index 5a0e44c0..191c3cd4 100644 --- a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php +++ b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php @@ -45,12 +45,12 @@ public function process(File $phpcsFile, $stackPtr) $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), + "Couldn't parse contents of '%s', check that they are in valid XML format", $stackPtr, - self::ERROR_CODE + self::ERROR_CODE, + [ + $phpcsFile->getFilename(), + ] ); return; } diff --git a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php index 0316f6ae..ff443f69 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php @@ -41,12 +41,12 @@ public function process(File $phpcsFile, $stackPtr) $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), + "Couldn't parse contents of '%s', check that they are in valid XML format", $stackPtr, - self::ERROR_CODE_CONFIG + self::ERROR_CODE_CONFIG, + [ + $phpcsFile->getFilename(), + ] ); } @@ -58,13 +58,13 @@ public function process(File $phpcsFile, $stackPtr) foreach ($matches as $match) { $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE_CONFIG, - $xpath, - $suggestion - ), + self::ERROR_MESSAGE_CONFIG, dom_import_simplexml($match)->getLineNo() - 1, - self::ERROR_CODE_CONFIG + self::ERROR_CODE_CONFIG, + [ + $xpath, + $suggestion, + ] ); } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php index feb95fe4..66682d84 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php @@ -63,9 +63,12 @@ private function validateObsoleteMethod(File $phpcsFile, int $stackPtr) foreach ($this->obsoleteMethods as $method) { if ($tokens[$stringPos]['content'] === $method) { $phpcsFile->addWarning( - sprintf("Contains obsolete method: %s. Please use getConnection method instead.", $method), + "Contains obsolete method: %s. Please use getConnection method instead.", $stackPtr, - self::OBSOLETE_METHOD_ERROR_CODE + self::OBSOLETE_METHOD_ERROR_CODE, + [ + $method, + ] ); } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php index 367b4133..2851c48f 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php @@ -66,12 +66,12 @@ public function process(File $phpcsFile, $stackPtr) private function invalidXML(File $phpcsFile, int $stackPtr): void { $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format.", - $phpcsFile->getFilename(), - ), + "Couldn't parse contents of '%s', check that they are in valid XML format.", $stackPtr, - self::ERROR_CODE_XML + self::ERROR_CODE_XML, + [ + $phpcsFile->getFilename(), + ] ); } diff --git a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php index 726856b2..adc28fc6 100644 --- a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php +++ b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php @@ -71,14 +71,14 @@ public function process(File $phpcsFile, $stackPtr) } $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE, - $token, - $phpcsFile->getFilename(), - $this->classes[$token]['replacement'] - ), + self::ERROR_MESSAGE, $stackPtr, $this->classes[$token]['warning_code'], + [ + $token, + $phpcsFile->getFilename(), + $this->classes[$token]['replacement'], + ] ); } } diff --git a/Magento2/Sniffs/Legacy/TableNameSniff.php b/Magento2/Sniffs/Legacy/TableNameSniff.php index b25d96fd..f2627456 100644 --- a/Magento2/Sniffs/Legacy/TableNameSniff.php +++ b/Magento2/Sniffs/Legacy/TableNameSniff.php @@ -124,12 +124,12 @@ private function checkOccurrencesInMethods(File $phpcsFile, int $stackPtr, array if (strpos($tokens[$paramPos]['content'], '/') !== false) { $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE, - $tokens[$paramPos]['content'], - ), + self::ERROR_MESSAGE, $paramPos, - self::ERROR_CODE + self::ERROR_CODE, + [ + $tokens[$paramPos]['content'], + ] ); } } @@ -142,12 +142,12 @@ private function checkOccurrencesInMethods(File $phpcsFile, int $stackPtr, array $paramPos = $phpcsFile->findNext(T_PARAM_NAME, $stackPtr + 1); if (strpos($tokens[$paramPos]['content'], '/') !== false) { $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE, - $tokens[$paramPos]['content'], - ), + self::ERROR_MESSAGE, $paramPos, - self::ERROR_CODE + self::ERROR_CODE, + [ + $tokens[$paramPos]['content'], + ] ); } } @@ -174,12 +174,12 @@ private function checkOccurrencesInProperty(File $phpcsFile, int $stackPtr, arra if (strpos($tokens[$tableNamePos]['content'], '/') !== false) { $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE, - $tokens[$tableNamePos]['content'], - ), + self::ERROR_MESSAGE, $tableNamePos, - self::ERROR_CODE + self::ERROR_CODE, + [ + $tokens[$tableNamePos]['content'], + ] ); } } @@ -213,12 +213,12 @@ private function checkOccurrencesInArray(File $phpcsFile, int $stackPtr, array $ if (strpos($tokens[$tableNamePos]['content'], '/') !== false) { $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE, - $tokens[$tableNamePos]['content'], - ), + self::ERROR_MESSAGE, $tableNamePos, - self::ERROR_CODE + self::ERROR_CODE, + [ + $tokens[$tableNamePos]['content'], + ] ); } } diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index dec42fdf..4a418c77 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -92,12 +92,12 @@ public function process(File $phpcsFile, $stackPtr) protected function invalidXML(File $phpcsFile, int $stackPtr): void { $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), + "Couldn't parse contents of '%s', check that they are in valid XML format", $stackPtr, - self::ERROR_CODE_XML + self::ERROR_CODE_XML, + [ + $phpcsFile->getFilename(), + ] ); } diff --git a/Magento2/Sniffs/Less/ColonSpacingSniff.php b/Magento2/Sniffs/Less/ColonSpacingSniff.php index cf986f04..ddbbaf35 100644 --- a/Magento2/Sniffs/Less/ColonSpacingSniff.php +++ b/Magento2/Sniffs/Less/ColonSpacingSniff.php @@ -108,8 +108,14 @@ private function validateSpaces(File $phpcsFile, $stackPtr, array $tokens) if (false !== strpos($content, ' ')) { $length = strlen($content); if ($length !== 1) { - $error = sprintf('Expected 1 space after colon in style definition; %s found', $length); - $phpcsFile->addError($error, $stackPtr, 'After'); + $phpcsFile->addError( + 'Expected 1 space after colon in style definition; %s found', + $stackPtr, + 'After', + [ + $length, + ] + ); } } } diff --git a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php index 14ba678a..bd56fe20 100644 --- a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php +++ b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php @@ -70,9 +70,12 @@ public function process(File $phpcsFile, $stackPtr) $methodPosition = $phpcsFile->findNext([T_STRING, T_VARIABLE], $resourcePosition + 1, $endOfStatement); if ($methodPosition !== false && in_array($tokens[$methodPosition]['content'], $this->methods, true)) { $phpcsFile->addWarning( - sprintf($this->warningMessage, $tokens[$methodPosition]['content']), + $this->warningMessage, $stackPtr, - $this->warningCode + $this->warningCode, + [ + $tokens[$methodPosition]['content'], + ] ); } } diff --git a/Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php b/Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php index 29d4884e..7b042f77 100644 --- a/Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php +++ b/Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php @@ -90,13 +90,13 @@ private function validateRequestedClass(File $phpcsFile, int $arrowPosition): vo } $phpcsFile->addError( - sprintf( - 'Class %s needs to be requested in constructor, ' . + 'Class %s needs to be requested in constructor, ' . 'otherwise compiler will not be able to find and generate this class', - $requestedClass - ), $arrowPosition, - self::ERROR_CODE + self::ERROR_CODE, + [ + $requestedClass, + ] ); } diff --git a/Magento2Framework/Sniffs/Header/LicenseSniff.php b/Magento2Framework/Sniffs/Header/LicenseSniff.php index a36359fb..d2114ac4 100644 --- a/Magento2Framework/Sniffs/Header/LicenseSniff.php +++ b/Magento2Framework/Sniffs/Header/LicenseSniff.php @@ -74,9 +74,12 @@ private function checkLicense(string $content, int $stackPtr, File $phpcsFile): foreach (self::LEGACY_TEXTS as $legacyText) { if (stripos($commentContent, $legacyText) !== false) { $phpcsFile->addWarning( - sprintf("The copyright license contains legacy text: %s.", $legacyText), + "The copyright license contains legacy text: %s.", $stackPtr, - self::WARNING_CODE + self::WARNING_CODE, + [ + $legacyText, + ] ); } } From 6c03b6063026b1a85a406519f80be9e08cb4916f Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 3 Jul 2023 13:36:33 +0100 Subject: [PATCH 3/6] Update getFormattedXML() to match its docblock --- .../ClassReferencesInConfigurationFilesSniff.php | 12 ++++++++---- Magento2/Sniffs/Legacy/LayoutSniff.php | 12 ++++++++---- Magento2/Sniffs/Legacy/ModuleXMLSniff.php | 12 ++++++++---- Magento2/Sniffs/Legacy/ObsoleteAclSniff.php | 12 ++++++++---- Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php | 12 ++++++++---- Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php | 12 ++++++++---- .../Legacy/ObsoleteSystemConfigurationSniff.php | 12 ++++++++---- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 12 ++++++++---- 8 files changed, 64 insertions(+), 32 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index e6f529a9..c163347a 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -113,10 +113,14 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes) */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } /** diff --git a/Magento2/Sniffs/Legacy/LayoutSniff.php b/Magento2/Sniffs/Legacy/LayoutSniff.php index c2cc61f8..1b9afedd 100644 --- a/Magento2/Sniffs/Legacy/LayoutSniff.php +++ b/Magento2/Sniffs/Legacy/LayoutSniff.php @@ -272,10 +272,14 @@ private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFil */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } /** diff --git a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php index 191c3cd4..af75ece8 100644 --- a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php +++ b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php @@ -88,9 +88,13 @@ public function process(File $phpcsFile, $stackPtr) */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php index 537e11ce..42f1e901 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php @@ -57,9 +57,13 @@ public function process(File $phpcsFile, $stackPtr) */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php index ff443f69..35835d00 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php @@ -79,10 +79,14 @@ public function process(File $phpcsFile, $stackPtr) */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } /** diff --git a/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php index 35ec8d14..f10ca4da 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php @@ -62,9 +62,13 @@ public function process(File $phpcsFile, $stackPtr) */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php index 2851c48f..4a82ffd2 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php @@ -84,9 +84,13 @@ private function invalidXML(File $phpcsFile, int $stackPtr): void */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } } diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index 4a418c77..e74c15d3 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -110,9 +110,13 @@ protected function invalidXML(File $phpcsFile, int $stackPtr): void */ private function getFormattedXML(File $phpcsFile) { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } } } From 880d4f7dbdf80609cca3f46bcb32f6e9aa290679 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 3 Jul 2023 13:48:50 +0100 Subject: [PATCH 4/6] Extract common code to new trait --- ...assReferencesInConfigurationFilesSniff.php | 21 +------- Magento2/Sniffs/Legacy/LayoutSniff.php | 21 +------- Magento2/Sniffs/Legacy/ModuleXMLSniff.php | 21 +------- Magento2/Sniffs/Legacy/ObsoleteAclSniff.php | 21 +------- .../Legacy/ObsoleteConfigNodesSniff.php | 21 +------- Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php | 21 +------- .../ObsoleteSystemConfigurationSniff.php | 40 +-------------- Magento2/Sniffs/Legacy/ParseXMLTrait.php | 51 +++++++++++++++++++ Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 40 +-------------- 9 files changed, 67 insertions(+), 190 deletions(-) create mode 100644 Magento2/Sniffs/Legacy/ParseXMLTrait.php diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index c163347a..b897e752 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -16,6 +16,8 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff { + use ParseXMLTrait; + private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference'; private const ERROR_CODE_CONFIG = 'IncorrectClassReference'; private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference'; @@ -104,25 +106,6 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes) } } - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } - /** * Parse an XML for references to PHP class names in selected tags or attributes * diff --git a/Magento2/Sniffs/Legacy/LayoutSniff.php b/Magento2/Sniffs/Legacy/LayoutSniff.php index 1b9afedd..28abe2b0 100644 --- a/Magento2/Sniffs/Legacy/LayoutSniff.php +++ b/Magento2/Sniffs/Legacy/LayoutSniff.php @@ -19,6 +19,8 @@ */ class LayoutSniff implements Sniff { + use ParseXMLTrait; + private const ERROR_CODE_XML = 'WrongXML'; private const ERROR_CODE_OBSOLETE_BLOCK = 'ObsoleteBlock'; private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass'; @@ -263,25 +265,6 @@ private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFil } } - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } - /** * Check that the output attribute has the right value * diff --git a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php index af75ece8..28d18ad7 100644 --- a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php +++ b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php @@ -17,6 +17,8 @@ */ class ModuleXMLSniff implements Sniff { + use ParseXMLTrait; + private const WARNING_CODE = 'FoundObsoleteAttribute'; private const ERROR_CODE = 'WrongXML'; @@ -78,23 +80,4 @@ public function process(File $phpcsFile, $stackPtr) } } } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php index 42f1e901..25e84df3 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php @@ -16,6 +16,8 @@ */ class ObsoleteAclSniff implements Sniff { + use ParseXMLTrait; + private const WARNING_OBSOLETE_ACL_STRUCTURE = 'ObsoleteAclStructure'; /** @@ -47,23 +49,4 @@ public function process(File $phpcsFile, $stackPtr) ); } } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php index 35835d00..e1eb86ac 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php @@ -13,6 +13,8 @@ class ObsoleteConfigNodesSniff implements Sniff { + use ParseXMLTrait; + private const ERROR_MESSAGE_CONFIG = "Nodes identified by XPath '%s' are obsolete. %s"; private const ERROR_CODE_CONFIG = 'ObsoleteNodeInConfig'; @@ -70,25 +72,6 @@ public function process(File $phpcsFile, $stackPtr) } } - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } - /** * Get a list of obsolete nodes in the format => * diff --git a/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php index f10ca4da..74698848 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php @@ -16,6 +16,8 @@ */ class ObsoleteMenuSniff implements Sniff { + use ParseXMLTrait; + private const WARNING_OBSOLETE_MENU_STRUCTURE = 'ObsoleteMenuStructure'; /** @@ -52,23 +54,4 @@ public function process(File $phpcsFile, $stackPtr) ); } } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php index 4a82ffd2..501011ab 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php @@ -13,7 +13,8 @@ class ObsoleteSystemConfigurationSniff implements Sniff { - private const ERROR_CODE_XML = 'WrongXML'; + use ParseXMLTrait; + private const WARNING_CODE_OBSOLETE = 'FoundObsoleteSystemConfiguration'; /** @@ -56,41 +57,4 @@ public function process(File $phpcsFile, $stackPtr) ); } } - - /** - * Adds an invalid XML error - * - * @param File $phpcsFile - * @param int $stackPtr - */ - private function invalidXML(File $phpcsFile, int $stackPtr): void - { - $phpcsFile->addError( - "Couldn't parse contents of '%s', check that they are in valid XML format.", - $stackPtr, - self::ERROR_CODE_XML, - [ - $phpcsFile->getFilename(), - ] - ); - } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } } diff --git a/Magento2/Sniffs/Legacy/ParseXMLTrait.php b/Magento2/Sniffs/Legacy/ParseXMLTrait.php new file mode 100644 index 00000000..d946b255 --- /dev/null +++ b/Magento2/Sniffs/Legacy/ParseXMLTrait.php @@ -0,0 +1,51 @@ +addError( + "Couldn't parse contents of '%s', check that they are in valid XML format.", + $stackPtr, + 'WrongXML', + [ + $phpcsFile->getFilename(), + ] + ); + } + + /** + * Format the incoming XML to avoid tags split into several lines. + * + * @param File $phpcsFile + * + * @return false|string + */ + private function getFormattedXML(File $phpcsFile) + { + try { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); + return $doc->saveXML(); + } catch (\Exception $e) { + return false; + } + } +} diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index e74c15d3..07cf68a8 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -16,10 +16,11 @@ */ class WidgetXMLSniff implements Sniff { + use ParseXMLTrait; + private const ERROR_CODE_OBSOLETE_SUPPORTED_BLOCKS = 'FoundObsoleteNodeSupportedBlocks'; private const ERROR_CODE_OBSOLETE_BLOCK_NAME = 'FoundObsoleteNodeBlockName'; private const ERROR_CODE_FACTORY = 'FoundFactory'; - private const ERROR_CODE_XML = 'WrongXML'; /** * @inheritdoc @@ -82,41 +83,4 @@ public function process(File $phpcsFile, $stackPtr) ); } } - - /** - * Adds an invalid XML error - * - * @param File $phpcsFile - * @param int $stackPtr - */ - protected function invalidXML(File $phpcsFile, int $stackPtr): void - { - $phpcsFile->addError( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $stackPtr, - self::ERROR_CODE_XML, - [ - $phpcsFile->getFilename(), - ] - ); - } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - try { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } catch (\Exception $e) { - return false; - } - } } From c688fd35c49226b7a9f6bdecd3e98fc0b2e5558a Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 3 Jul 2023 14:02:34 +0100 Subject: [PATCH 5/6] Remove duplicate code --- ...assReferencesInConfigurationFilesSniff.php | 9 +------ Magento2/Sniffs/Legacy/LayoutSniff.php | 8 ------ Magento2/Sniffs/Legacy/ModuleXMLSniff.php | 8 ------ .../Legacy/ObsoleteConfigNodesSniff.php | 9 +------ .../ObsoleteSystemConfigurationSniff.php | 1 - Magento2/Sniffs/Legacy/ParseXMLTrait.php | 26 ++++++------------- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 1 - 7 files changed, 10 insertions(+), 52 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index b897e752..9786f25a 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -47,14 +47,7 @@ public function process(File $phpcsFile, $stackPtr) // instead, as it is the one we compare with $stackPtr later on. $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { - $phpcsFile->addError( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $stackPtr, - self::ERROR_CODE_CONFIG, - [ - $phpcsFile->getFilename(), - ] - ); + return; } $classes = $this->collectClassesInConfig($xml); diff --git a/Magento2/Sniffs/Legacy/LayoutSniff.php b/Magento2/Sniffs/Legacy/LayoutSniff.php index 28abe2b0..e57d023e 100644 --- a/Magento2/Sniffs/Legacy/LayoutSniff.php +++ b/Magento2/Sniffs/Legacy/LayoutSniff.php @@ -221,14 +221,6 @@ public function process(File $phpcsFile, $stackPtr) $layout = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($layout === false) { - $phpcsFile->addError( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $stackPtr, - self::ERROR_CODE_XML, - [ - $phpcsFile->getFilename(), - ] - ); return; } diff --git a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php index 28d18ad7..2b051f61 100644 --- a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php +++ b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php @@ -46,14 +46,6 @@ public function process(File $phpcsFile, $stackPtr) // instead, as it is the one we compare with $stackPtr later on. $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { - $phpcsFile->addError( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $stackPtr, - self::ERROR_CODE, - [ - $phpcsFile->getFilename(), - ] - ); return; } diff --git a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php index e1eb86ac..6a82442d 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php @@ -42,14 +42,7 @@ public function process(File $phpcsFile, $stackPtr) // instead, as it is the one we compare with $stackPtr later on. $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { - $phpcsFile->addError( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $stackPtr, - self::ERROR_CODE_CONFIG, - [ - $phpcsFile->getFilename(), - ] - ); + return; } foreach ($this->getObsoleteNodes() as $xpath => $suggestion) { diff --git a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php index 501011ab..469e7e41 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php @@ -39,7 +39,6 @@ public function process(File $phpcsFile, $stackPtr) $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { - $this->invalidXML($phpcsFile, $stackPtr); return; } diff --git a/Magento2/Sniffs/Legacy/ParseXMLTrait.php b/Magento2/Sniffs/Legacy/ParseXMLTrait.php index d946b255..13d53f31 100644 --- a/Magento2/Sniffs/Legacy/ParseXMLTrait.php +++ b/Magento2/Sniffs/Legacy/ParseXMLTrait.php @@ -12,24 +12,6 @@ trait ParseXMLTrait { - /** - * Adds an invalid XML error - * - * @param File $phpcsFile - * @param int $stackPtr - */ - private function invalidXML(File $phpcsFile, int $stackPtr): void - { - $phpcsFile->addError( - "Couldn't parse contents of '%s', check that they are in valid XML format.", - $stackPtr, - 'WrongXML', - [ - $phpcsFile->getFilename(), - ] - ); - } - /** * Format the incoming XML to avoid tags split into several lines. * @@ -45,6 +27,14 @@ private function getFormattedXML(File $phpcsFile) $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); return $doc->saveXML(); } catch (\Exception $e) { + $phpcsFile->addError( + "Couldn't parse contents of '%s', check that they are in valid XML format.", + 0, + 'WrongXML', + [ + $phpcsFile->getFilename(), + ] + ); return false; } } diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index 07cf68a8..f36b1d7c 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -44,7 +44,6 @@ public function process(File $phpcsFile, $stackPtr) $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { - $this->invalidXML($phpcsFile, $stackPtr); return; } From a73bafef403a234b14c1e217dccbc54b7db41f76 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 3 Jul 2023 14:10:29 +0100 Subject: [PATCH 6/6] Respect basepath configuration option in output --- Magento2/Sniffs/Legacy/InstallUpgradeSniff.php | 3 ++- Magento2/Sniffs/Legacy/ParseXMLTrait.php | 3 ++- Magento2/Sniffs/Legacy/RestrictedCodeSniff.php | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php b/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php index c24c31b9..da71706c 100644 --- a/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php +++ b/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Common; use SplFileInfo; class InstallUpgradeSniff implements Sniff @@ -108,7 +109,7 @@ public function process(File $phpcsFile, $stackPtr) self::INVALID_DIRECTORIES_ERROR_CODES[$folderName], [ $fileInfo->getFilename(), - $fileInfo->getPath(), + Common::stripBasepath($fileInfo->getPath(), $phpcsFile->config->basepath), ] ); } diff --git a/Magento2/Sniffs/Legacy/ParseXMLTrait.php b/Magento2/Sniffs/Legacy/ParseXMLTrait.php index 13d53f31..a81cb06f 100644 --- a/Magento2/Sniffs/Legacy/ParseXMLTrait.php +++ b/Magento2/Sniffs/Legacy/ParseXMLTrait.php @@ -9,6 +9,7 @@ use DOMDocument; use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Common; trait ParseXMLTrait { @@ -32,7 +33,7 @@ private function getFormattedXML(File $phpcsFile) 0, 'WrongXML', [ - $phpcsFile->getFilename(), + Common::stripBasepath($phpcsFile->getFilename(), $phpcsFile->config->basepath), ] ); return false; diff --git a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php index adc28fc6..c6207ec9 100644 --- a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php +++ b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Common; /** * Tests to find usage of restricted code @@ -76,7 +77,7 @@ public function process(File $phpcsFile, $stackPtr) $this->classes[$token]['warning_code'], [ $token, - $phpcsFile->getFilename(), + Common::stripBasepath($phpcsFile->getFilename(), $phpcsFile->config->basepath), $this->classes[$token]['replacement'], ] );