Completed
Branch fix-caching-loader-test (60c3ed)
by
unknown
13:30 queued 04:34
created
src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -30,62 +30,62 @@
 block discarded – undo
30 30
 {
31 31
 
32 32
 
33
-    /**
34
-     * Registers the tokens that this sniff wants to listen for.
35
-     *
36
-     * @return int[]
37
-     */
38
-    public function register()
39
-    {
40
-        return [T_FOR];
41
-
42
-    }//end register()
43
-
44
-
45
-    /**
46
-     * Processes this test, 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
50
-     *                                               in the stack passed in $tokens.
51
-     *
52
-     * @return void
53
-     */
54
-    public function process(File $phpcsFile, $stackPtr)
55
-    {
56
-        $tokens = $phpcsFile->getTokens();
57
-        $token  = $tokens[$stackPtr];
58
-
59
-        // Skip invalid statement.
60
-        if (isset($token['parenthesis_opener']) === false) {
61
-            return;
62
-        }
63
-
64
-        $next = ++$token['parenthesis_opener'];
65
-        $end  = --$token['parenthesis_closer'];
66
-
67
-        $parts = [
68
-            0,
69
-            0,
70
-            0,
71
-        ];
72
-        $index = 0;
73
-
74
-        for (; $next <= $end; ++$next) {
75
-            $code = $tokens[$next]['code'];
76
-            if ($code === T_SEMICOLON) {
77
-                ++$index;
78
-            } else if (isset(Tokens::$emptyTokens[$code]) === false) {
79
-                ++$parts[$index];
80
-            }
81
-        }
82
-
83
-        if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
84
-            $error = 'This FOR loop can be simplified to a WHILE loop';
85
-            $phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
86
-        }
87
-
88
-    }//end process()
33
+	/**
34
+	 * Registers the tokens that this sniff wants to listen for.
35
+	 *
36
+	 * @return int[]
37
+	 */
38
+	public function register()
39
+	{
40
+		return [T_FOR];
41
+
42
+	}//end register()
43
+
44
+
45
+	/**
46
+	 * Processes this test, 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
50
+	 *                                               in the stack passed in $tokens.
51
+	 *
52
+	 * @return void
53
+	 */
54
+	public function process(File $phpcsFile, $stackPtr)
55
+	{
56
+		$tokens = $phpcsFile->getTokens();
57
+		$token  = $tokens[$stackPtr];
58
+
59
+		// Skip invalid statement.
60
+		if (isset($token['parenthesis_opener']) === false) {
61
+			return;
62
+		}
63
+
64
+		$next = ++$token['parenthesis_opener'];
65
+		$end  = --$token['parenthesis_closer'];
66
+
67
+		$parts = [
68
+			0,
69
+			0,
70
+			0,
71
+		];
72
+		$index = 0;
73
+
74
+		for (; $next <= $end; ++$next) {
75
+			$code = $tokens[$next]['code'];
76
+			if ($code === T_SEMICOLON) {
77
+				++$index;
78
+			} else if (isset(Tokens::$emptyTokens[$code]) === false) {
79
+				++$parts[$index];
80
+			}
81
+		}
82
+
83
+		if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
84
+			$error = 'This FOR loop can be simplified to a WHILE loop';
85
+			$phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
86
+		}
87
+
88
+	}//end process()
89 89
 
90 90
 
91 91
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/PHP/DeprecatedFunctionsSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@
 block discarded – undo
58 58
      *
59 59
      * @return void
60 60
      */
