11
11
12
12
namespace PHP_CodeSniffer \Standards \Generic \Sniffs \PHP ;
13
13
14
- use PHP_CodeSniffer \Config ;
15
14
use PHP_CodeSniffer \Files \File ;
16
15
use PHP_CodeSniffer \Sniffs \Sniff ;
17
16
18
17
class DisallowAlternativePHPTagsSniff implements Sniff
19
18
{
20
19
21
- /**
22
- * Whether ASP tags are enabled or not.
23
- *
24
- * @var boolean
25
- */
26
- private $ aspTags = false ;
27
-
28
- /**
29
- * The current PHP version.
30
- *
31
- * @var integer|string|null
32
- */
33
- private $ phpVersion = null ;
34
-
35
20
36
21
/**
37
22
* Returns an array of tokens this test wants to listen for.
@@ -40,22 +25,7 @@ class DisallowAlternativePHPTagsSniff implements Sniff
40
25
*/
41
26
public function register ()
42
27
{
43
- if ($ this ->phpVersion === null ) {
44
- $ this ->phpVersion = Config::getConfigData ('php_version ' );
45
- if ($ this ->phpVersion === null ) {
46
- $ this ->phpVersion = PHP_VERSION_ID ;
47
- }
48
- }
49
-
50
- if ($ this ->phpVersion < 70000 ) {
51
- $ this ->aspTags = (bool ) ini_get ('asp_tags ' );
52
- }
53
-
54
- return [
55
- T_OPEN_TAG ,
56
- T_OPEN_TAG_WITH_ECHO ,
57
- T_INLINE_HTML ,
58
- ];
28
+ return [T_INLINE_HTML ];
59
29
60
30
}//end register()
61
31
@@ -79,61 +49,8 @@ public function process(File $phpcsFile, $stackPtr)
79
49
return ;
80
50
}
81
51
82
- if ($ openTag ['code ' ] === T_OPEN_TAG ) {
83
- if ($ content === '<% ' ) {
84
- $ error = 'ASP style opening tag used; expected "<?php" but found "%s" ' ;
85
- $ closer = $ this ->findClosingTag ($ phpcsFile , $ tokens , $ stackPtr , '%> ' );
86
- $ errorCode = 'ASPOpenTagFound ' ;
87
- } else if (strpos ($ content , '<script ' ) !== false ) {
88
- $ error = 'Script style opening tag used; expected "<?php" but found "%s" ' ;
89
- $ closer = $ this ->findClosingTag ($ phpcsFile , $ tokens , $ stackPtr , '</script> ' );
90
- $ errorCode = 'ScriptOpenTagFound ' ;
91
- }
92
-
93
- if (isset ($ error , $ closer , $ errorCode ) === true ) {
94
- $ data = [$ content ];
95
-
96
- if ($ closer === false ) {
97
- $ phpcsFile ->addError ($ error , $ stackPtr , $ errorCode , $ data );
98
- } else {
99
- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , $ errorCode , $ data );
100
- if ($ fix === true ) {
101
- $ this ->addChangeset ($ phpcsFile , $ tokens , $ stackPtr , $ closer );
102
- }
103
- }
104
- }
105
-
106
- return ;
107
- }//end if
108
-
109
- if ($ openTag ['code ' ] === T_OPEN_TAG_WITH_ECHO && $ content === '<%= ' ) {
110
- $ error = 'ASP style opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..." ' ;
111
- $ nextVar = $ phpcsFile ->findNext (T_WHITESPACE , ($ stackPtr + 1 ), null , true );
112
- $ snippet = $ this ->getSnippet ($ tokens [$ nextVar ]['content ' ]);
113
- $ data = [
114
- $ snippet ,
115
- $ content ,
116
- $ snippet ,
117
- ];
118
-
119
- $ closer = $ this ->findClosingTag ($ phpcsFile , $ tokens , $ stackPtr , '%> ' );
120
-
121
- if ($ closer === false ) {
122
- $ phpcsFile ->addError ($ error , $ stackPtr , 'ASPShortOpenTagFound ' , $ data );
123
- } else {
124
- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'ASPShortOpenTagFound ' , $ data );
125
- if ($ fix === true ) {
126
- $ this ->addChangeset ($ phpcsFile , $ tokens , $ stackPtr , $ closer , true );
127
- }
128
- }
129
-
130
- return ;
131
- }//end if
132
-
133
- // Account for incorrect script open tags.
134
- if ($ openTag ['code ' ] === T_INLINE_HTML
135
- && preg_match ('`(<script (?:[^>]+)?language=[ \'"]?php[ \'"]?(?:[^>]+)?>)`i ' , $ content , $ match ) === 1
136
- ) {
52
+ // Account for script open tags.
53
+ if (preg_match ('`(<script (?:[^>]+)?language=[ \'"]?php[ \'"]?(?:[^>]+)?>)`i ' , $ content , $ match ) === 1 ) {
137
54
$ error = 'Script style opening tag used; expected "<?php" but found "%s" ' ;
138
55
$ snippet = $ this ->getSnippet ($ content , $ match [1 ]);
139
56
$ data = [$ match [1 ].$ snippet ];
@@ -142,20 +59,19 @@ public function process(File $phpcsFile, $stackPtr)
142
59
return ;
143
60
}
144
61
145
- if ( $ openTag [ ' code ' ] === T_INLINE_HTML && $ this -> aspTags === false ) {
146
- if (strpos ($ content , '<%= ' ) !== false ) {
147
- $ error = 'Possible use of ASP style short opening tags detected; found: %s ' ;
148
- $ snippet = $ this ->getSnippet ($ content , '<%= ' );
149
- $ data = ['<%= ' .$ snippet ];
62
+ // Account for ASP style tags.
63
+ if (strpos ($ content , '<%= ' ) !== false ) {
64
+ $ error = 'Possible use of ASP style short opening tags detected; found: %s ' ;
65
+ $ snippet = $ this ->getSnippet ($ content , '<%= ' );
66
+ $ data = ['<%= ' .$ snippet ];
150
67
151
- $ phpcsFile ->addWarning ($ error , $ stackPtr , 'MaybeASPShortOpenTagFound ' , $ data );
152
- } else if (strpos ($ content , '<% ' ) !== false ) {
153
- $ error = 'Possible use of ASP style opening tags detected; found: %s ' ;
154
- $ snippet = $ this ->getSnippet ($ content , '<% ' );
155
- $ data = ['<% ' .$ snippet ];
68
+ $ phpcsFile ->addWarning ($ error , $ stackPtr , 'MaybeASPShortOpenTagFound ' , $ data );
69
+ } else if (strpos ($ content , '<% ' ) !== false ) {
70
+ $ error = 'Possible use of ASP style opening tags detected; found: %s ' ;
71
+ $ snippet = $ this ->getSnippet ($ content , '<% ' );
72
+ $ data = ['<% ' .$ snippet ];
156
73
157
- $ phpcsFile ->addWarning ($ error , $ stackPtr , 'MaybeASPOpenTagFound ' , $ data );
158
- }
74
+ $ phpcsFile ->addWarning ($ error , $ stackPtr , 'MaybeASPOpenTagFound ' , $ data );
159
75
}
160
76
161
77
}//end process()
@@ -191,63 +107,4 @@ protected function getSnippet($content, $start='', $length=40)
191
107
}//end getSnippet()
192
108
193
109
194
- /**
195
- * Try and find a matching PHP closing tag.
196
- *
197
- * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
198
- * @param array $tokens The token stack.
199
- * @param int $stackPtr The position of the current token
200
- * in the stack passed in $tokens.
201
- * @param string $content The expected content of the closing tag to match the opener.
202
- *
203
- * @return int|false Pointer to the position in the stack for the closing tag or false if not found.
204
- */
205
- protected function findClosingTag (File $ phpcsFile , $ tokens , $ stackPtr , $ content )
206
- {
207
- $ closer = $ phpcsFile ->findNext (T_CLOSE_TAG , ($ stackPtr + 1 ));
208
-
209
- if ($ closer !== false && $ content === trim ($ tokens [$ closer ]['content ' ])) {
210
- return $ closer ;
211
- }
212
-
213
- return false ;
214
-
215
- }//end findClosingTag()
216
-
217
-
218
- /**
219
- * Add a changeset to replace the alternative PHP tags.
220
- *
221
- * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
222
- * @param array $tokens The token stack.
223
- * @param int $openTagPointer Stack pointer to the PHP open tag.
224
- * @param int $closeTagPointer Stack pointer to the PHP close tag.
225
- * @param bool $echo Whether to add 'echo' or not.
226
- *
227
- * @return void
228
- */
229
- protected function addChangeset (File $ phpcsFile , $ tokens , $ openTagPointer , $ closeTagPointer , $ echo =false )
230
- {
231
- // Build up the open tag replacement and make sure there's always whitespace behind it.
232
- $ openReplacement = '<?php ' ;
233
- if ($ echo === true ) {
234
- $ openReplacement .= ' echo ' ;
235
- }
236
-
237
- if ($ tokens [($ openTagPointer + 1 )]['code ' ] !== T_WHITESPACE ) {
238
- $ openReplacement .= ' ' ;
239
- }
240
-
241
- // Make sure we don't remove any line breaks after the closing tag.
242
- $ regex = '` ' .preg_quote (trim ($ tokens [$ closeTagPointer ]['content ' ])).'` ' ;
243
- $ closeReplacement = preg_replace ($ regex , '?> ' , $ tokens [$ closeTagPointer ]['content ' ]);
244
-
245
- $ phpcsFile ->fixer ->beginChangeset ();
246
- $ phpcsFile ->fixer ->replaceToken ($ openTagPointer , $ openReplacement );
247
- $ phpcsFile ->fixer ->replaceToken ($ closeTagPointer , $ closeReplacement );
248
- $ phpcsFile ->fixer ->endChangeset ();
249
-
250
- }//end addChangeset()
251
-
252
-
253
110
}//end class
0 commit comments