From 702cd60d087c65061e5ebc6a017a4a20ee4360d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Thu, 9 Sep 2021 11:38:04 +0200 Subject: [PATCH 1/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 114 ++++++++++++++++++ Magento2/Tests/Legacy/WidgetXMLUnitTest.1.xml | 19 +++ Magento2/Tests/Legacy/WidgetXMLUnitTest.php | 31 +++++ 3 files changed, 164 insertions(+) create mode 100644 Magento2/Sniffs/Legacy/WidgetXMLSniff.php create mode 100644 Magento2/Tests/Legacy/WidgetXMLUnitTest.1.xml create mode 100644 Magento2/Tests/Legacy/WidgetXMLUnitTest.php diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php new file mode 100644 index 00000000..f15b8a94 --- /dev/null +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -0,0 +1,114 @@ + 0) { + return; + } + + $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(), + ), + 1, + self::ERROR_CODE_XML + ); + } + + $foundElements = $xml->xpath('/widgets/*[@type]'); + + foreach ($foundElements as $element) { + if (property_exists($element->attributes(), 'type')) { + $type = $element['type']; + if (preg_match('/\//', $type)) { + $phpcsFile->addError( + "Factory name detected: {$type}.", + dom_import_simplexml($element)->getLineNo() - 1, + self::ERROR_CODE_FACTORY + ); + } + } + } + + $foundElements = $xml->xpath('/widgets/*/supported_blocks'); + foreach ($foundElements as $element) { + $phpcsFile->addError( + "Obsolete node: . To be replaced with ", + dom_import_simplexml($element)->getLineNo() - 1, + self::ERROR_CODE_OBSOLETE + ); + } + + $foundElements = $xml->xpath('/widgets/*/*/*/block_name'); + foreach ($foundElements as $element) { + $phpcsFile->addError( + "Obsolete node: . To be replaced with ", + dom_import_simplexml($element)->getLineNo() - 1, + self::ERROR_CODE_OBSOLETE + ); + } + } + + /** + * Check if the element passed is in the currently sniffed line + * + * @param SimpleXMLElement $element + * @param int $stackPtr + * @return bool + */ + private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool + { + $node = dom_import_simplexml($element); + + return $node->getLineNo() === $stackPtr + 1; + } + + /** + * 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/Tests/Legacy/WidgetXMLUnitTest.1.xml b/Magento2/Tests/Legacy/WidgetXMLUnitTest.1.xml new file mode 100644 index 00000000..9c70a9cf --- /dev/null +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.1.xml @@ -0,0 +1,19 @@ + + + + + + List of Products that are set as New + Deprecated + + Deprecated + + + + + diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.php b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php new file mode 100644 index 00000000..36026b93 --- /dev/null +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php @@ -0,0 +1,31 @@ + 1, + 12 => 1, + 14 => 1, + ]; + } + + /** + * @inheritdoc + */ + public function getWarningList($testFile = '') + { + return []; + } +} From 76359c2f8daf43e9478d3cc6dd60615ee8fb9a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Thu, 9 Sep 2021 11:40:55 +0200 Subject: [PATCH 2/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index f15b8a94..6bc673b8 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -34,7 +34,7 @@ public function register(): array */ public function process(File $phpcsFile, $stackPtr) { - if($stackPtr > 0) { + if ($stackPtr > 0) { return; } From a25e2cad4e442ed4dbe7ec6eda0393bf110bb5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Thu, 9 Sep 2021 13:17:19 +0200 Subject: [PATCH 3/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 32 ++++++++--------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index 6bc673b8..b97311b7 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -11,7 +11,7 @@ use PHP_CodeSniffer\Sniffs\Sniff; /** - * Test for obsolete nodes/attributes in the module.xml + * Test for obsolete nodes/attributes in the widget.xml */ class WidgetXMLSniff implements Sniff { @@ -39,29 +39,19 @@ public function process(File $phpcsFile, $stackPtr) } $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); - if ($xml === false) { - $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), - 1, - self::ERROR_CODE_XML - ); - } $foundElements = $xml->xpath('/widgets/*[@type]'); - foreach ($foundElements as $element) { - if (property_exists($element->attributes(), 'type')) { - $type = $element['type']; - if (preg_match('/\//', $type)) { - $phpcsFile->addError( - "Factory name detected: {$type}.", - dom_import_simplexml($element)->getLineNo() - 1, - self::ERROR_CODE_FACTORY - ); - } + if (!property_exists($element->attributes(), 'type')) { + continue; + } + $type = $element['type']; + if (preg_match('/\//', $type)) { + $phpcsFile->addError( + "Factory name detected: {$type}.", + dom_import_simplexml($element)->getLineNo() - 1, + self::ERROR_CODE_FACTORY + ); } } From dc49aaa1a2218afc466ad95b2eac4d68d19e2dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Thu, 9 Sep 2021 13:37:36 +0200 Subject: [PATCH 4/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 15 ++++++++++++++- Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml | 11 +++++++++++ Magento2/Tests/Legacy/WidgetXMLUnitTest.php | 18 +++++++++++++----- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index b97311b7..f1d85161 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -38,9 +38,22 @@ public function process(File $phpcsFile, $stackPtr) return; } - $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); + try{ + $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); + } catch (\Exception $e) { + $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; + } $foundElements = $xml->xpath('/widgets/*[@type]'); + foreach ($foundElements as $element) { if (!property_exists($element->attributes(), 'type')) { continue; diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml b/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml new file mode 100644 index 00000000..03066d9f --- /dev/null +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.php b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php index 36026b93..0af6f292 100644 --- a/Magento2/Tests/Legacy/WidgetXMLUnitTest.php +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php @@ -14,11 +14,19 @@ class WidgetXMLUnitTest extends AbstractSniffUnitTest */ public function getErrorList($testFile = '') { - return [ - 9 => 1, - 12 => 1, - 14 => 1, - ]; + if ($testFile === 'WidgetXMLUnitTest.1.xml') { + return [ + 9 => 1, + 12 => 1, + 14 => 1, + ]; + } + if ($testFile === 'WidgetXMLUnitTest.2.xml') { + return [ + 1 => 1 + ]; + } + return []; } /** From 64512dd75251493940fe2773ed30fc1f84e81bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Thu, 9 Sep 2021 16:12:14 +0200 Subject: [PATCH 5/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index f1d85161..f139e37d 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -38,7 +38,7 @@ public function process(File $phpcsFile, $stackPtr) return; } - try{ + try { $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); } catch (\Exception $e) { $phpcsFile->addError( From df14044f89bf21a33de2b27a72f67153311af15b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Thu, 9 Sep 2021 17:10:38 +0200 Subject: [PATCH 6/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 29 ++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index f139e37d..e80f221b 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -41,14 +41,11 @@ public function process(File $phpcsFile, $stackPtr) try { $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); } catch (\Exception $e) { - $phpcsFile->addError( - sprintf( - "Couldn't parse contents of '%s', check that they are in valid XML format", - $phpcsFile->getFilename(), - ), - $stackPtr, - self::ERROR_CODE_XML - ); + $this->invalidXML($phpcsFile, $stackPtr); + return; + } + if ($xml === false) { + $this->invalidXML($phpcsFile, $stackPtr); return; } @@ -87,6 +84,22 @@ public function process(File $phpcsFile, $stackPtr) } } + /** + * @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 + ); + } + /** * Check if the element passed is in the currently sniffed line * From 2f4c72ead7ef5b6994faeb96ebad161f9fdb6afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Thu, 9 Sep 2021 17:11:52 +0200 Subject: [PATCH 7/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index e80f221b..064c88c2 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -85,6 +85,8 @@ public function process(File $phpcsFile, $stackPtr) } /** + * Adds an invalid XML error + * * @param File $phpcsFile * @param int $stackPtr */ From 74c47b2928e260d0071dbc75c55b2a76c5db087d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Mon, 13 Sep 2021 11:55:33 +0200 Subject: [PATCH 8/9] AC-661: Create phpcs static check for XmlTest --- Magento2/Sniffs/Legacy/WidgetXMLSniff.php | 22 ++----------------- Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml | 22 +++++++++++++++++++ Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml | 22 +++++++++++++++++++ Magento2/Tests/Legacy/WidgetXMLUnitTest.php | 4 +++- 4 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml diff --git a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php index 064c88c2..efd62aee 100644 --- a/Magento2/Sniffs/Legacy/WidgetXMLSniff.php +++ b/Magento2/Sniffs/Legacy/WidgetXMLSniff.php @@ -38,12 +38,8 @@ public function process(File $phpcsFile, $stackPtr) return; } - try { - $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); - } catch (\Exception $e) { - $this->invalidXML($phpcsFile, $stackPtr); - return; - } + $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); + if ($xml === false) { $this->invalidXML($phpcsFile, $stackPtr); return; @@ -102,20 +98,6 @@ protected function invalidXML(File $phpcsFile, int $stackPtr): void ); } - /** - * Check if the element passed is in the currently sniffed line - * - * @param SimpleXMLElement $element - * @param int $stackPtr - * @return bool - */ - private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool - { - $node = dom_import_simplexml($element); - - return $node->getLineNo() === $stackPtr + 1; - } - /** * Format the incoming XML to avoid tags split into several lines. * diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml b/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml index 03066d9f..f99ae1ca 100644 --- a/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml @@ -7,5 +7,27 @@ --> + + + + List of Products that are set as New + + + + + Deprecated + + + + Deprecated + + + diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml b/Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml new file mode 100644 index 00000000..ef90e28b --- /dev/null +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml @@ -0,0 +1,22 @@ + + + + + + + diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.php b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php index 0af6f292..c52b920b 100644 --- a/Magento2/Tests/Legacy/WidgetXMLUnitTest.php +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php @@ -23,7 +23,9 @@ public function getErrorList($testFile = '') } if ($testFile === 'WidgetXMLUnitTest.2.xml') { return [ - 1 => 1 + 9 => 1, + 17 => 1, + 24 => 1, ]; } return []; From dae1ab386078bb872a143216285b891d27d6c967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cuerdo=20=C3=81lvarez?= Date: Mon, 13 Sep 2021 12:07:11 +0200 Subject: [PATCH 9/9] AC-661: Create phpcs static check for XmlTest --- Magento2/ruleset.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 1e247a2f..158173f3 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -247,6 +247,11 @@ 8 warning + + *\/widget.xml$ + 8 + warning +