61
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
61
+    protected function addError($phpcsFile, $stackPtr, $function, $pattern = null)
62 62
     {
63 63
         $data  = [$function];
64 64
         $error = 'Function %s() has been deprecated';
Please login to merge, or discard this patch.
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -13,61 +13,61 @@
 block discarded – undo
13 13
 class DeprecatedFunctionsSniff extends ForbiddenFunctionsSniff
14 14
 {
15 15
 
16
-    /**
17
-     * A list of forbidden functions with their alternatives.
18
-     *
19
-     * The value is NULL if no alternative exists. IE, the
20
-     * function should just not be used.
21
-     *
22
-     * @var array<string, string|null>
23
-     */
24
-    public $forbiddenFunctions = [];
16
+	/**
17
+	 * A list of forbidden functions with their alternatives.
18
+	 *
19
+	 * The value is NULL if no alternative exists. IE, the
20
+	 * function should just not be used.
21
+	 *
22
+	 * @var array<string, string|null>
23
+	 */
24
+	public $forbiddenFunctions = [];
25 25
 
26 26
 
27
-    /**
28
-     * Constructor.
29
-     *
30
-     * Uses the Reflection API to get a list of deprecated functions.
31
-     */
32
-    public function __construct()
33
-    {
34
-        $functions = get_defined_functions();
27
+	/**
28
+	 * Constructor.
29
+	 *
30
+	 * Uses the Reflection API to get a list of deprecated functions.
31
+	 */
32
+	public function __construct()
33
+	{
34
+		$functions = get_defined_functions();
35 35
 
36
-        foreach ($functions['internal'] as $functionName) {
37
-            $function = new \ReflectionFunction($functionName);
36
+		foreach ($functions['internal'] as $functionName) {
37
+			$function = new \ReflectionFunction($functionName);
38 38
 
39
-            if ($function->isDeprecated() === true) {
40
-                $this->forbiddenFunctions[$functionName] = null;
41
-            }
42
-        }
39
+			if ($function->isDeprecated() === true) {
40
+				$this->forbiddenFunctions[$functionName] = null;
41
+			}
42
+		}
43 43
 
44
-    }//end __construct()
44
+	}//end __construct()
45 45
 
46 46
 
47
-    /**
48
-     * Generates the error or warning for this sniff.
49
-     *
50
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
51
-     * @param int                         $stackPtr  The position of the forbidden function
52
-     *                                               in the token array.
53
-     * @param string                      $function  The name of the forbidden function.
54
-     * @param string                      $pattern   The pattern used for the match.
55
-     *
56
-     * @return void
57
-     */
58
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
59
-    {
60
-        $data  = [$function];
61
-        $error = 'Function %s() has been deprecated';
62
-        $type  = 'Deprecated';
47
+	/**
48
+	 * Generates the error or warning for this sniff.
49
+	 *
50
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
51
+	 * @param int                         $stackPtr  The position of the forbidden function
52
+	 *                                               in the token array.
53
+	 * @param string                      $function  The name of the forbidden function.
54
+	 * @param string                      $pattern   The pattern used for the match.
55
+	 *
56
+	 * @return void
57
+	 */
58
+	protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
59
+	{
60
+		$data  = [$function];
61
+		$error = 'Function %s() has been deprecated';
62
+		$type  = 'Deprecated';
63 63
 
64
-        if ($this->error === true) {
65
-            $phpcsFile->addError($error, $stackPtr, $type, $data);
66
-        } else {
67
-            $phpcsFile->addWarning($error, $stackPtr, $type, $data);
68
-        }
64
+		if ($this->error === true) {
65
+			$phpcsFile->addError($error, $stackPtr, $type, $data);
66
+		} else {
67
+			$phpcsFile->addWarning($error, $stackPtr, $type, $data);
68
+		}
69 69
 
70
-    }//end addError()
70
+	}//end addError()
71 71
 
72 72
 
73 73
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/Generic/Sniffs/PHP/BacktickOperatorSniff.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -16,33 +16,33 @@
 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 [T_BACKTICK];
27
-
28
-    }//end register()
29
-
30
-
31
-    /**
32
-     * Processes this sniff, 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 in
36
-     *                                               the stack passed in $tokens.
37
-     *
38
-     * @return void
39
-     */
40
-    public function process(File $phpcsFile, $stackPtr)
41
-    {
42
-        $error = 'Use of the backtick operator is forbidden';
43
-        $phpcsFile->addError($error, $stackPtr, 'Found');
44
-
45
-    }//end process()
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 [T_BACKTICK];
27
+
28
+	}//end register()
29
+
30
+
31
+	/**
32
+	 * Processes this sniff, 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 in
36
+	 *                                               the stack passed in $tokens.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function process(File $phpcsFile, $stackPtr)
41
+	{
42
+		$error = 'Use of the backtick operator is forbidden';
43
+		$phpcsFile->addError($error, $stackPtr, 'Found');
44
+
45
+	}//end process()
46 46
 
47 47
 
48 48
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/Generic/Sniffs/PHP/DiscourageGotoSniff.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -16,35 +16,35 @@
 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_GOTO,
28
-            T_GOTO_LABEL,
29
-        ];
30
-
31
-    }//end register()
32
-
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 void
42
-     */
43
-    public function process(File $phpcsFile, $stackPtr)
44
-    {
45
-        $phpcsFile->addWarning('Use of the GOTO language construct is discouraged', $stackPtr, 'Found');
46
-
47
-    }//end process()
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_GOTO,
28
+			T_GOTO_LABEL,
29
+		];
30
+
31
+	}//end register()
32
+
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 void
42
+	 */
43
+	public function process(File $phpcsFile, $stackPtr)
44
+	{
45
+		$phpcsFile->addWarning('Use of the GOTO language construct is discouraged', $stackPtr, 'Found');
46
+
47
+	}//end process()
48 48
 
