Completed
Branch BUG/required-message-fields (8f9492)
by
unknown
10:53 queued 20s
created
src/Standards/Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
                 $token['content'],
81 81
                 $nextVar['content'],
82 82
             ];
83
-            $fix     = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
83
+            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
84 84
             if ($fix === true) {
85 85
                 if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
86 86
                     $phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
      *
145 145
      * @return string
146 146
      */
147
-    protected function getSnippet($content, $start='', $length=40)
147
+    protected function getSnippet($content, $start = '', $length = 40)
148 148
     {
149 149
         $startPos = 0;
150 150
 
Please login to merge, or discard this patch.
Indentation   +146 added lines, -146 removed lines patch added patch discarded remove patch
@@ -17,152 +17,152 @@
 block discarded – undo
17 17
 {
18 18
 
19 19
 
20
-    /**
21
-     * Returns an array of tokens this test wants to listen for.
22
-     *
23
-     * @return array
24
-     */
25
-    public function register()
26
-    {
27
-        $targets = [
28
-            T_OPEN_TAG,
29
-            T_OPEN_TAG_WITH_ECHO,
30
-        ];
31
-
32
-        $shortOpenTags = (bool) ini_get('short_open_tag');
33
-        if ($shortOpenTags === false) {
34
-            $targets[] = T_INLINE_HTML;
35
-        }
36
-
37
-        return $targets;
38
-
39
-    }//end register()
40
-
41
-
42
-    /**
43
-     * Processes this test, when one of its tokens is encountered.
44
-     *
45
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
46
-     * @param int                         $stackPtr  The position of the current token
47
-     *                                               in the stack passed in $tokens.
48
-     *
49
-     * @return void
50
-     */
51
-    public function process(File $phpcsFile, $stackPtr)
52
-    {
53
-        $tokens = $phpcsFile->getTokens();
54
-        $token  = $tokens[$stackPtr];
55
-
56
-        if ($token['code'] === T_OPEN_TAG && $token['content'] === '<?') {
57
-            $error = 'Short PHP opening tag used; expected "<?php" but found "%s"';
58
-            $data  = [$token['content']];
59
-            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data);
60
-            if ($fix === true) {
61
-                $correctOpening = '<?php';
62
-                if (isset($tokens[($stackPtr + 1)]) === true && $tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
63
-                    // Avoid creation of invalid open tags like <?phpecho if the original was <?echo .
64
-                    $correctOpening .= ' ';
65
-                }
66
-
67
-                $phpcsFile->fixer->replaceToken($stackPtr, $correctOpening);
68
-            }
69
-
70
-            $phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'yes');
71
-        } else {
72
-            $phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'no');
73
-        }
74
-
75
-        if ($token['code'] === T_OPEN_TAG_WITH_ECHO) {
76
-            $nextVar = $tokens[$phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true)];
77
-            $error   = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
78
-            $data    = [
79
-                $nextVar['content'],
80
-                $token['content'],
81
-                $nextVar['content'],
82
-            ];
83
-            $fix     = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
84
-            if ($fix === true) {
85
-                if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
86
-                    $phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
87
-                } else {
88
-                    $phpcsFile->fixer->replaceToken($stackPtr, '<?php echo');
89
-                }
90
-            }
91
-        }
92
-
93
-        if ($token['code'] === T_INLINE_HTML) {
94
-            $content     = $token['content'];
95
-            $openerFound = strpos($content, '<?');
96
-
97
-            if ($openerFound === false) {
98
-                return;
99
-            }
100
-
101
-            $closerFound = false;
102
-
103
-            // Inspect current token and subsequent inline HTML token to find a close tag.
104
-            for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) {
105
-                if ($tokens[$i]['code'] !== T_INLINE_HTML) {
106
-                    break;
107
-                }
108
-
109
-                $closerFound = strrpos($tokens[$i]['content'], '?>');
110
-                if ($closerFound !== false) {
111
-                    if ($i !== $stackPtr) {
112
-                        break;
113
-                    } else if ($closerFound > $openerFound) {
114
-                        break;
115
-                    } else {
116
-                        $closerFound = false;
117
-                    }
118
-                }
119
-            }
120
-
121
-            if ($closerFound !== false) {
122
-                $error   = 'Possible use of short open tags detected; found: %s';
123
-                $snippet = $this->getSnippet($content, '<?');
124
-                $data    = ['<?'.$snippet];
125
-
126
-                $phpcsFile->addWarning($error, $stackPtr, 'PossibleFound', $data);
127
-
128
-                // Skip forward to the token containing the closer.
129
-                if (($i - 1) > $stackPtr) {
130
-                    return $i;
131
-                }
132
-            }
133
-        }//end if
134
-
135
-    }//end process()
136
-
137
-
138
-    /**
139
-     * Get a snippet from a HTML token.
140
-     *
141
-     * @param string $content The content of the HTML token.
142
-     * @param string $start   Partial string to use as a starting point for the snippet.
143
-     * @param int    $length  The target length of the snippet to get. Defaults to 40.
144
-     *
145
-     * @return string
146
-     */
147
-    protected function getSnippet($content, $start='', $length=40)
148
-    {
149
-        $startPos = 0;
150
-
151
-        if ($start !== '') {
152
-            $startPos = strpos($content, $start);
153
-            if ($startPos !== false) {
154
-                $startPos += strlen($start);
155
-            }
156
-        }
157
-
158
-        $snippet = substr($content, $startPos, $length);
159
-        if ((strlen($content) - $startPos) > $length) {
160
-            $snippet .= '...';
161
-        }
162
-
163
-        return $snippet;
164
-
165
-    }//end getSnippet()
20
+	/**
21
+	 * Returns an array of tokens this test wants to listen for.
22
+	 *
23
+	 * @return array
24
+	 */
25
+	public function register()
26
+	{
27
+		$targets = [
28
+			T_OPEN_TAG,
29
+			T_OPEN_TAG_WITH_ECHO,
30
+		];
31
+
32
+		$shortOpenTags = (bool) ini_get('short_open_tag');
33
+		if ($shortOpenTags === false) {
34
+			$targets[] = T_INLINE_HTML;
35
+		}
36
+
37
+		return $targets;
38
+
39
+	}//end register()
40
+
41
+
42
+	/**
43
+	 * Processes this test, when one of its tokens is encountered.
44
+	 *
45
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
46
+	 * @param int                         $stackPtr  The position of the current token
47
+	 *                                               in the stack passed in $tokens.
48
+	 *
49
+	 * @return void
50
+	 */
51
+	public function process(File $phpcsFile, $stackPtr)
52
+	{
53
+		$tokens = $phpcsFile->getTokens();
54
+		$token  = $tokens[$stackPtr];
55
+
56
+		if ($token['code'] === T_OPEN_TAG && $token['content'] === '<?') {
57
+			$error = 'Short PHP opening tag used; expected "<?php" but found "%s"';
58
+			$data  = [$token['content']];
59
+			$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data);
60
+			if ($fix === true) {
61
+				$correctOpening = '<?php';
62
+				if (isset($tokens[($stackPtr + 1)]) === true && $tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
63
+					// Avoid creation of invalid open tags like <?phpecho if the original was <?echo .
64
+					$correctOpening .= ' ';
65
+				}
66
+
67
+				$phpcsFile->fixer->replaceToken($stackPtr, $correctOpening);
68
+			}
69
+
70
+			$phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'yes');
71
+		} else {
72
+			$phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'no');
73
+		}
74
+
75
+		if ($token['code'] === T_OPEN_TAG_WITH_ECHO) {
76
+			$nextVar = $tokens[$phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true)];
77
+			$error   = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
78
+			$data    = [
79
+				$nextVar['content'],
80
+				$token['content'],
81
+				$nextVar['content'],
82
+			];
83
+			$fix     = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
84
+			if ($fix === true) {
85
+				if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
86
+					$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
87
+				} else {
88
+					$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo');
89
+				}
90
+			}
91
+		}
92
+
93
+		if ($token['code'] === T_INLINE_HTML) {
94
+			$content     = $token['content'];
95
+			$openerFound = strpos($content, '<?');
96
+
97
+			if ($openerFound === false) {
98
+				return;
99
+			}
100
+
101
+			$closerFound = false;
102
+
103
+			// Inspect current token and subsequent inline HTML token to find a close tag.
104
+			for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) {
105
+				if ($tokens[$i]['code'] !== T_INLINE_HTML) {
106
+					break;
107
+				}
108
+
109
+				$closerFound = strrpos($tokens[$i]['content'], '?>');
110
+				if ($closerFound !== false) {
111
+					if ($i !== $stackPtr) {
112
+						break;
113
+					} else if ($closerFound > $openerFound) {
114
+						break;
115
+					} else {
116
+						$closerFound = false;
117
+					}
118
+				}
119
+			}
120
+
121
+			if ($closerFound !== false) {
122
+				$error   = 'Possible use of short open tags detected; found: %s';
123
+				$snippet = $this->getSnippet($content, '<?');
124
+				$data    = ['<?'.$snippet];
125
+
126
+				$phpcsFile->addWarning($error, $stackPtr, 'PossibleFound', $data);
127
+
128
+				// Skip forward to the token containing the closer.
129
+				if (($i - 1) > $stackPtr) {
130
+					return $i;
131
+				}
132
+			}
133
+		}//end if
134
+
135
+	}//end process()
136
+
137
+
138
+	/**
139
+	 * Get a snippet from a HTML token.
140
+	 *
141
+	 * @param string $content The content of the HTML token.
142
+	 * @param string $start   Partial string to use as a starting point for the snippet.
143
+	 * @param int    $length  The target length of the snippet to get. Defaults to 40.
144
+	 *
145
+	 * @return string
146
+	 */
147
+	protected function getSnippet($content, $start='', $length=40)
148
+	{
149
+		$startPos = 0;
150
+
151
+		if ($start !== '') {
152
+			$startPos = strpos($content, $start);
153
+			if ($startPos !== false) {
154
+				$startPos += strlen($start);
155
+			}
156
+		}
157
+
158
+		$snippet = substr($content, $startPos, $length);
159
+		if ((strlen($content) - $startPos) > $length) {
160
+			$snippet .= '...';
161
+		}
162
+
163
+		return $snippet;
164
+
165
+	}//end getSnippet()
166 166
 
