13
13
class PhtmlTemplateSniff implements Sniff
14
14
{
15
15
private const WARNING_CODE_TEXT_JAVASCRIPT = 'TextJavascriptTypeFound ' ;
16
- private const WARNING_CODE_THIS_USAGE = 'ThisUsageObsolete ' ;
17
16
private const WARNING_CODE_PROTECTED_PRIVATE_BLOCK_ACCESS = 'ProtectedPrivateBlockAccess ' ;
18
-
19
- private const OBSOLETE_REGEX = [
20
- 'FoundJQueryUI ' => [
21
- 'pattern ' => '/([" \'])jquery\/ui\1/ ' ,
22
- 'message ' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ui widget instead '
23
- ],
24
- 'FoundDataMageInit ' => [
25
- 'pattern ' => '/data-mage-init=(?: \'|")(?!\s*{\s*"[^"]+")/ ' ,
26
- 'message ' => 'Please do not initialize JS component in php. Do it in template '
27
- ],
28
- 'FoundXMagentoInit ' => [
29
- 'pattern ' => '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i ' ,
30
- 'message ' => 'Please do not initialize JS component in php. Do it in template '
31
- ],
32
- ];
33
17
34
18
/**
35
19
* @inheritdoc
@@ -51,18 +35,9 @@ public function process(File $phpcsFile, $stackPtr)
51
35
$ tokens = $ phpcsFile ->getTokens ();
52
36
if ($ tokens [$ stackPtr ]['code ' ] === T_OBJECT_OPERATOR ) {
53
37
$ this ->checkBlockVariable ($ phpcsFile , $ stackPtr , $ tokens );
54
- $ this ->checkThisVariable ($ phpcsFile , $ stackPtr , $ tokens );
55
38
}
56
39
if ($ tokens [$ stackPtr ]['code ' ] === T_INLINE_HTML || $ tokens [$ stackPtr ]['code ' ] === T_HEREDOC ) {
57
40
$ this ->checkHtml ($ phpcsFile , $ stackPtr );
58
-
59
- $ file = $ phpcsFile ->getFilename ();
60
-
61
- if (strpos ($ file , '/view/frontend/templates/ ' ) !== false
62
- || strpos ($ file , '/view/base/templates/ ' ) !== false
63
- ) {
64
- $ this ->checkHtmlSpecificFiles ($ phpcsFile , $ stackPtr );
65
- }
66
41
}
67
42
}
68
43
@@ -90,30 +65,6 @@ private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $token
90
65
}
91
66
}
92
67
93
- /**
94
- * Check access to members and methods of Block class through $this
95
- *
96
- * @param File $phpcsFile
97
- * @param int $stackPtr
98
- * @param array $tokens
99
- */
100
- private function checkThisVariable (File $ phpcsFile , int $ stackPtr , array $ tokens ): void
101
- {
102
- $ varPos = $ phpcsFile ->findPrevious (T_VARIABLE , $ stackPtr - 1 );
103
- if ($ tokens [$ varPos ]['content ' ] !== '$this ' ) {
104
- return ;
105
- }
106
- $ stringPos = $ phpcsFile ->findNext (T_STRING , $ stackPtr + 1 );
107
- if (strpos ($ tokens [$ stringPos ]['content ' ], 'helper ' ) === false ) {
108
- $ phpcsFile ->addWarning (
109
- 'Access to members and methods of Block class through $this is ' .
110
- 'obsolete in phtml templates. Use only $block instead of $this. ' ,
111
- $ stringPos ,
112
- self ::WARNING_CODE_THIS_USAGE
113
- );
114
- }
115
- }
116
-
117
68
/**
118
69
* Check use of "text/javascript" type
119
70
*
@@ -132,25 +83,4 @@ private function checkHtml(File $phpcsFile, int $stackPtr): void
132
83
);
133
84
}
134
85
}
135
-
136
- /**
137
- * Check of some obsoletes uses in specific files
138
- *
139
- * @param File $phpcsFile
140
- * @param int $stackPtr
141
- */
142
- private function checkHtmlSpecificFiles (File $ phpcsFile , int $ stackPtr ): void
143
- {
144
- $ content = $ phpcsFile ->getTokensAsString ($ stackPtr , 1 );
145
-
146
- foreach (self ::OBSOLETE_REGEX as $ code => $ data ) {
147
- if (preg_match ($ data ['pattern ' ], $ content )) {
148
- $ phpcsFile ->addWarning (
149
- $ data ['message ' ],
150
- $ stackPtr ,
151
- $ code
152
- );
153
- }
154
- }
155
- }
156
86
}
0 commit comments