Skip to content

Commit 2c19804

Browse files
committed
Auto-fix ...PhtmlTemplate.TextJavascriptTypeFound
1 parent aec6744 commit 2c19804

File tree

4 files changed

+131
-5
lines changed

4 files changed

+131
-5
lines changed

Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PhtmlTemplateSniff implements Sniff
1414
{
1515
private const WARNING_CODE_TEXT_JAVASCRIPT = 'TextJavascriptTypeFound';
1616
private const WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS = 'ProtectedPrivateBlockAccess';
17-
17+
1818
/**
1919
* @inheritdoc
2020
*/
@@ -40,7 +40,7 @@ public function process(File $phpcsFile, $stackPtr)
4040
$this->checkHtml($phpcsFile, $stackPtr);
4141
}
4242
}
43-
43+
4444
/**
4545
* Check access to protected and private members of Block
4646
*
@@ -74,13 +74,19 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token
7474
private function checkHtml(File $phpcsFile, int $stackPtr): void
7575
{
7676
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
77-
78-
if (preg_match('/type="text\/javascript"/', $content)) {
79-
$phpcsFile->addWarning(
77+
$pattern = '_\s+type="text/javascript"_';
78+
79+
if (preg_match($pattern, $content)) {
80+
$fix = $phpcsFile->addFixableWarning(
8081
'Please do not use "text/javascript" type attribute.',
8182
$stackPtr,
8283
self::WARNING_CODE_TEXT_JAVASCRIPT
8384
);
85+
86+
if ($fix) {
87+
$content = preg_replace($pattern, '', $content);
88+
$phpcsFile->fixer->replaceToken($stackPtr, $content);
89+
}
8490
}
8591
}
8692
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<script type="text/x-magento-init">
8+
</script>
9+
<script>
10+
</script>
11+
<div id="block-testing"
12+
class="block shipping"
13+
data-mage-init='{}'
14+
>
15+
<?php
16+
function _testing()
17+
{
18+
return true;
19+
}
20+
$_productCollection = $block->_getTestFunction();
21+
$block->getTestFunction();
22+
$_something = $this->something();
23+
$block->_getTest();
24+
$block = _testing();
25+
?>
26+
<?php
27+
$block->_getTestAnotherFunction();
28+
?>
29+
30+
<?php $scriptString = <<<script
31+
require([
32+
"jquery",
33+
"jquery/ui",
34+
], function ($, Confirm) {
35+
});
36+
script;
37+
$this->helper();
38+
?>
39+
<script type="jquery/ui">
40+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<script type="text/x-magento-init">
8+
</script>
9+
<script>
10+
</script>
11+
<div id="block-testing"
12+
class="block shipping"
13+
data-mage-init='{}'
14+
>
15+
<?php
16+
function _testing()
17+
{
18+
return true;
19+
}
20+
$_productCollection = $block->_getTestFunction();
21+
$block->getTestFunction();
22+
$_something = $this->something();
23+
$block->_getTest();
24+
$block = _testing();
25+
?>
26+
<?php
27+
$block->_getTestAnotherFunction();
28+
?>
29+
30+
<?php $scriptString = <<<script
31+
require([
32+
"jquery",
33+
"jquery/ui",
34+
], function ($, Confirm) {
35+
});
36+
script;
37+
$this->helper();
38+
?>
39+
<script type="jquery/ui">
40+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<script type="text/x-magento-init">
8+
</script>
9+
<script>
10+
</script>
11+
<div id="block-testing"
12+
class="block shipping"
13+
data-mage-init='{}'
14+
>
15+
<?php
16+
function _testing()
17+
{
18+
return true;
19+
}
20+
$_productCollection = $block->_getTestFunction();
21+
$block->getTestFunction();
22+
$_something = $this->something();
23+
$block->_getTest();
24+
$block = _testing();
25+
?>
26+
<?php
27+
$block->_getTestAnotherFunction();
28+
?>
29+
30+
<?php $scriptString = <<<script
31+
require([
32+
"jquery",
33+
"jquery/ui",
34+
], function ($, Confirm) {
35+
});
36+
script;
37+
$this->helper();
38+
?>
39+
<script type="jquery/ui">
40+
</script>

0 commit comments

Comments
 (0)