167 167
 
168 168
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -171,7 +171,7 @@  discard block
 block discarded – undo
171 171
      *
172 172
      * @return string
173 173
      */
174
-    protected function getSnippet($content, $start='', $length=40)
174
+    protected function getSnippet($content, $start = '', $length = 40)
175 175
     {
176 176
         $startPos = 0;
177 177
 
@@ -227,7 +227,7 @@  discard block
 block discarded – undo
227 227
      *
228 228
      * @return void
229 229
      */
230
-    protected function addChangeset(File $phpcsFile, $tokens, $openTagPointer, $closeTagPointer, $echo=false)
230
+    protected function addChangeset(File $phpcsFile, $tokens, $openTagPointer, $closeTagPointer, $echo = false)
231 231
     {
232 232
         // Build up the open tag replacement and make sure there's always whitespace behind it.
233 233
         $openReplacement = '<?php';
Please login to merge, or discard this patch.
Indentation   +230 added lines, -230 removed lines patch added patch discarded remove patch
@@ -18,236 +18,236 @@
 block discarded – undo
18 18
 class DisallowAlternativePHPTagsSniff implements Sniff
19 19
 {
20 20
 
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
32
-     */
33
-    private $phpVersion = null;
34
-
35
-
36
-    /**
37
-     * Returns an array of tokens this test wants to listen for.
38
-     *
39
-     * @return array
40
-     */
41
-    public function register()
42
-    {
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
-        ];
59
-
60
-    }//end register()
61
-
62
-
63
-    /**
64
-     * Processes this test, when one of its tokens is encountered.
65
-     *
66
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
67
-     * @param int                         $stackPtr  The position of the current token
68
-     *                                               in the stack passed in $tokens.
69
-     *
70
-     * @return void
71
-     */
72
-    public function process(File $phpcsFile, $stackPtr)
73
-    {
74
-        $tokens  = $phpcsFile->getTokens();
75
-        $openTag = $tokens[$stackPtr];
76
-        $content = $openTag['content'];
77
-
78
-        if (trim($content) === '') {
79
-            return;
80
-        }
81
-
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
-        ) {
137
-            $error   = 'Script style opening tag used; expected "<?php" but found "%s"';
138
-            $snippet = $this->getSnippet($content, $match[1]);
139
-            $data    = [$match[1].$snippet];
140
-
141
-            $phpcsFile->addError($error, $stackPtr, 'ScriptOpenTagFound', $data);
142
-            return;
143
-        }
144
-
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];
150
-
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];
156
-
157
-                $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
158
-            }
159
-        }
160
-
161
-    }//end process()
162
-
163
-
164
-    /**
165
-     * Get a snippet from a HTML token.
166
-     *
167
-     * @param string $content The content of the HTML token.
168
-     * @param string $start   Partial string to use as a starting point for the snippet.
169
-     * @param int    $length  The target length of the snippet to get. Defaults to 40.
170
-     *
171
-     * @return string
172
-     */
173
-    protected function getSnippet($content, $start='', $length=40)
174
-    {
175
-        $startPos = 0;
176
-
177
-        if ($start !== '') {
178
-            $startPos = strpos($content, $start);
179
-            if ($startPos !== false) {
180
-                $startPos += strlen($start);
181
-            }
182
-        }
183
-
184
-        $snippet = substr($content, $startPos, $length);
185
-        if ((strlen($content) - $startPos) > $length) {
186
-            $snippet .= '...';
187
-        }
188
-
189
-        return $snippet;
190
-
191
-    }//end getSnippet()
192
-
193
-
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()
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
32
+	 */
33
+	private $phpVersion = null;
34
+
35
+
36
+	/**
37
+	 * Returns an array of tokens this test wants to listen for.
38
+	 *
39
+	 * @return array
40
+	 */
41
+	public function register()
42
+	{
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
+		];
59
+
60
+	}//end register()
61
+
62
+
63
+	/**
64
+	 * Processes this test, when one of its tokens is encountered.
65
+	 *
66
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
67
+	 * @param int                         $stackPtr  The position of the current token
68
+	 *                                               in the stack passed in $tokens.
69
+	 *
70
+	 * @return void
71
+	 */
72
+	public function process(File $phpcsFile, $stackPtr)
73
+	{
74
+		$tokens  = $phpcsFile->getTokens();
75
+		$openTag = $tokens[$stackPtr];
76
+		$content = $openTag['content'];
77
+
78
+		if (trim($content) === '') {
79
+			return;
80
+		}
81
+
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
+		) {
137
+			$error   = 'Script style opening tag used; expected "<?php" but found "%s"';
138
+			$snippet = $this->getSnippet($content, $match[1]);
139
+			$data    = [$match[1].$snippet];
140
+
141
+			$phpcsFile->addError($error, $stackPtr, 'ScriptOpenTagFound', $data);
142
+			return;
143
+		}
144
+
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];
150
+
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];
156
+
157
+				$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
158
+			}
159
+		}
160
+
161
+	}//end process()
162
+
163
+
164
+	/**
165
+	 * Get a snippet from a HTML token.
166
+	 *
167
+	 * @param string $content The content of the HTML token.
168
+	 * @param string $start   Partial string to use as a starting point for the snippet.
169
+	 * @param int    $length  The target length of the snippet to get. Defaults to 40.
170
+	 *
171
+	 * @return string
172
+	 */
173
+	protected function getSnippet($content, $start='', $length=40)
174
+	{
175
+		$startPos = 0;
176
+
177
+		if ($start !== '') {
178
+			$startPos = strpos($content, $start);
179
+			if ($startPos !== false) {
180
+				$startPos += strlen($start);
181
+			}
182
+		}
183
+
184
+		$snippet = substr($content, $startPos, $length);
185
+		if ((strlen($content) - $startPos) > $length) {
186
+			$snippet .= '...';
187
+		}
188
+
189
+		return $snippet;
190
+
191
+	}//end getSnippet()
192
+
193
+
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 251
 
