diff --git a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php index fa5eb70e..1f897d38 100644 --- a/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php +++ b/Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php @@ -14,7 +14,7 @@ class PhtmlTemplateSniff implements Sniff { private const WARNING_CODE_TEXT_JAVASCRIPT = 'TextJavascriptTypeFound'; private const WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS = 'ProtectedPrivateBlockAccess'; - + /** * @inheritdoc */ @@ -40,7 +40,7 @@ public function process(File $phpcsFile, $stackPtr) $this->checkHtml($phpcsFile, $stackPtr); } } - + /** * Check access to protected and private members of Block * @@ -74,13 +74,19 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token private function checkHtml(File $phpcsFile, int $stackPtr): void { $content = $phpcsFile->getTokensAsString($stackPtr, 1); - - if (preg_match('/type="text\/javascript"/', $content)) { - $phpcsFile->addWarning( + $pattern = '_(]*?)\s+type=(["\'])text/javascript\2_i'; + + if (preg_match($pattern, $content, $matches)) { + $fix = $phpcsFile->addFixableWarning( 'Please do not use "text/javascript" type attribute.', $stackPtr, self::WARNING_CODE_TEXT_JAVASCRIPT ); + + if ($fix) { + $content = preg_replace($pattern, $matches[1], $content); + $phpcsFile->fixer->replaceToken($stackPtr, $content); + } } } } diff --git a/Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php b/Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php index 129229a3..ecb54dc4 100644 --- a/Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php +++ b/Magento2/Tests/Legacy/PhtmlTemplateUnitTest.php @@ -37,7 +37,7 @@ protected function getTestFiles($testFileBase): array // Put them in order. sort($testFiles); - + return $testFiles; } @@ -54,22 +54,20 @@ public function getErrorList() */ public function getWarningList($testFile = '') { - if ($testFile === 'PhtmlTemplateUnitTest.1.phtml' || $testFile === 'PhtmlTemplateUnitTest.2.phtml') { - return [ - 9 => 1, - 20 => 1, - 23 => 1, - 27 => 1 - ]; - } - if ($testFile === 'PhtmlTemplateUnitTest.3.phtml') { - return [ - 9 => 1, - 20 => 1, - 23 => 1, - 27 => 1, - ]; - } - return []; + return [ + 9 => 1, + 20 => 1, + 23 => 1, + 27 => 1, + 44 => 1, + 45 => 1, + 46 => 1, + 47 => 1, + 48 => 1, + 49 => 1, + 50 => 1, + 51 => 1, + 52 => 1, + ]; } } 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 772783c9..bba83c89 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,3 +38,18 @@ $this->helper(); ?> + +

This sniff should remove the any type="text/javascript" attributes from <script> tags, but not from elsewhere in the document.

+ + + + + + + + + + + diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml.fixed b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml.fixed new file mode 100644 index 00000000..272b1668 --- /dev/null +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/adminhtml/templates/PhtmlTemplateUnitTest.3.phtml.fixed @@ -0,0 +1,55 @@ + + + +
+_getTestFunction(); +$block->getTestFunction(); +$_something = $this->something(); +$block->_getTest(); +$block = _testing(); +?> +_getTestAnotherFunction(); +?> + +helper(); +?> + + +

This sniff should remove the any type="text/javascript" attributes from <script> tags, but not from elsewhere in the document.

+ + + + + + + + + + + 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 772783c9..bba83c89 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,3 +38,18 @@ $this->helper(); ?> + +

This sniff should remove the any type="text/javascript" attributes from <script> tags, but not from elsewhere in the document.

+ + + + + + + + + + + diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml.fixed b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml.fixed new file mode 100644 index 00000000..272b1668 --- /dev/null +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/base/templates/PhtmlTemplateUnitTest.1.phtml.fixed @@ -0,0 +1,55 @@ + + + +
+_getTestFunction(); +$block->getTestFunction(); +$_something = $this->something(); +$block->_getTest(); +$block = _testing(); +?> +_getTestAnotherFunction(); +?> + +helper(); +?> + + +

This sniff should remove the any type="text/javascript" attributes from <script> tags, but not from elsewhere in the document.

+ + + + + + + + + + + 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 772783c9..bba83c89 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,3 +38,18 @@ $this->helper(); ?> + +

This sniff should remove the any type="text/javascript" attributes from <script> tags, but not from elsewhere in the document.

+ + + + + + + + + + + diff --git a/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml.fixed b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml.fixed new file mode 100644 index 00000000..272b1668 --- /dev/null +++ b/Magento2/Tests/Legacy/_files/PhtmlTemplateUnitTest/view/frontend/templates/PhtmlTemplateUnitTest.2.phtml.fixed @@ -0,0 +1,55 @@ + + + +
+_getTestFunction(); +$block->getTestFunction(); +$_something = $this->something(); +$block->_getTest(); +$block = _testing(); +?> +_getTestAnotherFunction(); +?> + +helper(); +?> + + +

This sniff should remove the any type="text/javascript" attributes from <script> tags, but not from elsewhere in the document.

+ + + + + + + + + + +