From a7a82f95ed411d2a7ed932654d902746caf5a775 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Tue, 5 Oct 2021 15:07:51 +0200 Subject: [PATCH 1/5] AC-681: Create phpcs static check for PhtmlTemplateTest --- Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php | 136 ++++++++++++++++++ .../Tests/Legacy/PhtmlTemplateUnitTest.php | 82 +++++++++++ .../templates/PhtmlTemplateUnitTest.3.phtml | 41 ++++++ .../templates/PhtmlTemplateUnitTest.1.phtml | 41 ++++++ .../templates/PhtmlTemplateUnitTest.2.phtml | 41 ++++++ Magento2/ruleset.xml | 5 + 6 files changed, 346 insertions(+) create mode 100644 Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php create mode 100644 Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php create mode 100644 Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml create mode 100644 Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml create mode 100644 Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml diff --git a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php new file mode 100644 index 00000000..d9909c68 --- /dev/null +++ b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php @@ -0,0 +1,136 @@ + 'Please do not use "jquery/ui" library in templates. Use needed jquery ' . + 'ui widget instead.', + '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/' => 'Please do not initialize JS component in php. Do ' . + 'it in template.', + '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i' => 'Please do not initialize JS component ' . + 'in php. Do it in template.', + ]; + + /** + * @inheritdoc + */ + public function register(): array + { + return [ + T_OBJECT_OPERATOR, + T_INLINE_HTML, + T_HEREDOC + ]; + } + + /** + * @inheritdoc + */ + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + if ($tokens[$stackPtr]['code'] === T_OBJECT_OPERATOR) { + $this->checkBlockVariable($phpcsFile, $stackPtr, $tokens); + $this->checkThisVariable($phpcsFile, $stackPtr, $tokens); + } + if ($tokens[$stackPtr]['code'] === T_INLINE_HTML || $tokens[$stackPtr]['code'] === T_HEREDOC) { + $this->checkHtml($phpcsFile, $stackPtr); + + $file = $phpcsFile->getFilename(); + + if (strpos($file, '/view/frontend/templates/') !== false + || strpos($file, '/view/base/templates/') !== false + ) { + $this->checkHtmlSpecificFiles($phpcsFile, $stackPtr); + } + } + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + * @param array $tokens + */ + private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $tokens): void + { + $varPos = $phpcsFile->findPrevious(T_VARIABLE, $stackPtr - 1); + if ($tokens[$varPos]['content'] !== '$block') { + return; + } + $stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); + if (strpos($tokens[$stringPos]['content'], '_') === 0) { + $phpcsFile->addWarning( + 'Access to protected and private members of Block class is ' . + 'obsolete in phtml templates. Use only public members.', + $stringPos, + self::WARNING_CODE + ); + } + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + * @param array $tokens + */ + private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens): void + { + $varPos = $phpcsFile->findPrevious(T_VARIABLE, $stackPtr - 1); + if ($tokens[$varPos]['content'] !== '$this') { + return; + } + $stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); + if (strpos($tokens[$stringPos]['content'], 'helper') === false) { + $phpcsFile->addWarning( + 'Access to members and methods of Block class through $this is ' . + 'obsolete in phtml templates. Use only $block instead of $this.', + $stringPos, + self::WARNING_CODE + ); + } + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + */ + private function checkHtml(File $phpcsFile, int $stackPtr): void + { + $content = $phpcsFile->getTokensAsString($stackPtr, 1); + + if (preg_match('/type="text\/javascript"/', $content)) { + $phpcsFile->addWarning( + 'Please do not use "text/javascript" type attribute.', + $stackPtr, + self::WARNING_CODE + ); + } + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + */ + private function checkHtmlSpecificFiles(File $phpcsFile, int $stackPtr): void + { + $content = $phpcsFile->getTokensAsString($stackPtr, 1); + + foreach (self::OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES as $obsoleteRegex => $errorMessage) { + if (preg_match($obsoleteRegex, $content)) { + $phpcsFile->addWarning( + $errorMessage, + $stackPtr, + self::WARNING_CODE + ); + } + } + } + +} diff --git a/Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php b/Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php new file mode 100644 index 00000000..46417690 --- /dev/null +++ b/Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php @@ -0,0 +1,82 @@ +isDir()) { + continue; + } + $path = $file->getPathname(); + if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') { + $testFiles[] = $path; + } + } + + // Put them in order. + sort($testFiles); + + return $testFiles; + } + + /** + * @inheritdoc + */ + public function getErrorList() + { + return []; + } + + /** + * @inheritdoc + */ + public function getWarningList($testFile = '') + { + if ($testFile === 'PhtmlTemplateUnitTest.1.phtml' || $testFile === 'PhtmlTemplateUnitTest.2.phtml') { + return [ + 7 => 1, + 9 => 1, + 13 => 1, + 20 => 1, + 22 => 1, + 23 => 1, + 27 => 1, + 33 => 1, + 39 => 1 + ]; + } + if ($testFile === 'PhtmlTemplateUnitTest.3.phtml') + { + return [ + 9 => 1, + 20 => 1, + 22 => 1, + 23 => 1, + 27 => 1, + ]; + } + return []; + } +} diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml new file mode 100644 index 00000000..c9759f58 --- /dev/null +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml @@ -0,0 +1,41 @@ + + + +
+_getTestFunction(); +$block->getTestFunction(); +$_something = $this->something(); +$block->_getTest(); +$block = _testing(); +?> +_getTestAnotherFunction(); +?> + +helper(); +?> + + \ No newline at end of file diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml new file mode 100644 index 00000000..c9759f58 --- /dev/null +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml @@ -0,0 +1,41 @@ + + + +
+_getTestFunction(); +$block->getTestFunction(); +$_something = $this->something(); +$block->_getTest(); +$block = _testing(); +?> +_getTestAnotherFunction(); +?> + +helper(); +?> + + \ No newline at end of file diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml new file mode 100644 index 00000000..c9759f58 --- /dev/null +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml @@ -0,0 +1,41 @@ + + + +
+_getTestFunction(); +$block->getTestFunction(); +$_something = $this->something(); +$block->_getTest(); +$block = _testing(); +?> +_getTestAnotherFunction(); +?> + +helper(); +?> + + \ No newline at end of file diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 852a70da..d45aa30f 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -302,6 +302,11 @@ 8 warning + + *\.phtml$ + 8 + warning + From a5dff74183a28bc11aeca87a6ea7d3bfe2617284 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Tue, 5 Oct 2021 15:17:16 +0200 Subject: [PATCH 2/5] AC-681: Create phpcs static check for PhtmlTemplateTest --- .../view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml | 1 - .../view/base/templates/PhtmlTemplateUnitTest.1.phtml | 1 - .../view/frontend/templates/PhtmlTemplateUnitTest.2.phtml | 1 - 3 files changed, 3 deletions(-) diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml index c9759f58..772783c9 100644 --- a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml @@ -38,4 +38,3 @@ $this->helper(); ?> - \ No newline at end of file diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml index c9759f58..772783c9 100644 --- a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml @@ -38,4 +38,3 @@ $this->helper(); ?> - \ No newline at end of file diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml index c9759f58..772783c9 100644 --- a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml @@ -38,4 +38,3 @@ $this->helper(); ?> - \ No newline at end of file From 203e8af1e3aa67fe196e3c661911efae30652420 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Tue, 5 Oct 2021 15:28:11 +0200 Subject: [PATCH 3/5] AC-681: Create phpcs static check for PhtmlTemplateTest --- Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php index d9909c68..431b6f00 100644 --- a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php +++ b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php @@ -10,11 +10,11 @@ class PhtmlTemplateSniff implements Sniff private const WARNING_CODE = 'PhtmlTemplateObsolete'; private const OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES = [ - '/(["\'])jquery\/ui\1/' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ' . + '/(["\'])jquery\/ui\1/' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ' . 'ui widget instead.', - '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/' => 'Please do not initialize JS component in php. Do ' . + '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/' => 'Please do not initialize JS component in php. Do ' . 'it in template.', - '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i' => 'Please do not initialize JS component ' . + '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i' => 'Please do not initialize JS component ' . 'in php. Do it in template.', ]; @@ -54,6 +54,8 @@ public function process(File $phpcsFile, $stackPtr) } /** + * Check access to protected and private members of Block + * * @param File $phpcsFile * @param int $stackPtr * @param array $tokens @@ -76,6 +78,8 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token } /** + * Check access to members and methods of Block class through $this + * * @param File $phpcsFile * @param int $stackPtr * @param array $tokens @@ -98,6 +102,8 @@ private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens } /** + * Check use of "text/javascript" type + * * @param File $phpcsFile * @param int $stackPtr */ @@ -115,6 +121,8 @@ private function checkHtml(File $phpcsFile, int $stackPtr): void } /** + * Check of some obsoletes uses in specific files + * * @param File $phpcsFile * @param int $stackPtr */ @@ -132,5 +140,4 @@ private function checkHtmlSpecificFiles(File $phpcsFile, int $stackPtr): void } } } - } From a43949d3017309575d67e55dc6ce97d3267490c0 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Tue, 5 Oct 2021 15:29:54 +0200 Subject: [PATCH 4/5] AC-681: Create phpcs static check for PhtmlTemplateTest --- Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php index 431b6f00..2b730768 100644 --- a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php +++ b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php @@ -55,7 +55,7 @@ public function process(File $phpcsFile, $stackPtr) /** * Check access to protected and private members of Block - * + * * @param File $phpcsFile * @param int $stackPtr * @param array $tokens @@ -79,7 +79,7 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token /** * Check access to members and methods of Block class through $this - * + * * @param File $phpcsFile * @param int $stackPtr * @param array $tokens @@ -103,7 +103,7 @@ private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens /** * Check use of "text/javascript" type - * + * * @param File $phpcsFile * @param int $stackPtr */ @@ -122,7 +122,7 @@ private function checkHtml(File $phpcsFile, int $stackPtr): void /** * Check of some obsoletes uses in specific files - * + * * @param File $phpcsFile * @param int $stackPtr */ From 70a747c2651115b6d40a0c0995487630c2426dc8 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Tue, 5 Oct 2021 17:58:40 +0200 Subject: [PATCH 5/5] AC-681: Create phpcs static check for PhtmlTemplateTest --- Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php index 2b730768..67d74575 100644 --- a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php +++ b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php @@ -1,4 +1,9 @@