252 252
 
253 253
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -16,63 +16,63 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * Registers the tokens that this sniff wants to listen for.
21
-     *
22
-     * @return int[]
23
-     */
24
-    public function register()
25
-    {
26
-        return [T_ARRAY];
27
-
28
-    }//end register()
29
-
30
-
31
-    /**
32
-     * Processes this test, when one of its tokens is encountered.
33
-     *
34
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
-     * @param int                         $stackPtr  The position of the current token
36
-     *                                               in the stack passed in $tokens.
37
-     *
38
-     * @return void
39
-     */
40
-    public function process(File $phpcsFile, $stackPtr)
41
-    {
42
-        $tokens = $phpcsFile->getTokens();
43
-
44
-        $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
45
-
46
-        $error = 'Short array syntax must be used to define arrays';
47
-
48
-        if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
49
-            || isset($tokens[$stackPtr]['parenthesis_closer']) === false
50
-        ) {
51
-            // Live coding/parse error, just show the error, don't try and fix it.
52
-            $phpcsFile->addError($error, $stackPtr, 'Found');
53
-            return;
54
-        }
55
-
56
-        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
57
-
58
-        if ($fix === true) {
59
-            $opener = $tokens[$stackPtr]['parenthesis_opener'];
60
-            $closer = $tokens[$stackPtr]['parenthesis_closer'];
61
-
62
-            $phpcsFile->fixer->beginChangeset();
63
-
64
-            if ($opener === null) {
65
-                $phpcsFile->fixer->replaceToken($stackPtr, '[]');
66
-            } else {
67
-                $phpcsFile->fixer->replaceToken($stackPtr, '');
68
-                $phpcsFile->fixer->replaceToken($opener, '[');
69
-                $phpcsFile->fixer->replaceToken($closer, ']');
70
-            }
71
-
72
-            $phpcsFile->fixer->endChangeset();
73
-        }
74
-
75
-    }//end process()
19
+	/**
20
+	 * Registers the tokens that this sniff wants to listen for.
21
+	 *
22
+	 * @return int[]
23
+	 */
24
+	public function register()
25
+	{
26
+		return [T_ARRAY];
27
+
28
+	}//end register()
29
+
30
+
31
+	/**
32
+	 * Processes this test, when one of its tokens is encountered.
33
+	 *
34
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
+	 * @param int                         $stackPtr  The position of the current token
36
+	 *                                               in the stack passed in $tokens.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function process(File $phpcsFile, $stackPtr)
41
+	{
42
+		$tokens = $phpcsFile->getTokens();
43
+
44
+		$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
45
+
46
+		$error = 'Short array syntax must be used to define arrays';
47
+
48
+		if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
49
+			|| isset($tokens[$stackPtr]['parenthesis_closer']) === false
50
+		) {
51
+			// Live coding/parse error, just show the error, don't try and fix it.
52
+			$phpcsFile->addError($error, $stackPtr, 'Found');
53
+			return;
54
+		}
55
+
56
+		$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
57
+
58
+		if ($fix === true) {
59
+			$opener = $tokens[$stackPtr]['parenthesis_opener'];
60
+			$closer = $tokens[$stackPtr]['parenthesis_closer'];
61
+
62
+			$phpcsFile->fixer->beginChangeset();
63
+
64
+			if ($opener === null) {
65
+				$phpcsFile->fixer->replaceToken($stackPtr, '[]');
66
+			} else {
67
+				$phpcsFile->fixer->replaceToken($stackPtr, '');
68
+				$phpcsFile->fixer->replaceToken($opener, '[');
69
+				$phpcsFile->fixer->replaceToken($closer, ']');
70
+			}
71
+
72
+			$phpcsFile->fixer->endChangeset();
73
+		}
74
+
75
+	}//end process()
76 76
 
77 77
 
78 78
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/Arrays/DisallowShortArraySyntaxSniff.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -16,46 +16,46 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * Registers the tokens that this sniff wants to listen for.
21
-     *
22
-     * @return int[]
23
-     */
24
-    public function register()
25
-    {
26
-        return [T_OPEN_SHORT_ARRAY];
27
-
28
-    }//end register()
29
-
30
-
31
-    /**
32
-     * Processes this test, when one of its tokens is encountered.
33
-     *
34
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
-     * @param int                         $stackPtr  The position of the current token
36
-     *                                               in the stack passed in $tokens.
37
-     *
38
-     * @return void
39
-     */
40
-    public function process(File $phpcsFile, $stackPtr)
41
-    {
42
-        $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes');
43
-
44
-        $error = 'Short array syntax is not allowed';
45
-        $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
46
-
47
-        if ($fix === true) {
48
-            $tokens = $phpcsFile->getTokens();
49
-            $opener = $tokens[$stackPtr]['bracket_opener'];
50
-            $closer = $tokens[$stackPtr]['bracket_closer'];
51
-
52
-            $phpcsFile->fixer->beginChangeset();
53
-            $phpcsFile->fixer->replaceToken($opener, 'array(');
54
-            $phpcsFile->fixer->replaceToken($closer, ')');
55
-            $phpcsFile->fixer->endChangeset();
56
-        }
57
-
58
-    }//end process()
19
+	/**
20
+	 * Registers the tokens that this sniff wants to listen for.
21
+	 *
22
+	 * @return int[]
23
+	 */
24
+	public function register()
25
+	{
26
+		return [T_OPEN_SHORT_ARRAY];
27
+
28
+	}//end register()
29
+
30
+
31
+	/**
32
+	 * Processes this test, when one of its tokens is encountered.
33
+	 *
34
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
+	 * @param int                         $stackPtr  The position of the current token
36
+	 *                                               in the stack passed in $tokens.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function process(File $phpcsFile, $stackPtr)
41
+	{
42
+		$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes');
43
+
44
+		$error = 'Short array syntax is not allowed';
45
+		$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
46
+
47
+		if ($fix === true) {
48
+			$tokens = $phpcsFile->getTokens();
49
+			$opener = $tokens[$stackPtr]['bracket_opener'];
50
+			$closer = $tokens[$stackPtr]['bracket_closer'];
51
+
52
+			$phpcsFile->fixer->beginChangeset();
53
+			$phpcsFile->fixer->replaceToken($opener, 'array(');
54
+			$phpcsFile->fixer->replaceToken($closer, ')');
55
+			$phpcsFile->fixer->endChangeset();
56
+		}
57
+
58
+	}//end process()
59 59
 
60 60
 
61 61
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/Formatting/NoSpaceAfterCastSniff.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -17,42 +17,42 @@
 block discarded – undo
17 17
 {
18 18
 
19 19
 
20
-    /**
21
-     * Returns an array of tokens this test wants to listen for.
22
-     *
23
-     * @return array
24
-     */
25
-    public function register()
26
-    {
27
-        return Tokens::$castTokens;
28
-
29
-    }//end register()
30
-
31
-
32
-    /**
33
-     * Processes this test, when one of its tokens is encountered.
34
-     *
35
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36
-     * @param int                         $stackPtr  The position of the current token in
37
-     *                                               the stack passed in $tokens.
38
-     *
39
-     * @return void
40
-     */
41
-    public function process(File $phpcsFile, $stackPtr)
42
-    {
43
-        $tokens = $phpcsFile->getTokens();
44
-
45
-        if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
46
-            return;
47
-        }
48
-
49
-        $error = 'A cast statement must not be followed by a space';
50
-        $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceFound');
51
-        if ($fix === true) {
52
-            $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
53
-        }
54
-
55
-    }//end process()
20
+	/**
21
+	 * Returns an array of tokens this test wants to listen for.
22
+	 *
23
+	 * @return array
24
+	 */
25
+	public function register()
26
+	{
27
+		return Tokens::$castTokens;
28
+
29
+	}//end register()
30
+
31
+
32
+	/**
33
+	 * Processes this test, when one of its tokens is encountered.
34
+	 *
35
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36
+	 * @param int                         $stackPtr  The position of the current token in
37
+	 *                                               the stack passed in $tokens.
38
+	 *
39
+	 * @return void
40
+	 */
41
+	public function process(File $phpcsFile, $stackPtr)
42
+	{
43
+		$tokens = $phpcsFile->getTokens();
44
+
45
+		if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
46
+			return;
47
+		}
48
+
49
+		$error = 'A cast statement must not be followed by a space';
50
+		$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceFound');
51
+		if ($fix === true) {
52
+			$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
53
+		}
54
+
55
+	}//end process()
56 56
 
