Skip to content

Autofix for ThisInTemplate sniff #369

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

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 13 additions & 35 deletions Magento2/Sniffs/Templates/ThisInTemplateSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,8 @@
*/
class ThisInTemplateSniff implements Sniff
{
/**
* Warning violation code.
*
* @var string
*/
protected $warningCodeFoundHelper = 'FoundHelper';

/**
* String representation of warning.
*
* @var string
*/
protected $warningMessageFoundHelper = 'The use of helpers in templates is discouraged. Use ViewModel instead.';

/**
* Warning violation code.
*
* @var string
*/
protected $warningCodeFoundThis = 'FoundThis';

/**
* String representation of warning.
*
* @var string
*/
protected $warningMessageFoundThis = 'The use of $this in templates is deprecated. Use $block instead.';
private const MESSAGE_THIS = 'The use of $this in templates is deprecated. Use $block instead.';
private const MESSAGE_HELPER = 'The use of helpers in templates is discouraged. Use ViewModel instead.';

/**
* @inheritdoc
Expand All @@ -54,14 +29,17 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] === '$this') {
$position = $phpcsFile->findNext(T_STRING, $stackPtr, null, false, 'helper', true);
if ($position !== false) {
$phpcsFile->addWarning($this->warningMessageFoundHelper, $position, $this->warningCodeFoundHelper);
} else {
$phpcsFile->addWarning($this->warningMessageFoundThis, $stackPtr, $this->warningCodeFoundThis);
}
if ($phpcsFile->getTokensAsString($stackPtr, 1) !== '$this') {
return;
}
$isHelperCall = $phpcsFile->findNext(T_STRING, $stackPtr, null, false, 'helper', true);
if ($isHelperCall) {
$phpcsFile->addWarning(self::MESSAGE_HELPER, $stackPtr, 'FoundHelper');
}
if ($phpcsFile->addFixableWarning(self::MESSAGE_THIS, $stackPtr, 'FoundThis') === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($stackPtr, '$block');
$phpcsFile->fixer->endChangeset();
}
}
}
5 changes: 5 additions & 0 deletions Magento2/Tests/Templates/ThisInTemplateUnitTest.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
echo $block->escapeHtml($block->getGroupCode());
echo $block->escapeHtml($block->getGroupCode());
$block->foo();
$block->helper();
2 changes: 1 addition & 1 deletion Magento2/Tests/Templates/ThisInTemplateUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getWarningList()
return [
3 => 2,
4 => 1,
5 => 1,
5 => 2,
];
}
}