Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-fix Magento2.Legacy.PhtmlTemplate.TextJavascriptTypeFound #448

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Magento2/Sniffs/Legacy/PhtmlTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -40,7 +40,7 @@ public function process(File $phpcsFile, $stackPtr)
$this->checkHtml($phpcsFile, $stackPtr);
}
}

/**
* Check access to protected and private members of Block
*
Expand Down Expand Up @@ -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"_';

if (preg_match($pattern, $content)) {
$fix = $phpcsFile->addFixableWarning(
'Please do not use "text/javascript" type attribute.',
$stackPtr,
self::WARNING_CODE_TEXT_JAVASCRIPT
);

if ($fix) {
$content = preg_replace($pattern, '', $content);
$phpcsFile->fixer->replaceToken($stackPtr, $content);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<script type="text/x-magento-init">
</script>
<script>
</script>
<div id="block-testing"
class="block shipping"
data-mage-init='{}'
>
<?php
function _testing()
{
return true;
}
$_productCollection = $block->_getTestFunction();
$block->getTestFunction();
$_something = $this->something();
$block->_getTest();
$block = _testing();
?>
<?php
$block->_getTestAnotherFunction();
?>

<?php $scriptString = <<<script
require([
"jquery",
"jquery/ui",
], function ($, Confirm) {
});
script;
$this->helper();
?>
<script type="jquery/ui">
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<script type="text/x-magento-init">
</script>
<script>
</script>
<div id="block-testing"
class="block shipping"
data-mage-init='{}'
>
<?php
function _testing()
{
return true;
}
$_productCollection = $block->_getTestFunction();
$block->getTestFunction();
$_something = $this->something();
$block->_getTest();
$block = _testing();
?>
<?php
$block->_getTestAnotherFunction();
?>

<?php $scriptString = <<<script
require([
"jquery",
"jquery/ui",
], function ($, Confirm) {
});
script;
$this->helper();
?>
<script type="jquery/ui">
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<script type="text/x-magento-init">
</script>
<script>
</script>
<div id="block-testing"
class="block shipping"
data-mage-init='{}'
>
<?php
function _testing()
{
return true;
}
$_productCollection = $block->_getTestFunction();
$block->getTestFunction();
$_something = $this->something();
$block->_getTest();
$block = _testing();
?>
<?php
$block->_getTestAnotherFunction();
?>

<?php $scriptString = <<<script
require([
"jquery",
"jquery/ui",
], function ($, Confirm) {
});
script;
$this->helper();
?>
<script type="jquery/ui">
</script>