57 57
 
58 58
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/Strings/UnnecessaryStringConcatSniff.php 1 patch
Indentation   +104 added lines, -104 removed lines patch added patch discarded remove patch
@@ -16,110 +16,110 @@
 block discarded – undo
16 16
 class UnnecessaryStringConcatSniff implements Sniff
17 17
 {
18 18
 
19
-    /**
20
-     * A list of tokenizers this sniff supports.
21
-     *
22
-     * @var array
23
-     */
24
-    public $supportedTokenizers = [
25
-        'PHP',
26
-        'JS',
27
-    ];
28
-
29
-    /**
30
-     * If true, an error will be thrown; otherwise a warning.
31
-     *
32
-     * @var boolean
33
-     */
34
-    public $error = true;
35
-
36
-    /**
37
-     * If true, strings concatenated over multiple lines are allowed.
38
-     *
39
-     * Useful if you break strings over multiple lines to work
40
-     * within a max line length.
41
-     *
42
-     * @var boolean
43
-     */
44
-    public $allowMultiline = false;
45
-
46
-
47
-    /**
48
-     * Returns an array of tokens this test wants to listen for.
49
-     *
50
-     * @return array
51
-     */
52
-    public function register()
53
-    {
54
-        return [
55
-            T_STRING_CONCAT,
56
-            T_PLUS,
57
-        ];
58
-
59
-    }//end register()
60
-
61
-
62
-    /**
63
-     * Processes this sniff, when one of its tokens is encountered.
64
-     *
65
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
66
-     * @param int                         $stackPtr  The position of the current token
67
-     *                                               in the stack passed in $tokens.
68
-     *
69
-     * @return void
70
-     */
71
-    public function process(File $phpcsFile, $stackPtr)
72
-    {
73
-        // Work out which type of file this is for.
74
-        $tokens = $phpcsFile->getTokens();
75
-        if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) {
76
-            if ($phpcsFile->tokenizerType === 'JS') {
77
-                return;
78
-            }
79
-        } else {
80
-            if ($phpcsFile->tokenizerType === 'PHP') {
81
-                return;
82
-            }
83
-        }
84
-
85
-        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
86
-        $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
87
-        if ($prev === false || $next === false) {
88
-            return;
89
-        }
90
-
91
-        if (isset(Tokens::$stringTokens[$tokens[$prev]['code']]) === true
92
-            && isset(Tokens::$stringTokens[$tokens[$next]['code']]) === true
93
-        ) {
94
-            if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) {
95
-                // Before we throw an error for PHP, allow strings to be
96
-                // combined if they would have < and ? next to each other because
97
-                // this trick is sometimes required in PHP strings.
98
-                if ($phpcsFile->tokenizerType === 'PHP') {
99
-                    $prevChar = substr($tokens[$prev]['content'], -2, 1);
100
-                    $nextChar = $tokens[$next]['content'][1];
101
-                    $combined = $prevChar.$nextChar;
102
-                    if ($combined === '?'.'>' || $combined === '<'.'?') {
103
-                        return;
104
-                    }
105
-                }
106
-
107
-                if ($this->allowMultiline === true
108
-                    && $tokens[$prev]['line'] !== $tokens[$next]['line']
109
-                ) {
110
-                    return;
111
-                }
112
-
113
-                $error = 'String concat is not required here; use a single string instead';
114
-                if ($this->error === true) {
115
-                    $phpcsFile->addError($error, $stackPtr, 'Found');
116
-                } else {
117
-                    $phpcsFile->addWarning($error, $stackPtr, 'Found');
118
-                }
119
-            }//end if
120
-        }//end if
121
-
122
-    }//end process()
19
+	/**
20
+	 * A list of tokenizers this sniff supports.
21
+	 *
22
+	 * @var array
23
+	 */
24
+	public $supportedTokenizers = [
25
+		'PHP',
26
+		'JS',
27
+	];
28
+
29
+	/**
30
+	 * If true, an error will be thrown; otherwise a warning.
31
+	 *
32
+	 * @var boolean
33
+	 */
34
+	public $error = true;
35
+
36
+	/**
37
+	 * If true, strings concatenated over multiple lines are allowed.
38
+	 *
39
+	 * Useful if you break strings over multiple lines to work
40
+	 * within a max line length.
41
+	 *
42
+	 * @var boolean
43
+	 */
44
+	public $allowMultiline = false;
45
+
46
+
47
+	/**
48
+	 * Returns an array of tokens this test wants to listen for.
49
+	 *
50
+	 * @return array
51
+	 */
52
+	public function register()
53
+	{
54
+		return [
55
+			T_STRING_CONCAT,
56
+			T_PLUS,
57
+		];
58
+
59
+	}//end register()
60
+
61
+
62
+	/**
63
+	 * Processes this sniff, when one of its tokens is encountered.
64
+	 *
65
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
66
+	 * @param int                         $stackPtr  The position of the current token
67
+	 *                                               in the stack passed in $tokens.
68
+	 *
69
+	 * @return void
70
+	 */
71
+	public function process(File $phpcsFile, $stackPtr)
72
+	{
73
+		// Work out which type of file this is for.
74
+		$tokens = $phpcsFile->getTokens();
75
+		if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) {
76
+			if ($phpcsFile->tokenizerType === 'JS') {
77
+				return;
78
+			}
79
+		} else {
80
+			if ($phpcsFile->tokenizerType === 'PHP') {
81
+				return;
82
+			}
83
+		}
84
+
85
+		$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
86
+		$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
87
+		if ($prev === false || $next === false) {
88
+			return;
89
+		}
90
+
91
+		if (isset(Tokens::$stringTokens[$tokens[$prev]['code']]) === true
92
+			&& isset(Tokens::$stringTokens[$tokens[$next]['code']]) === true
93
+		) {
94
+			if ($tokens[$prev]['content'][0] === $tokens[$next]['content'][0]) {
95
+				// Before we throw an error for PHP, allow strings to be
96
+				// combined if they would have < and ? next to each other because
97
+				// this trick is sometimes required in PHP strings.
98
+				if ($phpcsFile->tokenizerType === 'PHP') {
99
+					$prevChar = substr($tokens[$prev]['content'], -2, 1);
100
+					$nextChar = $tokens[$next]['content'][1];
101
+					$combined = $prevChar.$nextChar;
102
+					if ($combined === '?'.'>' || $combined === '<'.'?') {
103
+						return;
104
+					}
105
+				}
106
+
107
+				if ($this->allowMultiline === true
108
+					&& $tokens[$prev]['line'] !== $tokens[$next]['line']
109
+				) {
110
+					return;
111
+				}
112
+
113
+				$error = 'String concat is not required here; use a single string instead';
114
+				if ($this->error === true) {
115
+					$phpcsFile->addError($error, $stackPtr, 'Found');
116
+				} else {
117
+					$phpcsFile->addWarning($error, $stackPtr, 'Found');
118
+				}
119
+			}//end if
120
+		}//end if
121
+
122
+	}//end process()
123 123
 
