-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathThisInTemplateSniff.php
46 lines (40 loc) · 1.29 KB
/
ThisInTemplateSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\Templates;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Detects possible usage of $this variable files.
*/
class ThisInTemplateSniff implements Sniff
{
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
*/
public function register()
{
return [T_VARIABLE];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
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');
} elseif ($phpcsFile->addFixableWarning(self::MESSAGE_THIS, $stackPtr, 'FoundThis') === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($stackPtr, '$block');
$phpcsFile->fixer->endChangeset();
}
}
}