49 49
 
50 50
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -194,7 +194,7 @@
 block discarded – undo
194 194
      *
195 195
      * @return void
196 196
      */
197
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
197
+    protected function addError($phpcsFile, $stackPtr, $function, $pattern = null)
198 198
     {
199 199
         $data  = [$function];
200 200
         $error = 'The use of function %s() is ';
Please login to merge, or discard this patch.
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -18,223 +18,223 @@
 block discarded – undo
18 18
 class ForbiddenFunctionsSniff implements Sniff
19 19
 {
20 20
 
21
-    /**
22
-     * A list of forbidden functions with their alternatives.
23
-     *
24
-     * The value is NULL if no alternative exists. IE, the
25
-     * function should just not be used.
26
-     *
27
-     * @var array<string, string|null>
28
-     */
29
-    public $forbiddenFunctions = [
30
-        'sizeof' => 'count',
31
-        'delete' => 'unset',
32
-    ];
33
-
34
-    /**
35
-     * A cache of forbidden function names, for faster lookups.
36
-     *
37
-     * @var string[]
38
-     */
39
-    protected $forbiddenFunctionNames = [];
40
-
41
-    /**
42
-     * If true, forbidden functions will be considered regular expressions.
43
-     *
44
-     * @var boolean
45
-     */
46
-    protected $patternMatch = false;
47
-
48
-    /**
49
-     * If true, an error will be thrown; otherwise a warning.
50
-     *
51
-     * @var boolean
52
-     */
53
-    public $error = true;
54
-
55
-
56
-    /**
57
-     * Returns an array of tokens this test wants to listen for.
58
-     *
59
-     * @return array
60
-     */
61
-    public function register()
62
-    {
63
-        // Everyone has had a chance to figure out what forbidden functions
64
-        // they want to check for, so now we can cache out the list.
65
-        $this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
66
-
67
-        if ($this->patternMatch === true) {
68
-            foreach ($this->forbiddenFunctionNames as $i => $name) {
69
-                $this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
70
-            }
71
-
72
-            return [T_STRING];
73
-        }
74
-
75
-        // If we are not pattern matching, we need to work out what
76
-        // tokens to listen for.
77
-        $hasHaltCompiler = false;
78
-        $string          = '<?php ';
79
-        foreach ($this->forbiddenFunctionNames as $name) {
80
-            if ($name === '__halt_compiler') {
81
-                $hasHaltCompiler = true;
82
-            } else {
83
-                $string .= $name.'();';
84
-            }
85
-        }
86
-
87
-        if ($hasHaltCompiler === true) {
88
-            $string .= '__halt_compiler();';
89
-        }
90
-
91
-        $register = [];
92
-
93
-        $tokens = token_get_all($string);
94
-        array_shift($tokens);
95
-        foreach ($tokens as $token) {
96
-            if (is_array($token) === true) {
97
-                $register[] = $token[0];
98
-            }
99
-        }
100
-
101
-        $this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
102
-        $this->forbiddenFunctions     = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
103
-
104
-        return array_unique($register);
105
-
106
-    }//end register()
107
-
108
-
109
-    /**
110
-     * Processes this test, when one of its tokens is encountered.
111
-     *
112
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
113
-     * @param int                         $stackPtr  The position of the current token in
114
-     *                                               the stack passed in $tokens.
115
-     *
116
-     * @return void
117
-     */
118
-    public function process(File $phpcsFile, $stackPtr)
119
-    {
120
-        $tokens = $phpcsFile->getTokens();
121
-
122
-        $ignore = [
123
-            T_DOUBLE_COLON             => true,
124
-            T_OBJECT_OPERATOR          => true,
125
-            T_NULLSAFE_OBJECT_OPERATOR => true,
126
-            T_FUNCTION                 => true,
127
-            T_CONST                    => true,
128
-            T_PUBLIC                   => true,
129
-            T_PRIVATE                  => true,
130
-            T_PROTECTED                => true,
131
-            T_AS                       => true,
132
-            T_NEW                      => true,
133
-            T_INSTEADOF                => true,
134
-            T_NS_SEPARATOR             => true,
135
-            T_IMPLEMENTS               => true,
136
-        ];
137
-
138
-        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
139
-
140
-        // If function call is directly preceded by a NS_SEPARATOR it points to the
141
-        // global namespace, so we should still catch it.
142
-        if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
143
-            $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), null, true);
144
-            if ($tokens[$prevToken]['code'] === T_STRING) {
145
-                // Not in the global namespace.
146
-                return;
147
-            }
148
-        }
149
-
150
-        if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
151
-            // Not a call to a PHP function.
152
-            return;
153
-        }
154
-
155
-        $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
156
-        if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
157
-            // Not a call to a PHP function.
158
-            return;
159
-        }
160
-
161
-        if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
162
-            // Not a call to a PHP function.
163
-            return;
164
-        }
165
-
166
-        $function = strtolower($tokens[$stackPtr]['content']);
167
-        $pattern  = null;
168
-
169
-        if ($this->patternMatch === true) {
170
-            $count   = 0;
171
-            $pattern = preg_replace(
172
-                $this->forbiddenFunctionNames,
173
-                $this->forbiddenFunctionNames,
174
-                $function,
175
-                1,
176
-                $count
177
-            );
178
-
179
-            if ($count === 0) {
180
-                return;
181
-            }
182
-
183
-            // Remove the pattern delimiters and modifier.
184
-            $pattern = substr($pattern, 1, -2);
185
-        } else {
186
-            if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
187
-                return;
188
-            }
189
-        }//end if
190
-
191
-        $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
192
-
193
-    }//end process()
194
-
195
-
196
-    /**
197
-     * Generates the error or warning for this sniff.
198
-     *
199
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
200
-     * @param int                         $stackPtr  The position of the forbidden function
201
-     *                                               in the token array.
202
-     * @param string                      $function  The name of the forbidden function.
203
-     * @param string                      $pattern   The pattern used for the match.
204
-     *
205
-     * @return void
206
-     */
207
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
208
-    {
209
-        $data  = [$function];
210
-        $error = 'The use of function %s() is ';
211
-        if ($this->error === true) {
212
-            $type   = 'Found';
213
-            $error .= 'forbidden';
214
-        } else {
215
-            $type   = 'Discouraged';
216
-            $error .= 'discouraged';
217
-        }
218
-
219
-        if ($pattern === null) {
220
-            $pattern = strtolower($function);
221
-        }
222
-
223
-        if ($this->forbiddenFunctions[$pattern] !== null
224
-            && $this->forbiddenFunctions[$pattern] !== 'null'
225
-        ) {
226
-            $type  .= 'WithAlternative';
227
-            $data[] = $this->forbiddenFunctions[$pattern];
228
-            $error .= '; use %s() instead';
229
-        }
230
-
231
-        if ($this->error === true) {
232
-            $phpcsFile->addError($error, $stackPtr, $type, $data);
233
-        } else {
234
-            $phpcsFile->addWarning($error, $stackPtr, $type, $data);
235
-        }
236
-
237
-    }//end addError()
21
+	/**
22
+	 * A list of forbidden functions with their alternatives.
23
+	 *
24
+	 * The value is NULL if no alternative exists. IE, the
25
+	 * function should just not be used.
26
+	 *
27
+	 * @var array<string, string|null>
28
+	 */
29
+	public $forbiddenFunctions = [
30
+		'sizeof' => 'count',
31
+		'delete' => 'unset',
32
+	];
33
+
34
+	/**
35
+	 * A cache of forbidden function names, for faster lookups.
36
+	 *
37
+	 * @var string[]
38
+	 */
39
+	protected $forbiddenFunctionNames = [];
40
+
41
+	/**
42
+	 * If true, forbidden functions will be considered regular expressions.
43
+	 *
44
+	 * @var boolean
45
+	 */
46
+	protected $patternMatch = false;
47
+
48
+	/**
49
+	 * If true, an error will be thrown; otherwise a warning.
50
+	 *
51
+	 * @var boolean
52
+	 */
53
+	public $error = true;
54
+
55
+
56
+	/**
57
+	 * Returns an array of tokens this test wants to listen for.
58
+	 *
59
+	 * @return array
60
+	 */
61
+	public function register()
62
+	{
63
+		// Everyone has had a chance to figure out what forbidden functions
64
+		// they want to check for, so now we can cache out the list.
65
+		$this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
66
+
67
+		if ($this->patternMatch === true) {
68
+			foreach ($this->forbiddenFunctionNames as $i => $name) {
69
+				$this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
70
+			}
71
+
72
+			return [T_STRING];
73
+		}
74
+
75
+		// If we are not pattern matching, we need to work out what
76
+		// tokens to listen for.
77
+		$hasHaltCompiler = false;
78
+		$string          = '<?php ';
79
+		foreach ($this->forbiddenFunctionNames as $name) {
80
+			if ($name === '__halt_compiler') {
81
+				$hasHaltCompiler = true;
82
+			} else {
83
+				$string .= $name.'();';
84
+			}
85
+		}
86
+
87
+		if ($hasHaltCompiler === true) {
88
+			$string .= '__halt_compiler();';
89
+		}
90
+
91
+		$register = [];
92
+
93
+		$tokens = token_get_all($string);
94
+		array_shift($tokens);
95
+		foreach ($tokens as $token) {
96
+			if (is_array($token) === true) {
97
+				$register[] = $token[0];
98
+			}
99
+		}
100
+
101
+		$this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
102
+		$this->forbiddenFunctions     = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
103
+
104
+		return array_unique($register);
105
+
106
+	}//end register()
107
+
108
+
109
+	/**
110
+	 * Processes this test, when one of its tokens is encountered.
111
+	 *
112
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
113
+	 * @param int                         $stackPtr  The position of the current token in
114
+	 *                                               the stack passed in $tokens.
115
+	 *
116
+	 * @return void
117
+	 */
118
+	public function process(File $phpcsFile, $stackPtr)
119
+	{
120
+		$tokens = $phpcsFile->getTokens();
121
+
122
+		$ignore = [
123
+			T_DOUBLE_COLON             => true,
124
+			T_OBJECT_OPERATOR          => true,
125
+			T_NULLSAFE_OBJECT_OPERATOR => true,
126
+			T_FUNCTION                 => true,
127
+			T_CONST                    => true,
128
+			T_PUBLIC                   => true,
129
+			T_PRIVATE                  => true,
130
+			T_PROTECTED                => true,
131
+			T_AS                       => true,
132
+			T_NEW                      => true,
133
+			T_INSTEADOF                => true,
134
+			T_NS_SEPARATOR             => true,
135
+			T_IMPLEMENTS               => true,
136
+		];
137
+
138
+		$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
139
+
140
+		// If function call is directly preceded by a NS_SEPARATOR it points to the
141
+		// global namespace, so we should still catch it.
142
+		if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
143
+			$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), null, true);
144
+			if ($tokens[$prevToken]['code'] === T_STRING) {
145
+				// Not in the global namespace.
146
+				return;
147
+			}
148
+		}
149
+
150
+		if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
151
+			// Not a call to a PHP function.
152
+			return;
153
+		}
154
+
155
+		$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
156
+		if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
157
+			// Not a call to a PHP function.
158
+			return;
159
+		}
160
+
161
+		if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
162
+			// Not a call to a PHP function.
163
+			return;
164
+		}
165
+
166
+		$function = strtolower($tokens[$stackPtr]['content']);
167
+		$pattern  = null;
168
+
169
+		if ($this->patternMatch === true) {
170
+			$count   = 0;
171
+			$pattern = preg_replace(
172
+				$this->forbiddenFunctionNames,
173
+				$this->forbiddenFunctionNames,
174
+				$function,
175
+				1,
176
+				$count
177
+			);
178
+
179
+			if ($count === 0) {
180
+				return;
181
+			}
182
+
183
+			// Remove the pattern delimiters and modifier.
184
+			$pattern = substr($pattern, 1, -2);
185
+		} else {
186
+			if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
187
+				return;
188
+			}
189
+		}//end if
190
+
191
+		$this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
192
+
193
+	}//end process()
194
+
195
+
196
+	/**
197
+	 * Generates the error or warning for this sniff.
198
+	 *
199
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
200
+	 * @param int                         $stackPtr  The position of the forbidden function
201
+	 *                                               in the token array.
202
+	 * @param string                      $function  The name of the forbidden function.
203
+	 * @param string                      $pattern   The pattern used for the match.
204
+	 *
205
+	 * @return void
206
+	 */
207
+	protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
208
+	{
209
+		$data  = [$function];
210
+		$error = 'The use of function %s() is ';
211
+		if ($this->error === true) {
212
+			$type   = 'Found';
213
+			$error .= 'forbidden';
214
+		} else {
215
+			$type   = 'Discouraged';
216
+			$error .= 'discouraged';
217
+		}
218
+
219
+		if ($pattern === null) {
220
+			$pattern = strtolower($function);
221
+		}
222
+
223
+		if ($this->forbiddenFunctions[$pattern] !== null
224
+			&& $this->forbiddenFunctions[$pattern] !== 'null'
225
+		) {
226
+			$type  .= 'WithAlternative';
227
+			$data[] = $this->forbiddenFunctions[$pattern];
228
+			$error .= '; use %s() instead';
229
+		}
230
+
231
+		if ($this->error === true) {
232
+			$phpcsFile->addError($error, $stackPtr, $type, $data);
233
+		} else {
234
+			$phpcsFile->addWarning($error, $stackPtr, $type, $data);
235
+		}
236
+
237
+	}//end addError()
238 238
 
239 239
 
240 240
 }//end class
Please login to merge, or discard this patch.
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.