124 124
 
125 125
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -16,65 +16,65 @@
 block discarded – undo
16 16
 class ByteOrderMarkSniff implements Sniff
17 17
 {
18 18
 
19
-    /**
20
-     * List of supported BOM definitions.
21
-     *
22
-     * Use encoding names as keys and hex BOM representations as values.
23
-     *
24
-     * @var array
25
-     */
26
-    protected $bomDefinitions = [
27
-        'UTF-8'       => 'efbbbf',
28
-        'UTF-16 (BE)' => 'feff',
29
-        'UTF-16 (LE)' => 'fffe',
30
-    ];
19
+	/**
20
+	 * List of supported BOM definitions.
21
+	 *
22
+	 * Use encoding names as keys and hex BOM representations as values.
23
+	 *
24
+	 * @var array
25
+	 */
26
+	protected $bomDefinitions = [
27
+		'UTF-8'       => 'efbbbf',
28
+		'UTF-16 (BE)' => 'feff',
29
+		'UTF-16 (LE)' => 'fffe',
30
+	];
31 31
 
32 32
 
33
-    /**
34
-     * Returns an array of tokens this test wants to listen for.
35
-     *
36
-     * @return array
37
-     */
38
-    public function register()
39
-    {
40
-        return [T_INLINE_HTML];
33
+	/**
34
+	 * Returns an array of tokens this test wants to listen for.
35
+	 *
36
+	 * @return array
37
+	 */
38
+	public function register()
39
+	{
40
+		return [T_INLINE_HTML];
41 41
 
42
-    }//end register()
42
+	}//end register()
43 43
 
44 44
 
45
-    /**
46
-     * Processes this sniff, when one of its tokens is encountered.
47
-     *
48
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
49
-     * @param int                         $stackPtr  The position of the current token in
50
-     *                                               the stack passed in $tokens.
51
-     *
52
-     * @return void
53
-     */
54
-    public function process(File $phpcsFile, $stackPtr)
55
-    {
56
-        // The BOM will be the very first token in the file.
57
-        if ($stackPtr !== 0) {
58
-            return;
59
-        }
45
+	/**
46
+	 * Processes this sniff, when one of its tokens is encountered.
47
+	 *
48
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
49
+	 * @param int                         $stackPtr  The position of the current token in
50
+	 *                                               the stack passed in $tokens.
51
+	 *
52
+	 * @return void
53
+	 */
54
+	public function process(File $phpcsFile, $stackPtr)
55
+	{
56
+		// The BOM will be the very first token in the file.
57
+		if ($stackPtr !== 0) {
58
+			return;
59
+		}
60 60
 
61
-        $tokens = $phpcsFile->getTokens();
61
+		$tokens = $phpcsFile->getTokens();
62 62
 
63
-        foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
64
-            $bomByteLength = (strlen($expectedBomHex) / 2);
65
-            $htmlBomHex    = bin2hex(substr($tokens[$stackPtr]['content'], 0, $bomByteLength));
66
-            if ($htmlBomHex === $expectedBomHex) {
67
-                $errorData = [$bomName];
68
-                $error     = 'File contains %s byte order mark, which may corrupt your application';
69
-                $phpcsFile->addError($error, $stackPtr, 'Found', $errorData);
70
-                $phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'yes');
71
-                return;
72
-            }
73
-        }
63
+		foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
64
+			$bomByteLength = (strlen($expectedBomHex) / 2);
65
+			$htmlBomHex    = bin2hex(substr($tokens[$stackPtr]['content'], 0, $bomByteLength));
66
+			if ($htmlBomHex === $expectedBomHex) {
67
+				$errorData = [$bomName];
68
+				$error     = 'File contains %s byte order mark, which may corrupt your application';
69
+				$phpcsFile->addError($error, $stackPtr, 'Found', $errorData);
70
+				$phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'yes');
71
+				return;
72
+			}
73
+		}
74 74
 
