From 56e2de84fd8b46185fcd32082bbc0dca070e9e94 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 6 Sep 2021 13:40:14 +0200 Subject: [PATCH 01/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/Sniffs/Legacy/ClassesPHPSniff.php | 93 ++++++++++++++++++++ Magento2/Tests/Legacy/ClassesPHPUnitTest.inc | 10 +++ Magento2/Tests/Legacy/ClassesPHPUnitTest.php | 30 +++++++ 3 files changed, 133 insertions(+) create mode 100644 Magento2/Sniffs/Legacy/ClassesPHPSniff.php create mode 100644 Magento2/Tests/Legacy/ClassesPHPUnitTest.inc create mode 100644 Magento2/Tests/Legacy/ClassesPHPUnitTest.php diff --git a/Magento2/Sniffs/Legacy/ClassesPHPSniff.php b/Magento2/Sniffs/Legacy/ClassesPHPSniff.php new file mode 100644 index 00000000..6849b46d --- /dev/null +++ b/Magento2/Sniffs/Legacy/ClassesPHPSniff.php @@ -0,0 +1,93 @@ +getTokens(); + $methodNameStackPtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true); + if ($methodNameStackPtr === false) { + return; + } + $name = $tokens[$methodNameStackPtr]['content']; + if (in_array($name, $this->methodsThatReceiveClassNameAsFirstArgument)) { + $firstArgumentStackPtr = $phpcsFile->findNext( + [T_CONSTANT_ENCAPSED_STRING], + $methodNameStackPtr + 1, + null, + false, + null, + true + ); + if ($firstArgumentStackPtr === false) { + return; + } + $name = $tokens[$firstArgumentStackPtr]['content']; + if (!$this->isAValidNonFactoryName($name)) { + $phpcsFile->addError( + self::errorMessage, + $methodNameStackPtr + 1, + self::errorCode, + ); + } + } + } + + /** + * Check whether specified classes or module names correspond to a file according PSR-1 Standard. + * + * @param string $name + * @return bool + */ + private function isAValidNonFactoryName(string $name): bool + { + if (strpos($name, 'Magento') === false) { + return true; + } + + if (false === strpos($name, '\\')) { + return false; + } + + if (preg_match('/^([A-Z\\\\][A-Za-z\d\\\\]+)+$/', $name) !== 1) { + return false; + } + + return true; + } +} diff --git a/Magento2/Tests/Legacy/ClassesPHPUnitTest.inc b/Magento2/Tests/Legacy/ClassesPHPUnitTest.inc new file mode 100644 index 00000000..5482d3fe --- /dev/null +++ b/Magento2/Tests/Legacy/ClassesPHPUnitTest.inc @@ -0,0 +1,10 @@ + 1, + 9 => 1, + ]; + } + + /** + * @inheritdoc + */ + public function getWarningList() + { + return []; + } +} From c9703a6f40e8596285447000309182179a98a823 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 6 Sep 2021 15:31:47 +0200 Subject: [PATCH 02/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/Sniffs/Legacy/ClassesPHPSniff.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassesPHPSniff.php b/Magento2/Sniffs/Legacy/ClassesPHPSniff.php index 6849b46d..866afefc 100644 --- a/Magento2/Sniffs/Legacy/ClassesPHPSniff.php +++ b/Magento2/Sniffs/Legacy/ClassesPHPSniff.php @@ -11,9 +11,9 @@ class ClassesPHPSniff implements Sniff { - private const errorMessage = 'Obsolete factory name(s) detected'; + private const ERROR_MESSAGE = 'Obsolete factory name(s) detected'; - private const errorCode = 'ObsoleteFactoryName'; + private const ERROR_CODE = 'ObsoleteFactoryName'; private $methodsThatReceiveClassNameAsFirstArgument = [ 'getModel', 'getSingleton', 'getResourceModel', 'getResourceSingleton', @@ -32,7 +32,6 @@ public function register(): array T_DOUBLE_COLON, ]; } - /** * @inheritdoc @@ -60,9 +59,9 @@ public function process(File $phpcsFile, $stackPtr) $name = $tokens[$firstArgumentStackPtr]['content']; if (!$this->isAValidNonFactoryName($name)) { $phpcsFile->addError( - self::errorMessage, + self::ERROR_MESSAGE, $methodNameStackPtr + 1, - self::errorCode, + self::ERROR_CODE, ); } } From 5a83385431d2b0628f341c757b761ab663c9039e Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 9 Sep 2021 11:45:49 +0200 Subject: [PATCH 03/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...niff.php => ObsoleteMethodsUsageSniff.php} | 46 ++++--------------- ...t.inc => ObsoleteMethodsUsageUnitTest.inc} | 6 +-- ...t.php => ObsoleteMethodsUsageUnitTest.php} | 7 +-- 3 files changed, 15 insertions(+), 44 deletions(-) rename Magento2/Sniffs/Legacy/{ClassesPHPSniff.php => ObsoleteMethodsUsageSniff.php} (53%) rename Magento2/Tests/Legacy/{ClassesPHPUnitTest.inc => ObsoleteMethodsUsageUnitTest.inc} (64%) rename Magento2/Tests/Legacy/{ClassesPHPUnitTest.php => ObsoleteMethodsUsageUnitTest.php} (76%) diff --git a/Magento2/Sniffs/Legacy/ClassesPHPSniff.php b/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php similarity index 53% rename from Magento2/Sniffs/Legacy/ClassesPHPSniff.php rename to Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php index 866afefc..44ae964b 100644 --- a/Magento2/Sniffs/Legacy/ClassesPHPSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php @@ -8,14 +8,13 @@ use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File; -class ClassesPHPSniff implements Sniff +class ObsoleteMethodsUsageSniff implements Sniff { - - private const ERROR_MESSAGE = 'Obsolete factory name(s) detected'; + private const ERROR_MESSAGE = 'Obsolete methods usage detected'; - private const ERROR_CODE = 'ObsoleteFactoryName'; + private const ERROR_CODE = 'ObsoleteMethodsUsage'; - private $methodsThatReceiveClassNameAsFirstArgument = [ + private $obsoleteStaticMethods = [ 'getModel', 'getSingleton', 'getResourceModel', 'getResourceSingleton', 'addBlock', 'createBlock', 'getBlockSingleton', 'initReport', 'setEntityModelClass', 'setAttributeModel', 'setBackendModel', 'setFrontendModel', @@ -28,7 +27,6 @@ class ClassesPHPSniff implements Sniff public function register(): array { return [ - T_OBJECT, T_DOUBLE_COLON, ]; } @@ -44,20 +42,19 @@ public function process(File $phpcsFile, $stackPtr) return; } $name = $tokens[$methodNameStackPtr]['content']; - if (in_array($name, $this->methodsThatReceiveClassNameAsFirstArgument)) { - $firstArgumentStackPtr = $phpcsFile->findNext( - [T_CONSTANT_ENCAPSED_STRING], - $methodNameStackPtr + 1, + if (in_array($name, $this->obsoleteStaticMethods)) { + $classNameStackPtr = $phpcsFile->findPrevious( + [T_STRING], + $methodNameStackPtr - 1, null, false, null, true ); - if ($firstArgumentStackPtr === false) { + if ($classNameStackPtr === false) { return; } - $name = $tokens[$firstArgumentStackPtr]['content']; - if (!$this->isAValidNonFactoryName($name)) { + if ($tokens[$classNameStackPtr]['content'] === 'Mage') { $phpcsFile->addError( self::ERROR_MESSAGE, $methodNameStackPtr + 1, @@ -66,27 +63,4 @@ public function process(File $phpcsFile, $stackPtr) } } } - - /** - * Check whether specified classes or module names correspond to a file according PSR-1 Standard. - * - * @param string $name - * @return bool - */ - private function isAValidNonFactoryName(string $name): bool - { - if (strpos($name, 'Magento') === false) { - return true; - } - - if (false === strpos($name, '\\')) { - return false; - } - - if (preg_match('/^([A-Z\\\\][A-Za-z\d\\\\]+)+$/', $name) !== 1) { - return false; - } - - return true; - } } diff --git a/Magento2/Tests/Legacy/ClassesPHPUnitTest.inc b/Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.inc similarity index 64% rename from Magento2/Tests/Legacy/ClassesPHPUnitTest.inc rename to Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.inc index 5482d3fe..91583ae7 100644 --- a/Magento2/Tests/Legacy/ClassesPHPUnitTest.inc +++ b/Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.inc @@ -1,10 +1,6 @@ 1, - 9 => 1, + 3 => 1, + 5 => 1, + 6 => 1, ]; } From 5b407128bb6de41fb2348c0bcfc55dc163f7e7fa Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 9 Sep 2021 11:51:44 +0200 Subject: [PATCH 04/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php b/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php index 44ae964b..d12c9fa6 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php @@ -14,6 +14,9 @@ class ObsoleteMethodsUsageSniff implements Sniff private const ERROR_CODE = 'ObsoleteMethodsUsage'; + /** + * @var string[] + */ private $obsoleteStaticMethods = [ 'getModel', 'getSingleton', 'getResourceModel', 'getResourceSingleton', 'addBlock', 'createBlock', 'getBlockSingleton', From 9d542d9f2c04d74e0e45930316c31d30894257b6 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 11:28:03 +0200 Subject: [PATCH 05/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../Legacy/ClassesConfigurationSniff.php | 214 ++++++++++++++++++ Magento2/Sniffs/Legacy/ExtendedNode.php | 25 ++ .../Legacy/ClassesConfigurationUnitTest.1.xml | 45 ++++ .../Legacy/ClassesConfigurationUnitTest.2.xml | 52 +++++ .../Legacy/ClassesConfigurationUnitTest.php | 38 ++++ Magento2/ruleset.xml | 8 + 6 files changed, 382 insertions(+) create mode 100644 Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php create mode 100644 Magento2/Sniffs/Legacy/ExtendedNode.php create mode 100644 Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml create mode 100644 Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml create mode 100644 Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php diff --git a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php new file mode 100644 index 00000000..67431932 --- /dev/null +++ b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php @@ -0,0 +1,214 @@ + 0) { + return; + } + + // We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement + // returns the position of the closing /> as the position of the tag, and we need the position of < + // 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 + ); + } + + $classes = $this->collectClassesInConfig($xml); + $this->assertNonFactoryName($phpcsFile, $stackPtr, $classes); + + $modules = $this->getXmlAttributeValues($xml, '//@module', 'module'); + $this->assertNonFactoryNameModule($phpcsFile, $stackPtr, $modules); + } + + /** + * Check whether specified classes or module names correspond to a file according PSR-1 Standard. + * + * @param File $phpcsFile + * @param int $stackPtr + * @param ExtendedNode[] $elements + */ + private function assertNonFactoryName(File $phpcsFile, int $stackPtr, array $elements) + { + foreach ($elements as $element) { + if (stripos($element->value, 'Magento') === false) { + continue; + } + if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element->value) !== 1) { + $phpcsFile->addError( + self::ERROR_MESSAGE_CONFIG, + $element->element->getLineNo() - 1, + self::ERROR_CODE_CONFIG, + ); + } + } + } + + /** + * Check whether specified classes or module names correspond to a file according PSR-1 Standard. + * + * @param File $phpcsFile + * @param int $stackPtr + * @param ExtendedNode[] $classes + */ + private function assertNonFactoryNameModule(File $phpcsFile, int $stackPtr, array $classes) + { + foreach ($classes as $element) { + if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element->value) !== 1) { + $phpcsFile->addError( + self::ERROR_MESSAGE_MODULE, + $element->element->getLineNo() - 1, + self::ERROR_CODE_MODULE, + ); + } + } + } + + /** + * 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(); + } + + /** + * @param SimpleXMLElement $xml + * @return array + */ + private function collectClassesInConfig(SimpleXMLElement $xml): array + { + $classes = $this->getXmlNode( + $xml, + ' + /config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)] + | //model[not(parent::connection)] | //backend_model | //source_model | //price_model + | //model_token | //writer_model | //clone_model | //frontend_model | //working_model + | //admin_renderer | //renderer | /config/*/di/preferences/*' + ); + $classes = array_merge($classes, $this->getXmlAttributeValues($xml, '//@backend_model', 'backend_model')); + $classes = array_merge( + $classes, + $this->getXmlNodeNames( + $xml, + '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/* | /config/*/di/preferences/*' + ) + ); + + $classes = array_map( + function(ExtendedNode $extendedNode) { + $extendedNode->value = explode('::', trim($extendedNode->value))[0]; + return $extendedNode; + }, + $classes + ); + $classes = array_filter( + $classes, + function ($value) { + return !empty($value); + } + ); + + return $classes; + } + + /** + * Get XML node text values using specified xPath + * + * The node must contain specified attribute + * + * @param SimpleXMLElement $xml + * @param string $xPath + * @return array + */ + private function getXmlNode(SimpleXMLElement $xml, string $xPath): array + { + $result = []; + $nodes = $xml->xpath($xPath) ?: []; + foreach ($nodes as $node) { + $result[] = new ExtendedNode((string)$node, $node); + } + return $result; + } + + /** + * Get XML node names using specified xPath + * + * @param SimpleXMLElement $xml + * @param string $xpath + * @return array + */ + private function getXmlNodeNames(SimpleXMLElement $xml, string $xpath): array + { + $result = []; + $nodes = $xml->xpath($xpath) ?: []; + foreach ($nodes as $node) { + $result[] = new ExtendedNode($node->getName(), $node); + } + return $result; + } + + /** + * Get XML node attribute values using specified xPath + * + * @param SimpleXMLElement $xml + * @param string $xPath + * @param string $attributeName + * @return array + */ + private function getXmlAttributeValues(SimpleXMLElement $xml, string $xPath, string $attributeName): array + { + $result = []; + $nodes = $xml->xpath($xPath) ?: []; + foreach ($nodes as $node) { + $nodeArray = (array)$node; + if (isset($nodeArray['@attributes'][$attributeName])) { + $result[] = new ExtendedNode($nodeArray['@attributes'][$attributeName], $node); + } + } + return $result; + } +} diff --git a/Magento2/Sniffs/Legacy/ExtendedNode.php b/Magento2/Sniffs/Legacy/ExtendedNode.php new file mode 100644 index 00000000..dbbd8e8d --- /dev/null +++ b/Magento2/Sniffs/Legacy/ExtendedNode.php @@ -0,0 +1,25 @@ +value = $value; + $this->element = dom_import_simplexml($element); + } +} \ No newline at end of file diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml new file mode 100644 index 00000000..f4a7b5cd --- /dev/null +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml @@ -0,0 +1,45 @@ + + + + +
+ + + + + Magento\Config\Model\Config\Source\Yesno + + + + + + + Magento\CONFIG\Model\Config\Source\Yesno + + + + + + + Config\Model\Config\Source\Yesno + + + + + + + Sales\MODEL\Config\Source\Order\Status\NewStatus + + + + MAGENTO\Payment\Model\Config\Source\Allspecificcountries + + +
+
+
\ No newline at end of file diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml new file mode 100644 index 00000000..5ea35a2e --- /dev/null +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml @@ -0,0 +1,52 @@ + + + + +
+ + + + + Magento\Config\Model\Config\Source\Yesno + + + + + + + + Magento\CONFIG\Model\Config\Source\Yesno + + + + + + + + Config\Model\Config\Source\Yesno + + + + + + + Sales\MODEL\Config\Source\Order\Status\NewStatus + + + + + + + MAGENTO\Payment\Model\Config\Source\Allspecificcountries + + + + +
+
+
\ No newline at end of file diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php new file mode 100644 index 00000000..20a249b2 --- /dev/null +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php @@ -0,0 +1,38 @@ + 1, + 40 => 1, + ]; + } + if ($testFile === 'ClassesConfigurationUnitTest.2.xml') { + return [ + 22 => 1, + 42 => 1, + ]; + } + } + + /** + * @inheritdoc + */ + public function getWarningList($testFile = '') + { + return []; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 212bf246..d1b4cfb0 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -115,6 +115,14 @@ 10 error + + *\/etc/*.xml$ + *\/etc/wsdl.xml$ + *\/etc/wsdl2.xml$ + *\/etc/wsi.xml$ + 10 + warning + From 2c5c076f64a91351166dfdb4f93417f34dd3b37d Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 11:33:26 +0200 Subject: [PATCH 06/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/Sniffs/Legacy/ExtendedNode.php | 2 +- .../Legacy/ObsoleteMethodsUsageSniff.php | 69 ------------------- .../Legacy/ClassesConfigurationUnitTest.1.xml | 2 +- .../Legacy/ClassesConfigurationUnitTest.2.xml | 2 +- .../Legacy/ObsoleteMethodsUsageUnitTest.inc | 6 -- .../Legacy/ObsoleteMethodsUsageUnitTest.php | 31 --------- 6 files changed, 3 insertions(+), 109 deletions(-) delete mode 100644 Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php delete mode 100644 Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.inc delete mode 100644 Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.php diff --git a/Magento2/Sniffs/Legacy/ExtendedNode.php b/Magento2/Sniffs/Legacy/ExtendedNode.php index dbbd8e8d..8efb770e 100644 --- a/Magento2/Sniffs/Legacy/ExtendedNode.php +++ b/Magento2/Sniffs/Legacy/ExtendedNode.php @@ -22,4 +22,4 @@ public function __construct(string $value, SimpleXMLElement $element) $this->value = $value; $this->element = dom_import_simplexml($element); } -} \ No newline at end of file +} diff --git a/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php b/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php deleted file mode 100644 index d12c9fa6..00000000 --- a/Magento2/Sniffs/Legacy/ObsoleteMethodsUsageSniff.php +++ /dev/null @@ -1,69 +0,0 @@ -getTokens(); - $methodNameStackPtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true); - if ($methodNameStackPtr === false) { - return; - } - $name = $tokens[$methodNameStackPtr]['content']; - if (in_array($name, $this->obsoleteStaticMethods)) { - $classNameStackPtr = $phpcsFile->findPrevious( - [T_STRING], - $methodNameStackPtr - 1, - null, - false, - null, - true - ); - if ($classNameStackPtr === false) { - return; - } - if ($tokens[$classNameStackPtr]['content'] === 'Mage') { - $phpcsFile->addError( - self::ERROR_MESSAGE, - $methodNameStackPtr + 1, - self::ERROR_CODE, - ); - } - } - } -} diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml index f4a7b5cd..864a154e 100644 --- a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml @@ -42,4 +42,4 @@ - \ No newline at end of file + diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml index 5ea35a2e..2753291f 100644 --- a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml @@ -49,4 +49,4 @@ - \ No newline at end of file + diff --git a/Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.inc b/Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.inc deleted file mode 100644 index 91583ae7..00000000 --- a/Magento2/Tests/Legacy/ObsoleteMethodsUsageUnitTest.inc +++ /dev/null @@ -1,6 +0,0 @@ - 1, - 5 => 1, - 6 => 1, - ]; - } - - /** - * @inheritdoc - */ - public function getWarningList() - { - return []; - } -} From 3636e9d81ac44ef12a8e4344d09962d21968c552 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 11:34:27 +0200 Subject: [PATCH 07/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/Sniffs/Legacy/ExtendedNode.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Magento2/Sniffs/Legacy/ExtendedNode.php b/Magento2/Sniffs/Legacy/ExtendedNode.php index 8efb770e..e3c69516 100644 --- a/Magento2/Sniffs/Legacy/ExtendedNode.php +++ b/Magento2/Sniffs/Legacy/ExtendedNode.php @@ -17,6 +17,10 @@ class ExtendedNode */ public $element; + /** + * @param string $value + * @param SimpleXMLElement $element + */ public function __construct(string $value, SimpleXMLElement $element) { $this->value = $value; From 029356085c064db4fd437e7a667268f267898428 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 11:35:40 +0200 Subject: [PATCH 08/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php index 67431932..4502ed27 100644 --- a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php @@ -52,20 +52,19 @@ public function process(File $phpcsFile, $stackPtr) } $classes = $this->collectClassesInConfig($xml); - $this->assertNonFactoryName($phpcsFile, $stackPtr, $classes); + $this->assertNonFactoryName($phpcsFile, $classes); $modules = $this->getXmlAttributeValues($xml, '//@module', 'module'); - $this->assertNonFactoryNameModule($phpcsFile, $stackPtr, $modules); + $this->assertNonFactoryNameModule($phpcsFile, $modules); } /** * Check whether specified classes or module names correspond to a file according PSR-1 Standard. * * @param File $phpcsFile - * @param int $stackPtr * @param ExtendedNode[] $elements */ - private function assertNonFactoryName(File $phpcsFile, int $stackPtr, array $elements) + private function assertNonFactoryName(File $phpcsFile, array $elements) { foreach ($elements as $element) { if (stripos($element->value, 'Magento') === false) { @@ -85,10 +84,9 @@ private function assertNonFactoryName(File $phpcsFile, int $stackPtr, array $ele * Check whether specified classes or module names correspond to a file according PSR-1 Standard. * * @param File $phpcsFile - * @param int $stackPtr * @param ExtendedNode[] $classes */ - private function assertNonFactoryNameModule(File $phpcsFile, int $stackPtr, array $classes) + private function assertNonFactoryNameModule(File $phpcsFile, array $classes) { foreach ($classes as $element) { if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element->value) !== 1) { From b91d0d4afdffdc8537cbf1d903ff301a4bfd2674 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 11:45:29 +0200 Subject: [PATCH 09/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php | 7 +++++-- Magento2/Sniffs/Legacy/ExtendedNode.php | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php index 4502ed27..46ae7cfe 100644 --- a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php @@ -1,8 +1,9 @@ value = explode('::', trim($extendedNode->value))[0]; return $extendedNode; }, diff --git a/Magento2/Sniffs/Legacy/ExtendedNode.php b/Magento2/Sniffs/Legacy/ExtendedNode.php index e3c69516..44c382fa 100644 --- a/Magento2/Sniffs/Legacy/ExtendedNode.php +++ b/Magento2/Sniffs/Legacy/ExtendedNode.php @@ -1,4 +1,8 @@ Date: Thu, 16 Sep 2021 11:55:27 +0200 Subject: [PATCH 10/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../Tests/Legacy/ClassesConfigurationUnitTest.3.xml | 13 +++++++++++++ .../Tests/Legacy/ClassesConfigurationUnitTest.php | 6 ++++++ 2 files changed, 19 insertions(+) create mode 100644 Magento2/Tests/Legacy/ClassesConfigurationUnitTest.3.xml diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.3.xml b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.3.xml new file mode 100644 index 00000000..4884a838 --- /dev/null +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.3.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php index 20a249b2..ad788298 100644 --- a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php @@ -26,6 +26,12 @@ public function getErrorList($testFile = '') 42 => 1, ]; } + if ($testFile === 'ClassesConfigurationUnitTest.3.xml') { + return [ + 10 => 1, + ]; + } + return []; } /** From 5810c6e22bfb6f5575fa0c0c591a2f4855ee7571 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 15:30:23 +0200 Subject: [PATCH 11/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../Legacy/ClassesConfigurationSniff.php | 5 +-- .../Legacy/ClassesConfigurationUnitTest.4.xml | 36 +++++++++++++++++++ .../Legacy/ClassesConfigurationUnitTest.php | 5 +++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 Magento2/Tests/Legacy/ClassesConfigurationUnitTest.4.xml diff --git a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php index 46ae7cfe..794eba9e 100644 --- a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php @@ -128,14 +128,15 @@ private function collectClassesInConfig(SimpleXMLElement $xml): array /config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)] | //model[not(parent::connection)] | //backend_model | //source_model | //price_model | //model_token | //writer_model | //clone_model | //frontend_model | //working_model - | //admin_renderer | //renderer | /config/*/di/preferences/*' + | //admin_renderer | //renderer' ); $classes = array_merge($classes, $this->getXmlAttributeValues($xml, '//@backend_model', 'backend_model')); + $classes = array_merge($classes, $this->getXmlAttributeValues($xml, '/config//preference', 'type')); $classes = array_merge( $classes, $this->getXmlNodeNames( $xml, - '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/* | /config/*/di/preferences/*' + '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*' ) ); diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.4.xml b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.4.xml new file mode 100644 index 00000000..a99f0589 --- /dev/null +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.4.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + system/currency/installed + + + + + Magento\Framework\Communication\Config\Reader\XmlReader\Converter + Magento\Framework\Communication\Config\Reader\XmlReader\SchemaLocator + communication.xml + + name + name + + + + + + Magento + + + diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php index ad788298..8d747e19 100644 --- a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php +++ b/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php @@ -31,6 +31,11 @@ public function getErrorList($testFile = '') 10 => 1, ]; } + if ($testFile === 'ClassesConfigurationUnitTest.4.xml') { + return [ + 10 => 1, + ]; + } return []; } From 0bc42a6828ceb03bf405909f16d45913ac7d3431 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 16:41:12 +0200 Subject: [PATCH 12/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...hp => ClassReferencesInConfigurationFilesSniff.php} | 2 +- ... ClassReferencesInConfigurationFilesUnitTest.1.xml} | 0 ... ClassReferencesInConfigurationFilesUnitTest.2.xml} | 0 ... ClassReferencesInConfigurationFilesUnitTest.3.xml} | 0 ... ClassReferencesInConfigurationFilesUnitTest.4.xml} | 0 ...=> ClassReferencesInConfigurationFilesUnitTest.php} | 10 +++++----- 6 files changed, 6 insertions(+), 6 deletions(-) rename Magento2/Sniffs/Legacy/{ClassesConfigurationSniff.php => ClassReferencesInConfigurationFilesSniff.php} (99%) rename Magento2/Tests/Legacy/{ClassesConfigurationUnitTest.1.xml => ClassReferencesInConfigurationFilesUnitTest.1.xml} (100%) rename Magento2/Tests/Legacy/{ClassesConfigurationUnitTest.2.xml => ClassReferencesInConfigurationFilesUnitTest.2.xml} (100%) rename Magento2/Tests/Legacy/{ClassesConfigurationUnitTest.3.xml => ClassReferencesInConfigurationFilesUnitTest.3.xml} (100%) rename Magento2/Tests/Legacy/{ClassesConfigurationUnitTest.4.xml => ClassReferencesInConfigurationFilesUnitTest.4.xml} (100%) rename Magento2/Tests/Legacy/{ClassesConfigurationUnitTest.php => ClassReferencesInConfigurationFilesUnitTest.php} (65%) diff --git a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php similarity index 99% rename from Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php rename to Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 794eba9e..55dff999 100644 --- a/Magento2/Sniffs/Legacy/ClassesConfigurationSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -11,7 +11,7 @@ use PHP_CodeSniffer\Files\File; use SimpleXMLElement; -class ClassesConfigurationSniff implements Sniff +class ClassReferencesInConfigurationFilesSniff implements Sniff { private const ERROR_MESSAGE_CONFIG = 'Attribute does not follow expected format'; private const ERROR_CODE_CONFIG = 'WrongXML'; diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.1.xml similarity index 100% rename from Magento2/Tests/Legacy/ClassesConfigurationUnitTest.1.xml rename to Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.1.xml diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.2.xml similarity index 100% rename from Magento2/Tests/Legacy/ClassesConfigurationUnitTest.2.xml rename to Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.2.xml diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.3.xml b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.3.xml similarity index 100% rename from Magento2/Tests/Legacy/ClassesConfigurationUnitTest.3.xml rename to Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.3.xml diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.4.xml b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.4.xml similarity index 100% rename from Magento2/Tests/Legacy/ClassesConfigurationUnitTest.4.xml rename to Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.4.xml diff --git a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.php similarity index 65% rename from Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php rename to Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.php index 8d747e19..83bb6f1d 100644 --- a/Magento2/Tests/Legacy/ClassesConfigurationUnitTest.php +++ b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.php @@ -7,31 +7,31 @@ use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; -class ClassesConfigurationUnitTest extends AbstractSniffUnitTest +class ClassReferencesInConfigurationFilesUnitTest extends AbstractSniffUnitTest { /** * @inheritdoc */ public function getErrorList($testFile = '') { - if ($testFile === 'ClassesConfigurationUnitTest.1.xml') { + if ($testFile === 'ClassReferencesInConfigurationFilesUnitTest.1.xml') { return [ 22 => 1, 40 => 1, ]; } - if ($testFile === 'ClassesConfigurationUnitTest.2.xml') { + if ($testFile === 'ClassReferencesInConfigurationFilesUnitTest.2.xml') { return [ 22 => 1, 42 => 1, ]; } - if ($testFile === 'ClassesConfigurationUnitTest.3.xml') { + if ($testFile === 'ClassReferencesInConfigurationFilesUnitTest.3.xml') { return [ 10 => 1, ]; } - if ($testFile === 'ClassesConfigurationUnitTest.4.xml') { + if ($testFile === 'ClassReferencesInConfigurationFilesUnitTest.4.xml') { return [ 10 => 1, ]; From 19bb57b569245f20d96a0feded57fdb127492565 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 17:19:22 +0200 Subject: [PATCH 13/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- Magento2/ruleset.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index d1b4cfb0..65d2cb3b 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -115,7 +115,7 @@ 10 error - + *\/etc/*.xml$ *\/etc/wsdl.xml$ *\/etc/wsdl2.xml$ From 48a58169ac51178250625b96798be3f69233a0ff Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 16 Sep 2021 17:20:36 +0200 Subject: [PATCH 14/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../Legacy/ClassReferencesInConfigurationFilesSniff.php | 4 ++-- Magento2/Sniffs/Legacy/ExtendedNode.php | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 55dff999..79f804af 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -74,7 +74,7 @@ private function assertNonFactoryName(File $phpcsFile, array $elements) if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element->value) !== 1) { $phpcsFile->addError( self::ERROR_MESSAGE_CONFIG, - $element->element->getLineNo() - 1, + $element->lineNumber - 1, self::ERROR_CODE_CONFIG, ); } @@ -93,7 +93,7 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes) if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element->value) !== 1) { $phpcsFile->addError( self::ERROR_MESSAGE_MODULE, - $element->element->getLineNo() - 1, + $element->lineNumber - 1, self::ERROR_CODE_MODULE, ); } diff --git a/Magento2/Sniffs/Legacy/ExtendedNode.php b/Magento2/Sniffs/Legacy/ExtendedNode.php index 44c382fa..0320e63c 100644 --- a/Magento2/Sniffs/Legacy/ExtendedNode.php +++ b/Magento2/Sniffs/Legacy/ExtendedNode.php @@ -6,7 +6,6 @@ namespace Magento2\Sniffs\Legacy; -use DOMElement; use SimpleXMLElement; class ExtendedNode @@ -17,9 +16,9 @@ class ExtendedNode public $value; /** - * @var DOMElement + * @var int */ - public $element; + public $lineNumber; /** * @param string $value @@ -28,6 +27,6 @@ class ExtendedNode public function __construct(string $value, SimpleXMLElement $element) { $this->value = $value; - $this->element = dom_import_simplexml($element); + $this->lineNumber = dom_import_simplexml($element)->getLineNo(); } } From b10df58d041ee7bf6993d9e76013c5c3e1cb62fb Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Fri, 17 Sep 2021 13:58:15 +0200 Subject: [PATCH 15/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...assReferencesInConfigurationFilesSniff.php | 106 ++++++++---------- 1 file changed, 46 insertions(+), 60 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 79f804af..6e4f37f7 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -18,6 +18,10 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff private const ERROR_MESSAGE_MODULE = 'Attribute does not follow expected format in module'; private const ERROR_CODE_MODULE = 'WrongXMLModule'; + private const FROM_CONTENT = 1; + private const FROM_NAME = 2; + private const FROM_ATTRIBUTE = 3; + /** * @inheritdoc */ @@ -55,7 +59,7 @@ public function process(File $phpcsFile, $stackPtr) $classes = $this->collectClassesInConfig($xml); $this->assertNonFactoryName($phpcsFile, $classes); - $modules = $this->getXmlAttributeValues($xml, '//@module', 'module'); + $modules = $this->getValuesFromXml($xml, '//@module', self::FROM_ATTRIBUTE, 'module'); $this->assertNonFactoryNameModule($phpcsFile, $modules); } @@ -122,21 +126,39 @@ private function getFormattedXML(File $phpcsFile) */ private function collectClassesInConfig(SimpleXMLElement $xml): array { - $classes = $this->getXmlNode( + $classes = $this->getValuesFromXml( $xml, ' /config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)] | //model[not(parent::connection)] | //backend_model | //source_model | //price_model | //model_token | //writer_model | //clone_model | //frontend_model | //working_model - | //admin_renderer | //renderer' + | //admin_renderer | //renderer', + self::FROM_CONTENT + ); + $classes = array_merge( + $classes, + $this->getValuesFromXml( + $xml, + '//@backend_model', + self::FROM_ATTRIBUTE, + 'backend_model' + ) ); - $classes = array_merge($classes, $this->getXmlAttributeValues($xml, '//@backend_model', 'backend_model')); - $classes = array_merge($classes, $this->getXmlAttributeValues($xml, '/config//preference', 'type')); $classes = array_merge( $classes, - $this->getXmlNodeNames( + $this->getValuesFromXml( $xml, - '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*' + '/config//preference', + self::FROM_ATTRIBUTE, + 'type' + ) + ); + $classes = array_merge( + $classes, + $this->getValuesFromXml( + $xml, + '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*', + self::FROM_NAME ) ); @@ -147,70 +169,34 @@ function (ExtendedNode $extendedNode) { }, $classes ); - $classes = array_filter( - $classes, - function ($value) { - return !empty($value); - } - ); return $classes; } /** - * Get XML node text values using specified xPath - * - * The node must contain specified attribute - * + * Extract value from the specified $extractFrom which exist in the XML path + * * @param SimpleXMLElement $xml * @param string $xPath + * @param int $extractFrom + * @param string $attribute * @return array */ - private function getXmlNode(SimpleXMLElement $xml, string $xPath): array + private function getValuesFromXml(SimpleXMLElement $xml, string $xPath, int $extractFrom, string $attribute = ''): array { - $result = []; $nodes = $xml->xpath($xPath) ?: []; - foreach ($nodes as $node) { - $result[] = new ExtendedNode((string)$node, $node); - } - return $result; - } - - /** - * Get XML node names using specified xPath - * - * @param SimpleXMLElement $xml - * @param string $xpath - * @return array - */ - private function getXmlNodeNames(SimpleXMLElement $xml, string $xpath): array - { - $result = []; - $nodes = $xml->xpath($xpath) ?: []; - foreach ($nodes as $node) { - $result[] = new ExtendedNode($node->getName(), $node); - } - return $result; - } - - /** - * Get XML node attribute values using specified xPath - * - * @param SimpleXMLElement $xml - * @param string $xPath - * @param string $attributeName - * @return array - */ - private function getXmlAttributeValues(SimpleXMLElement $xml, string $xPath, string $attributeName): array - { - $result = []; - $nodes = $xml->xpath($xPath) ?: []; - foreach ($nodes as $node) { - $nodeArray = (array)$node; - if (isset($nodeArray['@attributes'][$attributeName])) { - $result[] = new ExtendedNode($nodeArray['@attributes'][$attributeName], $node); + return array_map(function($item) use ($extractFrom, $attribute) { + switch ($extractFrom) { + case self::FROM_CONTENT: + return new ExtendedNode((string)$item, $item); + case self::FROM_NAME: + return new ExtendedNode($item->getName(), $item); + case self::FROM_ATTRIBUTE: + $nodeArray = (array)$item; + if (isset($nodeArray['@attributes'][$attribute])) { + return new ExtendedNode($nodeArray['@attributes'][$attribute], $item); + } } - } - return $result; + }, $nodes); } } From 64fa0cc80ec8ea6f4a9b690bd237f153c074b523 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Fri, 17 Sep 2021 14:01:10 +0200 Subject: [PATCH 16/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../Legacy/ClassReferencesInConfigurationFilesSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 6e4f37f7..a2348a96 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -64,7 +64,7 @@ public function process(File $phpcsFile, $stackPtr) } /** - * Check whether specified classes or module names correspond to a file according PSR-1 Standard. + * Check whether specified class names are right according PSR-1 Standard. * * @param File $phpcsFile * @param ExtendedNode[] $elements @@ -86,7 +86,7 @@ private function assertNonFactoryName(File $phpcsFile, array $elements) } /** - * Check whether specified classes or module names correspond to a file according PSR-1 Standard. + * Check whether specified class names in modules are right according PSR-1 Standard. * * @param File $phpcsFile * @param ExtendedNode[] $classes From 5aa57215c506a06e05e7b59fc6cdb27615b1438d Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Fri, 17 Sep 2021 14:04:19 +0200 Subject: [PATCH 17/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../ClassReferencesInConfigurationFilesSniff.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index a2348a96..700bc864 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -175,17 +175,17 @@ function (ExtendedNode $extendedNode) { /** * Extract value from the specified $extractFrom which exist in the XML path - * + * * @param SimpleXMLElement $xml * @param string $xPath * @param int $extractFrom - * @param string $attribute + * @param string $attr * @return array */ - private function getValuesFromXml(SimpleXMLElement $xml, string $xPath, int $extractFrom, string $attribute = ''): array + private function getValuesFromXml(SimpleXMLElement $xml, string $xPath, int $extractFrom, string $attr = ''): array { $nodes = $xml->xpath($xPath) ?: []; - return array_map(function($item) use ($extractFrom, $attribute) { + return array_map(function ($item) use ($extractFrom, $attr) { switch ($extractFrom) { case self::FROM_CONTENT: return new ExtendedNode((string)$item, $item); @@ -193,8 +193,8 @@ private function getValuesFromXml(SimpleXMLElement $xml, string $xPath, int $ext return new ExtendedNode($item->getName(), $item); case self::FROM_ATTRIBUTE: $nodeArray = (array)$item; - if (isset($nodeArray['@attributes'][$attribute])) { - return new ExtendedNode($nodeArray['@attributes'][$attribute], $item); + if (isset($nodeArray['@attributes'][$attr])) { + return new ExtendedNode($nodeArray['@attributes'][$attr], $item); } } }, $nodes); From 389d7cab75fdc0c3d3273f770f7c13e1e9e35ebb Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 20 Sep 2021 09:37:06 +0200 Subject: [PATCH 18/31] Update Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php Co-authored-by: Sergii Ivashchenko --- .../Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 700bc864..6cc79da0 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -13,7 +13,7 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff { - private const ERROR_MESSAGE_CONFIG = 'Attribute does not follow expected format'; + private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference'; private const ERROR_CODE_CONFIG = 'WrongXML'; private const ERROR_MESSAGE_MODULE = 'Attribute does not follow expected format in module'; private const ERROR_CODE_MODULE = 'WrongXMLModule'; From c4801005cb48be77c4047287692cf7bef1d0c979 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 20 Sep 2021 09:37:12 +0200 Subject: [PATCH 19/31] Update Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php Co-authored-by: Sergii Ivashchenko --- .../Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 6cc79da0..a33c6314 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -14,7 +14,7 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff { private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference'; - private const ERROR_CODE_CONFIG = 'WrongXML'; + private const ERROR_CODE_CONFIG = 'IncorrectClassReference'; private const ERROR_MESSAGE_MODULE = 'Attribute does not follow expected format in module'; private const ERROR_CODE_MODULE = 'WrongXMLModule'; From c0ead7c6d50fa0899b16bd0496161cde68748ea0 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 20 Sep 2021 09:37:18 +0200 Subject: [PATCH 20/31] Update Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php Co-authored-by: Sergii Ivashchenko --- .../Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index a33c6314..cbfc7b2b 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -15,7 +15,7 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff { private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference'; private const ERROR_CODE_CONFIG = 'IncorrectClassReference'; - private const ERROR_MESSAGE_MODULE = 'Attribute does not follow expected format in module'; + private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference'; private const ERROR_CODE_MODULE = 'WrongXMLModule'; private const FROM_CONTENT = 1; From 7b389222046074ffc4ac594b41faa26230f7c759 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 20 Sep 2021 09:37:23 +0200 Subject: [PATCH 21/31] Update Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php Co-authored-by: Sergii Ivashchenko --- .../Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index cbfc7b2b..db6950b3 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -16,7 +16,7 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff 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'; - private const ERROR_CODE_MODULE = 'WrongXMLModule'; + private const ERROR_CODE_MODULE = 'InforrectModuleReference'; private const FROM_CONTENT = 1; private const FROM_NAME = 2; From 9a6963854e2e6ca074e2b64ef9b384885494eeb9 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 20 Sep 2021 09:37:43 +0200 Subject: [PATCH 22/31] Update Magento2/ruleset.xml Co-authored-by: Sergii Ivashchenko --- Magento2/ruleset.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 65d2cb3b..8e78cb06 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -121,7 +121,7 @@ *\/etc/wsdl2.xml$ *\/etc/wsi.xml$ 10 - warning + error From 936c28c187c3e336fa72a8b7c2be76cd17f0f03a Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Mon, 20 Sep 2021 10:11:43 +0200 Subject: [PATCH 23/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...assReferencesInConfigurationFilesSniff.php | 97 ++++++++++++------- Magento2/Sniffs/Legacy/ExtendedNode.php | 32 ------ 2 files changed, 60 insertions(+), 69 deletions(-) delete mode 100644 Magento2/Sniffs/Legacy/ExtendedNode.php diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index db6950b3..6255cfa0 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -16,11 +16,7 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff 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'; - private const ERROR_CODE_MODULE = 'InforrectModuleReference'; - - private const FROM_CONTENT = 1; - private const FROM_NAME = 2; - private const FROM_ATTRIBUTE = 3; + private const ERROR_CODE_MODULE = 'IncorrectModuleReference'; /** * @inheritdoc @@ -59,7 +55,7 @@ public function process(File $phpcsFile, $stackPtr) $classes = $this->collectClassesInConfig($xml); $this->assertNonFactoryName($phpcsFile, $classes); - $modules = $this->getValuesFromXml($xml, '//@module', self::FROM_ATTRIBUTE, 'module'); + $modules = $this->getValuesFromXmlTagAttribute($xml, '//@module', 'module'); $this->assertNonFactoryNameModule($phpcsFile, $modules); } @@ -67,18 +63,18 @@ public function process(File $phpcsFile, $stackPtr) * Check whether specified class names are right according PSR-1 Standard. * * @param File $phpcsFile - * @param ExtendedNode[] $elements + * @param array $elements */ private function assertNonFactoryName(File $phpcsFile, array $elements) { foreach ($elements as $element) { - if (stripos($element->value, 'Magento') === false) { + if (stripos($element['value'], 'Magento') === false) { continue; } - if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element->value) !== 1) { + if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element['value']) !== 1) { $phpcsFile->addError( self::ERROR_MESSAGE_CONFIG, - $element->lineNumber - 1, + $element['lineNumber'], self::ERROR_CODE_CONFIG, ); } @@ -89,15 +85,15 @@ private function assertNonFactoryName(File $phpcsFile, array $elements) * Check whether specified class names in modules are right according PSR-1 Standard. * * @param File $phpcsFile - * @param ExtendedNode[] $classes + * @param array $classes */ private function assertNonFactoryNameModule(File $phpcsFile, array $classes) { foreach ($classes as $element) { - if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element->value) !== 1) { + if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element['value']) !== 1) { $phpcsFile->addError( self::ERROR_MESSAGE_MODULE, - $element->lineNumber - 1, + $element['lineNumber'], self::ERROR_CODE_MODULE, ); } @@ -126,45 +122,41 @@ private function getFormattedXML(File $phpcsFile) */ private function collectClassesInConfig(SimpleXMLElement $xml): array { - $classes = $this->getValuesFromXml( + $classes = $this->getValuesFromXmlTagContent( $xml, ' /config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)] | //model[not(parent::connection)] | //backend_model | //source_model | //price_model | //model_token | //writer_model | //clone_model | //frontend_model | //working_model | //admin_renderer | //renderer', - self::FROM_CONTENT ); $classes = array_merge( $classes, - $this->getValuesFromXml( + $this->getValuesFromXmlTagAttribute( $xml, '//@backend_model', - self::FROM_ATTRIBUTE, 'backend_model' ) ); $classes = array_merge( $classes, - $this->getValuesFromXml( + $this->getValuesFromXmlTagAttribute( $xml, '/config//preference', - self::FROM_ATTRIBUTE, 'type' ) ); $classes = array_merge( $classes, - $this->getValuesFromXml( + $this->getValuesFromXmlTagName( $xml, '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*', - self::FROM_NAME ) ); $classes = array_map( - function (ExtendedNode $extendedNode) { - $extendedNode->value = explode('::', trim($extendedNode->value))[0]; + function (array $extendedNode) { + $extendedNode['value'] = explode('::', trim($extendedNode['value']))[0]; return $extendedNode; }, $classes @@ -174,28 +166,59 @@ function (ExtendedNode $extendedNode) { } /** - * Extract value from the specified $extractFrom which exist in the XML path + * Extract value from tag contents which exist in the XML path + * + * @param SimpleXMLElement $xml + * @param string $xPath + * @return array + */ + private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath): array + { + $nodes = $xml->xpath($xPath) ?: []; + return array_map(function ($item) { + return [ + 'value' => (string)$item, + 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, + ]; + }, $nodes); + } + + /** + * Extract value from tag names which exist in the XML path + * + * @param SimpleXMLElement $xml + * @param string $xPath + * @return array + */ + private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): array + { + $nodes = $xml->xpath($xPath) ?: []; + return array_map(function ($item) { + return [ + 'value' => $item->getName(), + 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, + ]; + }, $nodes); + } + + /** + * Extract value from tag attributes which exist in the XML path * * @param SimpleXMLElement $xml * @param string $xPath - * @param int $extractFrom * @param string $attr * @return array */ - private function getValuesFromXml(SimpleXMLElement $xml, string $xPath, int $extractFrom, string $attr = ''): array + private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPath, string $attr): array { $nodes = $xml->xpath($xPath) ?: []; - return array_map(function ($item) use ($extractFrom, $attr) { - switch ($extractFrom) { - case self::FROM_CONTENT: - return new ExtendedNode((string)$item, $item); - case self::FROM_NAME: - return new ExtendedNode($item->getName(), $item); - case self::FROM_ATTRIBUTE: - $nodeArray = (array)$item; - if (isset($nodeArray['@attributes'][$attr])) { - return new ExtendedNode($nodeArray['@attributes'][$attr], $item); - } + return array_map(function ($item) use ($attr) { + $nodeArray = (array)$item; + if (isset($nodeArray['@attributes'][$attr])) { + return [ + 'value' => $nodeArray['@attributes'][$attr], + 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, + ]; } }, $nodes); } diff --git a/Magento2/Sniffs/Legacy/ExtendedNode.php b/Magento2/Sniffs/Legacy/ExtendedNode.php deleted file mode 100644 index 0320e63c..00000000 --- a/Magento2/Sniffs/Legacy/ExtendedNode.php +++ /dev/null @@ -1,32 +0,0 @@ -value = $value; - $this->lineNumber = dom_import_simplexml($element)->getLineNo(); - } -} From fbe2c1c05324a3289757c2390245143617b8eb5d Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Tue, 21 Sep 2021 10:46:12 +0200 Subject: [PATCH 24/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...assReferencesInConfigurationFilesSniff.php | 60 +++++++++ ...ferencesInConfigurationFilesUnitTest.5.xml | 116 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 6255cfa0..2a528edb 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -57,6 +57,12 @@ public function process(File $phpcsFile, $stackPtr) $modules = $this->getValuesFromXmlTagAttribute($xml, '//@module', 'module'); $this->assertNonFactoryNameModule($phpcsFile, $modules); + + $layouts = $this->collectClassesInLayout($xml); + $this->assertNonFactoryName($phpcsFile, $layouts); + + $layoutTabs = $this->collectClassesInLayoutTabs($xml); + $this->assertNonFactoryNameTab($phpcsFile, $layoutTabs); } /** @@ -100,6 +106,25 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes) } } + /** + * Check whether specified class names in layout tabs are right according PSR-1 Standard. + * + * @param File $phpcsFile + * @param array $elements + */ + private function assertNonFactoryNameTab(File $phpcsFile, array $elements) + { + foreach ($elements as $element) { + if (preg_match('/\//', $element['value']) !== false) { + $phpcsFile->addError( + self::ERROR_MESSAGE_CONFIG, + $element['lineNumber'], + self::ERROR_CODE_CONFIG, + ); + } + } + } + /** * Format the incoming XML to avoid tags split into several lines. * @@ -165,6 +190,41 @@ function (array $extendedNode) { return $classes; } + private function collectClassesInLayout(SimpleXMLElement $xml): array + { + $classes = $this->getValuesFromXmlTagAttribute( + $xml, + '/layout//@helper', + 'helper' + ); + $classes = array_map( + function (array $extendedNode) { + $extendedNode['value'] = explode('::', trim($extendedNode['value']))[0]; + return $extendedNode; + }, + $classes + ); + $classes = array_merge( + $classes, + $this->getValuesFromXmlTagAttribute( + $xml, + '/layout//@module', + 'module' + ) + ); + + return $classes; + } + + private function collectClassesInLayoutTabs(SimpleXMLElement $xml): array + { + return $this->getValuesFromXmlTagContent( + $xml, + '/layout//action[@method="addTab"]/block', + ); + } + + /** * Extract value from tag contents which exist in the XML path * diff --git a/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml new file mode 100644 index 00000000..142ef2d1 --- /dev/null +++ b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml @@ -0,0 +1,116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + Product + Item Status + Original Price + Price + Qty + Subtotal + Tax Amount + Tax Percent + Discount Amount + Row Total + + + + + + col-product + col-status + col-price-original + col-price + col-ordered-qty + col-subtotal + col-tax-amount + col-tax-percent + col-discont + col-total + + + + + + + + + + + + + + + + + + + + order_info + order_tab_info + + + + order_invoices + sales_order_invoice.grid.container + + + + + + + order_shipments + sales_order_shipment.grid.container + + + order_history + Magento\Sales\Block\Adminhtml\Order\View\Tab\History + + + + order_transactions + sales_transactions.grid.container + + + + + + + + + + + + + + + + + From 34169b2d6af24057c78eb27af0ba97164827c47c Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Tue, 21 Sep 2021 10:55:26 +0200 Subject: [PATCH 25/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../ClassReferencesInConfigurationFilesSniff.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 2a528edb..9bbd9f07 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -190,6 +190,12 @@ function (array $extendedNode) { return $classes; } + /** + * Parse an XML for references to PHP class names in selected tags or attributes + * + * @param SimpleXMLElement $xml + * @return array + */ private function collectClassesInLayout(SimpleXMLElement $xml): array { $classes = $this->getValuesFromXmlTagAttribute( @@ -206,7 +212,7 @@ function (array $extendedNode) { ); $classes = array_merge( $classes, - $this->getValuesFromXmlTagAttribute( + $this->getValuesFromXmlTagAttribute( $xml, '/layout//@module', 'module' @@ -216,6 +222,12 @@ function (array $extendedNode) { return $classes; } + /** + * Extract class references from layout tabs + * + * @param SimpleXMLElement $xml + * @return array + */ private function collectClassesInLayoutTabs(SimpleXMLElement $xml): array { return $this->getValuesFromXmlTagContent( @@ -224,7 +236,6 @@ private function collectClassesInLayoutTabs(SimpleXMLElement $xml): array ); } - /** * Extract value from tag contents which exist in the XML path * From 36776b8f788c629bb73afa88e22b3ddb3d2aa5d7 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Wed, 13 Oct 2021 12:46:43 +0200 Subject: [PATCH 26/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...assReferencesInConfigurationFilesSniff.php | 60 ++----------------- 1 file changed, 4 insertions(+), 56 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index 9bbd9f07..e0187223 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types = 1); namespace Magento2\Sniffs\Legacy; @@ -60,9 +61,6 @@ public function process(File $phpcsFile, $stackPtr) $layouts = $this->collectClassesInLayout($xml); $this->assertNonFactoryName($phpcsFile, $layouts); - - $layoutTabs = $this->collectClassesInLayoutTabs($xml); - $this->assertNonFactoryNameTab($phpcsFile, $layoutTabs); } /** @@ -106,25 +104,6 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes) } } - /** - * Check whether specified class names in layout tabs are right according PSR-1 Standard. - * - * @param File $phpcsFile - * @param array $elements - */ - private function assertNonFactoryNameTab(File $phpcsFile, array $elements) - { - foreach ($elements as $element) { - if (preg_match('/\//', $element['value']) !== false) { - $phpcsFile->addError( - self::ERROR_MESSAGE_CONFIG, - $element['lineNumber'], - self::ERROR_CODE_CONFIG, - ); - } - } - } - /** * Format the incoming XML to avoid tags split into several lines. * @@ -198,41 +177,10 @@ function (array $extendedNode) { */ private function collectClassesInLayout(SimpleXMLElement $xml): array { - $classes = $this->getValuesFromXmlTagAttribute( - $xml, - '/layout//@helper', - 'helper' - ); - $classes = array_map( - function (array $extendedNode) { - $extendedNode['value'] = explode('::', trim($extendedNode['value']))[0]; - return $extendedNode; - }, - $classes - ); - $classes = array_merge( - $classes, - $this->getValuesFromXmlTagAttribute( - $xml, - '/layout//@module', - 'module' - ) - ); - - return $classes; - } - - /** - * Extract class references from layout tabs - * - * @param SimpleXMLElement $xml - * @return array - */ - private function collectClassesInLayoutTabs(SimpleXMLElement $xml): array - { - return $this->getValuesFromXmlTagContent( + return $this->getValuesFromXmlTagAttribute( $xml, - '/layout//action[@method="addTab"]/block', + '/layout//@module', + 'module' ); } From 39267bad4f6ac3349585a0b2b81e94a3ada2f11c Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Wed, 13 Oct 2021 15:03:42 +0200 Subject: [PATCH 27/31] Update Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php Co-authored-by: Sergii Ivashchenko --- .../Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index e0187223..f612c7bc 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -114,7 +114,7 @@ private function getFormattedXML(File $phpcsFile) { $doc = new DomDocument('1.0'); $doc->formatOutput = true; - $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); return $doc->saveXML(); } From 8402f7d0888c536d186bd5bc2b6548b30db141d9 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Wed, 13 Oct 2021 17:16:42 +0200 Subject: [PATCH 28/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...lassReferencesInConfigurationFilesSniff.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index e0187223..1e06d1d8 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -58,9 +58,6 @@ public function process(File $phpcsFile, $stackPtr) $modules = $this->getValuesFromXmlTagAttribute($xml, '//@module', 'module'); $this->assertNonFactoryNameModule($phpcsFile, $modules); - - $layouts = $this->collectClassesInLayout($xml); - $this->assertNonFactoryName($phpcsFile, $layouts); } /** @@ -169,21 +166,6 @@ function (array $extendedNode) { return $classes; } - /** - * Parse an XML for references to PHP class names in selected tags or attributes - * - * @param SimpleXMLElement $xml - * @return array - */ - private function collectClassesInLayout(SimpleXMLElement $xml): array - { - return $this->getValuesFromXmlTagAttribute( - $xml, - '/layout//@module', - 'module' - ); - } - /** * Extract value from tag contents which exist in the XML path * From ee4c7d6363ba28191e4089b520b26e88e5886333 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Wed, 13 Oct 2021 17:31:47 +0200 Subject: [PATCH 29/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- ...ferencesInConfigurationFilesUnitTest.5.xml | 116 ------------------ 1 file changed, 116 deletions(-) delete mode 100644 Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml diff --git a/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml b/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml deleted file mode 100644 index 142ef2d1..00000000 --- a/Magento2/Tests/Legacy/ClassReferencesInConfigurationFilesUnitTest.5.xml +++ /dev/null @@ -1,116 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - Product - Item Status - Original Price - Price - Qty - Subtotal - Tax Amount - Tax Percent - Discount Amount - Row Total - - - - - - col-product - col-status - col-price-original - col-price - col-ordered-qty - col-subtotal - col-tax-amount - col-tax-percent - col-discont - col-total - - - - - - - - - - - - - - - - - - - - order_info - order_tab_info - - - - order_invoices - sales_order_invoice.grid.container - - - - - - - order_shipments - sales_order_shipment.grid.container - - - order_history - Magento\Sales\Block\Adminhtml\Order\View\Tab\History - - - - order_transactions - sales_transactions.grid.container - - - - - - - - - - - - - - - - - From 08ed34bee413e4dff87c40224be18dc0d1ad323b Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 14 Oct 2021 12:49:33 +0200 Subject: [PATCH 30/31] Update Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jorge Cuerdo Álvarez --- .../ClassReferencesInConfigurationFilesSniff.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index b82228d7..ca1474f3 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -131,24 +131,18 @@ private function collectClassesInConfig(SimpleXMLElement $xml): array | //model_token | //writer_model | //clone_model | //frontend_model | //working_model | //admin_renderer | //renderer', ); - $classes = array_merge( +$classes = array_merge( $classes, $this->getValuesFromXmlTagAttribute( $xml, '//@backend_model', 'backend_model' - ) - ); - $classes = array_merge( - $classes, - $this->getValuesFromXmlTagAttribute( + ), +$this->getValuesFromXmlTagAttribute( $xml, '/config//preference', 'type' - ) - ); - $classes = array_merge( - $classes, + ), $this->getValuesFromXmlTagName( $xml, '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*', From c9ca788bbbdeac6598752d23e84e02a3857eebd0 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 14 Oct 2021 12:53:42 +0200 Subject: [PATCH 31/31] AC-663: Create phpcs static check for ClassesTest::testPhpCode --- .../Legacy/ClassReferencesInConfigurationFilesSniff.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php index ca1474f3..f2b5b400 100644 --- a/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php +++ b/Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php @@ -131,14 +131,15 @@ private function collectClassesInConfig(SimpleXMLElement $xml): array | //model_token | //writer_model | //clone_model | //frontend_model | //working_model | //admin_renderer | //renderer', ); -$classes = array_merge( + + $classes = array_merge( $classes, $this->getValuesFromXmlTagAttribute( $xml, '//@backend_model', 'backend_model' ), -$this->getValuesFromXmlTagAttribute( + $this->getValuesFromXmlTagAttribute( $xml, '/config//preference', 'type'