diff --git a/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php index b23b2858..3c83a81e 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; @@ -67,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) { @@ -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) @@ -120,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 e410c4d9..2473d78f 100644 --- a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php +++ b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php @@ -1,8 +1,10 @@ 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 5ce7c20d..f4ccd586 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)) { @@ -85,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 f2b5b400..9786f25a 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -1,9 +1,11 @@ getFormattedXML($phpcsFile)); if ($xml === false) { - $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), - $stackPtr, - self::ERROR_CODE_CONFIG - ); + return; } $classes = $this->collectClassesInConfig($xml); @@ -72,6 +69,7 @@ private function assertNonFactoryName(File $phpcsFile, array $elements) if (stripos($element['value'], 'Magento') === false) { continue; } + if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element['value']) !== 1) { $phpcsFile->addError( self::ERROR_MESSAGE_CONFIG, @@ -101,24 +99,11 @@ 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) - { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } - /** * 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 +151,7 @@ function (array $extendedNode) { * * @param SimpleXMLElement $xml * @param string $xPath + * * @return array */ private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath): array @@ -174,7 +160,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 +170,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 +179,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 +190,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 +201,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..da71706c 100644 --- a/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php +++ b/Magento2/Sniffs/Legacy/InstallUpgradeSniff.php @@ -1,14 +1,17 @@ 0) { return; } - + $fileInfo = new SplFileInfo($phpcsFile->getFilename()); foreach (self::WRONG_PREFIXES as $code => $data) { @@ -99,11 +102,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(), + Common::stripBasepath($fileInfo->getPath(), $phpcsFile->config->basepath), + ] ); } } diff --git a/Magento2/Sniffs/Legacy/LayoutSniff.php b/Magento2/Sniffs/Legacy/LayoutSniff.php index 4b2fe240..e57d023e 100644 --- a/Magento2/Sniffs/Legacy/LayoutSniff.php +++ b/Magento2/Sniffs/Legacy/LayoutSniff.php @@ -1,9 +1,11 @@ getFormattedXML($phpcsFile)); if ($layout === false) { - $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), - $stackPtr, - self::ERROR_CODE_XML - ); return; } @@ -248,11 +244,12 @@ private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFil if (!isset($this->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 ); } @@ -260,20 +257,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) - { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); - return $doc->saveXML(); - } - /** * Check that the output attribute has the right value * @@ -286,7 +269,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 +280,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 +301,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 +329,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 ); } @@ -362,12 +347,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'] - ), - dom_import_simplexml($node)->getLineNo()-1, - self::ERROR_CODE_METHOD_NOT_ALLOWED + 'Call of method "%s" via layout instruction is not allowed.', + dom_import_simplexml($node)->getLineNo() - 1, + self::ERROR_CODE_METHOD_NOT_ALLOWED, + [ + $attributes['method'], + ] ); } } diff --git a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php index 338834a8..2b051f61 100644 --- a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php +++ b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php @@ -1,4 +1,5 @@ getFormattedXML($phpcsFile)); if ($xml === false) { - $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), - $stackPtr, - self::ERROR_CODE - ); return; } @@ -59,7 +54,7 @@ public function process(File $phpcsFile, $stackPtr) foreach ($foundElements as $element) { $phpcsFile->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,26 +64,12 @@ 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 ); } } } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); - } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php index 58f54910..25e84df3 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteAclSniff.php @@ -1,8 +1,10 @@ formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); - } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php index efc5fae7..6a82442d 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php @@ -8,11 +8,13 @@ namespace Magento2\Sniffs\Legacy; use DOMDocument; -use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; 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'; @@ -40,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( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), - $stackPtr, - self::ERROR_CODE_CONFIG - ); + return; } foreach ($this->getObsoleteNodes() as $xpath => $suggestion) { @@ -55,34 +50,21 @@ public function process(File $phpcsFile, $stackPtr) if (empty($matches)) { continue; } + foreach ($matches as $match) { $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE_CONFIG, + self::ERROR_MESSAGE_CONFIG, + dom_import_simplexml($match)->getLineNo() - 1, + self::ERROR_CODE_CONFIG, + [ $xpath, - $suggestion - ), - dom_import_simplexml($match)->getLineNo()-1, - self::ERROR_CODE_CONFIG + $suggestion, + ] ); } } } - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); - } - /** * Get a list of obsolete nodes in the format => * @@ -147,7 +129,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 +138,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..66682d84 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( - 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/ObsoleteMenuSniff.php b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php index 1955e2e7..74698848 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php @@ -1,8 +1,10 @@ formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); - } } diff --git a/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php b/Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php index e9de6db8..469e7e41 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'; /** @@ -38,16 +39,15 @@ public function process(File $phpcsFile, $stackPtr) $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); if ($xml === false) { - $this->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.", @@ -56,36 +56,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( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format.", - $phpcsFile->getFilename(), - ), - $stackPtr, - self::ERROR_CODE_XML - ); - } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); - } } diff --git a/Magento2/Sniffs/Legacy/ParseXMLTrait.php b/Magento2/Sniffs/Legacy/ParseXMLTrait.php new file mode 100644 index 00000000..a81cb06f --- /dev/null +++ b/Magento2/Sniffs/Legacy/ParseXMLTrait.php @@ -0,0 +1,42 @@ +formatOutput = true; + $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', + [ + Common::stripBasepath($phpcsFile->getFilename(), $phpcsFile->config->basepath), + ] + ); + return false; + } + } +} diff --git a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php index 5b6c860c..c6207ec9 100644 --- a/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php +++ b/Magento2/Sniffs/Legacy/RestrictedCodeSniff.php @@ -1,14 +1,17 @@ isExcluded($token, $phpcsFile)) { return; } + $phpcsFile->addError( - sprintf( - self::ERROR_MESSAGE, - $token, - $phpcsFile->getFilename(), - $this->classes[$token]['replacement'] - ), + self::ERROR_MESSAGE, $stackPtr, $this->classes[$token]['warning_code'], + [ + $token, + Common::stripBasepath($phpcsFile->getFilename(), $phpcsFile->config->basepath), + $this->classes[$token]['replacement'], + ] ); } } @@ -85,6 +89,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 +97,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..f2627456 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,14 +121,15 @@ private function checkOccurrencesInMethods(File $phpcsFile, int $stackPtr, array $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $paramPos + 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'], + ] ); } } @@ -137,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'], + ] ); } } @@ -169,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'], + ] ); } } @@ -208,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'], + ] ); } } @@ -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..f36b1d7c 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -1,4 +1,5 @@ getFormattedXML($phpcsFile)); if ($xml === false) { - $this->invalidXML($phpcsFile, $stackPtr); return; } @@ -52,6 +53,7 @@ public function process(File $phpcsFile, $stackPtr) if (!property_exists($element->attributes(), 'type')) { continue; } + $type = $element['type']; if (preg_match('/\//', $type)) { $phpcsFile->addError( @@ -80,36 +82,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( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), - $stackPtr, - self::ERROR_CODE_XML - ); - } - - /** - * Format the incoming XML to avoid tags split into several lines. - * - * @param File $phpcsFile - * @return false|string - */ - private function getFormattedXML(File $phpcsFile) - { - $doc = new DomDocument('1.0'); - $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); - return $doc->saveXML(); - } } diff --git a/Magento2/Sniffs/Less/ColonSpacingSniff.php b/Magento2/Sniffs/Less/ColonSpacingSniff.php index 6b68ea65..ddbbaf35 100644 --- a/Magento2/Sniffs/Less/ColonSpacingSniff.php +++ b/Magento2/Sniffs/Less/ColonSpacingSniff.php @@ -1,8 +1,10 @@ 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 41a9f97b..bd56fe20 100644 --- a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php +++ b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php @@ -1,8 +1,10 @@ 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 6b074a2e..7b042f77 100644 --- a/Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php +++ b/Magento2/Sniffs/PHP/AutogeneratedClassNotInConstructorSniff.php @@ -1,9 +1,11 @@ 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, + ] ); } @@ -101,6 +104,7 @@ private function validateRequestedClass(File $phpcsFile, int $arrowPosition): vo * Does the class have the suffix common for autogenerated classes e.g. Factory * * @param string $className + * * @return bool */ private function isClassAutogenerated(string $className): bool @@ -110,6 +114,7 @@ private function isClassAutogenerated(string $className): bool return true; } } + return false; } @@ -118,6 +123,7 @@ private function isClassAutogenerated(string $className): bool * * @param File $phpcsFile * @param int $stackPtr + * * @return string|null */ private function getObjectManagerVariableName(File $phpcsFile, int $stackPtr): ?string @@ -141,6 +147,7 @@ private function getObjectManagerVariableName(File $phpcsFile, int $stackPtr): ? * * @param File $phpcsFile * @param int $callerPosition + * * @return string|null */ private function getRequestedClass(File $phpcsFile, int $callerPosition): ?string @@ -163,6 +170,7 @@ private function getRequestedClass(File $phpcsFile, int $callerPosition): ?strin * Does the file contain class declaration * * @param File $phpcsFile + * * @return bool */ private function isClass(File $phpcsFile): bool @@ -172,6 +180,7 @@ private function isClass(File $phpcsFile): bool return true; } } + return false; } @@ -179,6 +188,7 @@ private function isClass(File $phpcsFile): bool * Get an array of constructor parameters * * @param File $phpcsFile + * * @return array */ private function getConstructorParameters(File $phpcsFile): array @@ -188,6 +198,7 @@ private function getConstructorParameters(File $phpcsFile): array return $phpcsFile->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..d2114ac4 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,12 +70,16 @@ 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( - sprintf("The copyright license contains legacy text: %s.", $legacyText), + "The copyright license contains legacy text: %s.", $stackPtr, - self::WARNING_CODE + self::WARNING_CODE, + [ + $legacyText, + ] ); } }