75
-        $phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'no');
75
+		$phpcsFile->recordMetric($stackPtr, 'Using byte order mark', 'no');
76 76
 
77
-    }//end process()
77
+	}//end process()
78 78
 
79 79
 
80 80
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/Generic/Sniffs/Files/LineEndingsSniff.php 2 patches
Switch Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -95,18 +95,18 @@
 block discarded – undo
95 95
         if ($fix === true) {
96 96
             $tokens = $phpcsFile->getTokens();
97 97
             switch ($this->eolChar) {
98
-            case '\n':
99
-                $eolChar = "\n";
100
-                break;
101
-            case '\r':
102
-                $eolChar = "\r";
103
-                break;
104
-            case '\r\n':
105
-                $eolChar = "\r\n";
106
-                break;
107
-            default:
108
-                $eolChar = $this->eolChar;
109
-                break;
98
+            	case '\n':
99
+                	$eolChar = "\n";
100
+                	break;
101
+            	case '\r':
102
+                	$eolChar = "\r";
103
+                	break;
104
+            	case '\r\n':
105
+                	$eolChar = "\r\n";
106
+                	break;
107
+            	default:
108
+                	$eolChar = $this->eolChar;
109
+                	break;
110 110
             }
111 111
 
112 112
             for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
Please login to merge, or discard this patch.
Indentation   +128 added lines, -128 removed lines patch added patch discarded remove patch
@@ -15,134 +15,134 @@
 block discarded – undo
15 15
 class LineEndingsSniff implements Sniff
16 16
 {
17 17
 
18
-    /**
19
-     * A list of tokenizers this sniff supports.
20
-     *
21
-     * @var array
22
-     */
23
-    public $supportedTokenizers = [
24
-        'PHP',
25
-        'JS',
26
-        'CSS',
27
-    ];
28
-
29
-    /**
30
-     * The valid EOL character.
31
-     *
32
-     * @var string
33
-     */
34
-    public $eolChar = '\n';
35
-
36
-
37
-    /**
38
-     * Returns an array of tokens this test wants to listen for.
39
-     *
40
-     * @return array
41
-     */
42
-    public function register()
43
-    {
44
-        return [
45
-            T_OPEN_TAG,
46
-            T_OPEN_TAG_WITH_ECHO,
47
-        ];
48
-
49
-    }//end register()
50
-
51
-
52
-    /**
53
-     * Processes this sniff, when one of its tokens is encountered.
54
-     *
55
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
56
-     * @param int                         $stackPtr  The position of the current token in
57
-     *                                               the stack passed in $tokens.
58
-     *
59
-     * @return int
60
-     */
61
-    public function process(File $phpcsFile, $stackPtr)
62
-    {
63
-        $found = $phpcsFile->eolChar;
64
-        $found = str_replace("\n", '\n', $found);
65
-        $found = str_replace("\r", '\r', $found);
66
-
67
-        $phpcsFile->recordMetric($stackPtr, 'EOL char', $found);
68
-
69
-        if ($found === $this->eolChar) {
70
-            // Ignore the rest of the file.
71
-            return ($phpcsFile->numTokens + 1);
72
-        }
73
-
74
-        // Check for single line files without an EOL. This is a very special
75
-        // case and the EOL char is set to \n when this happens.
76
-        if ($found === '\n') {
77
-            $tokens    = $phpcsFile->getTokens();
78
-            $lastToken = ($phpcsFile->numTokens - 1);
79
-            if ($tokens[$lastToken]['line'] === 1
80
-                && $tokens[$lastToken]['content'] !== "\n"
81
-            ) {
82
-                return;
83
-            }
84
-        }
85
-
86
-        $error    = 'End of line character is invalid; expected "%s" but found "%s"';
87
-        $expected = $this->eolChar;
88
-        $expected = str_replace("\n", '\n', $expected);
89
-        $expected = str_replace("\r", '\r', $expected);
90
-        $data     = [
91
-            $expected,
92
-            $found,
93
-        ];
94
-
95
-        // Errors are always reported on line 1, no matter where the first PHP tag is.
96
-        $fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data);
97
-
98
-        if ($fix === true) {
99
-            $tokens = $phpcsFile->getTokens();
100
-            switch ($this->eolChar) {
101
-            case '\n':
102
-                $eolChar = "\n";
103
-                break;
104
-            case '\r':
105
-                $eolChar = "\r";
106
-                break;
107
-            case '\r\n':
108
-                $eolChar = "\r\n";
109
-                break;
110
-            default:
111
-                $eolChar = $this->eolChar;
112
-                break;
113
-            }
114
-
115
-            for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
116
-                if (isset($tokens[($i + 1)]) === true
117
-                    && $tokens[($i + 1)]['line'] <= $tokens[$i]['line']
118
-                ) {
119
-                    continue;
120
-                }
121
-
122
-                // Token is the last on a line.
123
-                if (isset($tokens[$i]['orig_content']) === true) {
124
-                    $tokenContent = $tokens[$i]['orig_content'];
125
-                } else {
126
-                    $tokenContent = $tokens[$i]['content'];
127
-                }
128
-
129
-                if ($tokenContent === '') {
130
-                    // Special case for JS/CSS close tag.
131
-                    continue;
132
-                }
133
-
134
-                $newContent  = rtrim($tokenContent, "\r\n");
135
-                $newContent .= $eolChar;
136
-                if ($tokenContent !== $newContent) {
137
-                    $phpcsFile->fixer->replaceToken($i, $newContent);
138
-                }
139
-            }//end for
140
-        }//end if
141
-
142
-        // Ignore the rest of the file.
143
-        return ($phpcsFile->numTokens + 1);
144
-
145
-    }//end process()
18
+	/**
19
+	 * A list of tokenizers this sniff supports.
20
+	 *
21
+	 * @var array
22
+	 */
23
+	public $supportedTokenizers = [
24
+		'PHP',
25
+		'JS',
26
+		'CSS',
27
+	];
28
+
29
+	/**
30
+	 * The valid EOL character.
31
+	 *
32
+	 * @var string
33
+	 */
34
+	public $eolChar = '\n';
35
+
36
+
37
+	/**
38
+	 * Returns an array of tokens this test wants to listen for.
39
+	 *
40
+	 * @return array
41
+	 */
42
+	public function register()
43
+	{
44
+		return [
45
+			T_OPEN_TAG,
46
+			T_OPEN_TAG_WITH_ECHO,
47
+		];
48
+
49
+	}//end register()
50
+
51
+
52
+	/**
53
+	 * Processes this sniff, when one of its tokens is encountered.
54
+	 *
55
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
56
+	 * @param int                         $stackPtr  The position of the current token in
57
+	 *                                               the stack passed in $tokens.
58
+	 *
59
+	 * @return int
60
+	 */
61
+	public function process(File $phpcsFile, $stackPtr)
62
+	{
63
+		$found = $phpcsFile->eolChar;
64
+		$found = str_replace("\n", '\n', $found);
65
+		$found = str_replace("\r", '\r', $found);
66
+
67
+		$phpcsFile->recordMetric($stackPtr, 'EOL char', $found);
68
+
69
+		if ($found === $this->eolChar) {
70
+			// Ignore the rest of the file.
71
+			return ($phpcsFile->numTokens + 1);
72
+		}
73
+
74
+		// Check for single line files without an EOL. This is a very special
75
+		// case and the EOL char is set to \n when this happens.
76
+		if ($found === '\n') {
77
+			$tokens    = $phpcsFile->getTokens();
78
+			$lastToken = ($phpcsFile->numTokens - 1);
79
+			if ($tokens[$lastToken]['line'] === 1
80
+				&& $tokens[$lastToken]['content'] !== "\n"
81
+			) {
82
+				return;
83
+			}
84
+		}
85
+
86
+		$error    = 'End of line character is invalid; expected "%s" but found "%s"';
87
+		$expected = $this->eolChar;
88
+		$expected = str_replace("\n", '\n', $expected);
89
+		$expected = str_replace("\r", '\r', $expected);
90
+		$data     = [
91
+			$expected,
92
+			$found,
93
+		];
94
+
95
+		// Errors are always reported on line 1, no matter where the first PHP tag is.
96
+		$fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data);
97
+
98
+		if ($fix === true) {
99
+			$tokens = $phpcsFile->getTokens();
100
+			switch ($this->eolChar) {
101
+			case '\n':
102
+				$eolChar = "\n";
103
+				break;
104
+			case '\r':
105
+				$eolChar = "\r";
106
+				break;
107
+			case '\r\n':
108
+				$eolChar = "\r\n";
109
+				break;
110
+			default:
111
+				$eolChar = $this->eolChar;
112
+				break;
113
+			}
114
+
115
+			for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
116
+				if (isset($tokens[($i + 1)]) === true
117
+					&& $tokens[($i + 1)]['line'] <= $tokens[$i]['line']
118
+				) {
119
+					continue;
120
+				}
121
+
122
+				// Token is the last on a line.
123
+				if (isset($tokens[$i]['orig_content']) === true) {
124
+					$tokenContent = $tokens[$i]['orig_content'];
125
+				} else {
126
+					$tokenContent = $tokens[$i]['content'];
127
+				}
128
+
129
+				if ($tokenContent === '') {
130
+					// Special case for JS/CSS close tag.
131
+					continue;
132
+				}
133
+
134
+				$newContent  = rtrim($tokenContent, "\r\n");
135
+				$newContent .= $eolChar;
136
+				if ($tokenContent !== $newContent) {
137
+					$phpcsFile->fixer->replaceToken($i, $newContent);
138
+				}
139
+			}//end for
140
+		}//end if
141
+
142
+		// Ignore the rest of the file.
143
+		return ($phpcsFile->numTokens + 1);
144
+
145
+	}//end process()
146 146
 
147 147
 
148 148
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/Files/LowercasedFilenameSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -47,7 +47,7 @@
 block discarded – undo
47 47
         $filename          = basename($filename);
48 48
         $lowercaseFilename = strtolower($filename);
49 49
         if ($filename !== $lowercaseFilename) {
50
-            $data  = [
50
+            $data = [
51 51
                 $filename,
52 52
                 $lowercaseFilename,
53 53
             ];
Please login to merge, or discard this patch.
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -16,55 +16,55 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * Returns an array of tokens this test wants to listen for.
21
-     *
22
-     * @return array
23
-     */
24
-    public function register()
25
-    {
26
-        return [
27
-            T_OPEN_TAG,
28
-            T_OPEN_TAG_WITH_ECHO,
29
-        ];
19
+	/**
20
+	 * Returns an array of tokens this test wants to listen for.
21
+	 *
22
+	 * @return array
23
+	 */
24
+	public function register()
25
+	{
26
+		return [
27
+			T_OPEN_TAG,
28
+			T_OPEN_TAG_WITH_ECHO,
29
+		];
30 30
 
31
-    }//end register()
31
+	}//end register()
32 32
 
33 33
 
34
-    /**
35
-     * Processes this sniff, when one of its tokens is encountered.
36
-     *
37
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38
-     * @param int                         $stackPtr  The position of the current token in
39
-     *                                               the stack passed in $tokens.
40
-     *
41
-     * @return int
42
-     */
43
-    public function process(File $phpcsFile, $stackPtr)
44
-    {
45
-        $filename = $phpcsFile->getFilename();
46
-        if ($filename === 'STDIN') {
47
-            return;
48
-        }
34
+	/**
35
+	 * Processes this sniff, when one of its tokens is encountered.
36
+	 *
37
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38
+	 * @param int                         $stackPtr  The position of the current token in
39
+	 *                                               the stack passed in $tokens.
40
+	 *
41
+	 * @return int
42
+	 */
43
+	public function process(File $phpcsFile, $stackPtr)
44
+	{
45
+		$filename = $phpcsFile->getFilename();
46
+		if ($filename === 'STDIN') {
47
+			return;
48
+		}
49 49
 
50
-        $filename          = basename($filename);
51
-        $lowercaseFilename = strtolower($filename);
52
-        if ($filename !== $lowercaseFilename) {
53
-            $data  = [
54
-                $filename,
55
-                $lowercaseFilename,
56
-            ];
57
-            $error = 'Filename "%s" doesn\'t match the expected filename "%s"';
58
-            $phpcsFile->addError($error, $stackPtr, 'NotFound', $data);
59
-            $phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'no');
60
-        } else {
61
-            $phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'yes');
62
-        }
50
+		$filename          = basename($filename);
51
+		$lowercaseFilename = strtolower($filename);
52
+		if ($filename !== $lowercaseFilename) {
53
+			$data  = [
54
+				$filename,
55
+				$lowercaseFilename,
56
+			];
57
+			$error = 'Filename "%s" doesn\'t match the expected filename "%s"';
58
+			$phpcsFile->addError($error, $stackPtr, 'NotFound', $data);
59
+			$phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'no');
60
+		} else {
61
+			$phpcsFile->recordMetric($stackPtr, 'Lowercase filename', 'yes');
62
+		}
63 63
 
64
-        // Ignore the rest of the file.
65
-        return ($phpcsFile->numTokens + 1);
64
+		// Ignore the rest of the file.
65
+		return ($phpcsFile->numTokens + 1);
66 66
 
67
-    }//end process()
67
+	}//end process()
68 68
 
69 69
 
70 70
 }//end class
Please login to merge, or discard this patch.