You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: wiki/Advanced-Usage.md
+81-51Lines changed: 81 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,12 +22,14 @@
22
22
By default, PHP_CodeSniffer will check any file it finds with a `.inc`, `.php`, `.js` or `.css` extension, although not all standards will actually check all these file types. Sometimes, this means that PHP_CodeSniffer is not checking enough of your files. Sometimes, the opposite is true. PHP_CodeSniffer allows you to specify a list of valid file extensions using the `--extensions` command line argument. Extensions are separated by commas.
23
23
24
24
To only check .php files:
25
-
26
-
$ phpcs --extensions=php /path/to/code
25
+
```bash
26
+
$ phpcs --extensions=php /path/to/code
27
+
```
27
28
28
29
To check .php, .inc and .lib files:
29
-
30
-
$ phpcs --extensions=php,inc,lib /path/to/code
30
+
```bash
31
+
$ phpcs --extensions=php,inc,lib /path/to/code
32
+
```
31
33
32
34
<palign="right"><ahref="#table-of-contents">back to top</a></p>
33
35
@@ -36,8 +38,9 @@ To check .php, .inc and .lib files:
36
38
Sometimes you want PHP_CodeSniffer to run over a very large number of files, but you want some files and folders to be skipped. The `--ignore` command line argument can be used to tell PHP_CodeSniffer to skip files and folders that match one or more patterns.
37
39
38
40
In the following example, PHP_CodeSniffer will skip all files inside the package's tests and data directories. This is useful if you are checking a package but don't want your test or data files to conform to your coding standard.
39
-
40
-
$ phpcs --ignore=*/tests/*,*/data/* /path/to/code
41
+
```bash
42
+
$ phpcs --ignore=*/tests/*,*/data/* /path/to/code
43
+
```
41
44
42
45
> [!IMPORTANT]
43
46
> The ignore patterns are treated as regular expressions. If you do specify a regular expression, be aware that `*` is converted to `.*` for the convenience in simple patterns, like those used in the example above. So use `*` anywhere you would normally use `.*`. Also ensure you escape any `.` characters that you want treated as a literal dot, such as when checking file extensions. So if you are checking for `.inc` in your ignore pattern, use `\.inc` instead.
@@ -178,12 +181,14 @@ By default, PHP_CodeSniffer will check your code using all sniffs in the specifi
178
181
> All sniffs specified on the command line must be used in the coding standard you are using to check your files.
179
182
180
183
The following example will only run two sniffs over the code instead of all sniffs in the PEAR standard:
> If you use both the `--sniffs` and `--exclude` command line arguments together, the `--exclude` list will be ignored.
@@ -195,14 +200,16 @@ The following example will run all sniffs in the PEAR standard except for the tw
195
200
By default, PHP_CodeSniffer assigns a severity of 5 to all errors and warnings. Standards may change the severity of some messages so they are hidden by default or even so that they are raised to indicate greater importance. PHP_CodeSniffer allows you to decide what the minimum severity level must be to show a message in its report using the `--severity` command line argument.
196
201
197
202
To hide errors and warnings with a severity less than 3:
198
-
199
-
$ phpcs --severity=3 /path/to/code
203
+
```bash
204
+
$ phpcs --severity=3 /path/to/code
205
+
```
200
206
201
207
You can specify different values for errors and warnings using the `--error-severity` and `--warning-severity` command line arguments.
202
208
203
209
To show all errors, but only warnings with a severity of 8 or more:
Setting the severity of warnings to `0` is the same as using the `-n` command line argument. If you set the severity of errors to `0` PHP_CodeSniffer will not show any errors, which may be useful if you just want to show warnings.
208
215
@@ -215,8 +222,9 @@ This feature is particularly useful during manual code reviews. During normal de
215
222
Most of the sniffs written for PHP_CodeSniffer do not support the usage of tabs for indentation and alignment. You can write your own sniffs that check for tabs instead of spaces, but you can also get PHP_CodeSniffer to convert your tabs into spaces before a file is checked. This allows you to use the existing space-based sniffs on your tab-based files.
216
223
217
224
In the following example, PHP_CodeSniffer will replace all tabs in the files being checked with between 1 and 4 spaces, depending on the column the tab indents to.
218
-
219
-
$ phpcs --tab-width=4 /path/to/code
225
+
```bash
226
+
$ phpcs --tab-width=4 /path/to/code
227
+
```
220
228
221
229
> [!NOTE]
222
230
> The [included sniff](https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php) that enforces space indentation will still generate errors even if you have replaced tabs with spaces using the `--tab-width` setting. This sniff looks at the unmodified version of the code to check line indentation and so must be disabled in a [[custom ruleset.xml file|Annotated ruleset]] if you want to use tab indentation.
@@ -226,16 +234,18 @@ In the following example, PHP_CodeSniffer will replace all tabs in the files bei
226
234
227
235
## Specifying an Encoding
228
236
By default, PHP_CodeSniffer will treat all source files as if they use UTF-8 encoding. If you need your source files to be processed using a specific encoding, you can specify the encoding using the `--encoding` command line argument.
229
-
230
-
$ phpcs --encoding=windows-1251 /path/to/code
237
+
```bash
238
+
$ phpcs --encoding=windows-1251 /path/to/code
239
+
```
231
240
232
241
<palign="right"><ahref="#table-of-contents">back to top</a></p>
233
242
234
243
235
244
## Using a Bootstrap File
236
245
PHP_CodeSniffer can optionally include one or more custom bootstrap files before beginning the run. Bootstrap files are included after command line arguments and rulesets have been parsed, and right before files begin to process. These custom files may be used to perform such taks as manipulating the internal settings of PHP_CodeSniffer that are not exposed through command line arguments. Multiple bootstrap files are seperated by commas.
<palign="right"><ahref="#table-of-contents">back to top</a></p>
241
251
@@ -257,11 +267,15 @@ The `phpcs.xml` file has exactly the same format as a normal [[ruleset.xml file|
257
267
## Specifying php.ini Settings
258
268
PHP_CodeSniffer allows you to set temporary php.ini settings during a run using the `-d` command line argument. The name of the php.ini setting must be specified on the command line, but the value is optional. If no value is set, the php.ini setting will be given a value of TRUE.
<palign="right"><ahref="#table-of-contents">back to top</a></p>
267
281
@@ -271,11 +285,15 @@ PHP_CodeSniffer has some configuration options that can be set. Individual codin
271
285
272
286
To set a configuration option, use the `--config-set` command line argument.
273
287
274
-
$ phpcs --config-set <option> <value>
288
+
```bash
289
+
$ phpcs --config-set <option><value>
290
+
```
275
291
276
292
Configuration options are written to a global configuration file. If you want to set them for a single run only, use the `--runtime-set` command line argument.
> Not all configuration options can be set using the `--runtime-set` command line argument. Configuration options that provide defaults for command line arguments, such as the default standard or report type, can not be used with `--runtime-set`. To set these values for a single run only, use the dedicated CLI arguments that PHP_CodeSniffer provides. The [[Configuration Options|Configuration Options]] list provides an alternative CLI argument for each configuration option not supported by `--runtime-set`.
@@ -288,20 +306,24 @@ PHP_CodeSniffer allows you to delete any configuration option, reverting it to i
288
306
289
307
To delete a configuration option, use the `--config-delete` command line argument.
290
308
291
-
$ phpcs --config-delete <option>
309
+
```bash
310
+
$ phpcs --config-delete <option>
311
+
```
292
312
293
313
<palign="right"><ahref="#table-of-contents">back to top</a></p>
294
314
295
315
296
316
## Viewing Configuration Options
297
317
To view the currently set configuration options, use the `--config-show` command line argument.
298
318
299
-
$ phpcs --config-show
300
-
Array
301
-
(
302
-
[default_standard] => PEAR
303
-
[zend_ca_path] => /path/to/ZendCodeAnalyzer
304
-
)
319
+
```bash
320
+
$ phpcs --config-show
321
+
Array
322
+
(
323
+
[default_standard] => PEAR
324
+
[zend_ca_path] => /path/to/ZendCodeAnalyzer
325
+
)
326
+
```
305
327
306
328
<palign="right"><ahref="#table-of-contents">back to top</a></p>
307
329
@@ -316,14 +338,16 @@ The output of the PHP_CodeSniffer tokeniser shows the step-by-step creation of t
316
338
### The Scope Map
317
339
The scope map is best explained with an example. For the following file:
318
340
319
-
<?php
320
-
if ($condition) {
321
-
echo 'Condition was true';
322
-
}
323
-
?>
341
+
```php
342
+
<?php
343
+
if ($condition) {
344
+
echo 'Condition was true';
345
+
}
346
+
?>
347
+
```
324
348
325
349
The scope map output is:
326
-
350
+
```text
327
351
*** START SCOPE MAP ***
328
352
Start scope map at 1: T_IF => if
329
353
Process token 2 []: T_WHITESPACE =>
@@ -342,7 +366,8 @@ The scope map output is:
342
366
Process token 15 [opener:7;]: T_CLOSE_CURLY_BRACKET => }
343
367
=> Found scope closer for 1 (T_IF)
344
368
*** END SCOPE MAP ***
345
-
369
+
```
370
+
346
371
The scope map output above shows the following pieces of information about the file:
347
372
* A scope token `if` was found at token 1 (note that token 0 is the open PHP tag).
348
373
* The opener for the if statement, the open curly brace, was found at token 7.
@@ -354,14 +379,16 @@ The scope map output is most useful when debugging PHP_CodeSniffer's scope map,
354
379
### The Level Map
355
380
The level map is best explained with an example. For the following file:
356
381
357
-
<?php
358
-
if ($condition) {
359
-
echo 'Condition was true';
360
-
}
361
-
?>
382
+
```php
383
+
<?php
384
+
if ($condition) {
385
+
echo 'Condition was true';
386
+
}
387
+
?>
388
+
```
362
389
363
390
The level map output is:
364
-
391
+
```text
365
392
*** START LEVEL MAP ***
366
393
Process token 0 on line 1 [lvl:0;]: T_OPEN_TAG => <?php\n
367
394
Process token 1 on line 2 [lvl:0;]: T_IF => if
@@ -388,8 +415,8 @@ The level map output is:
388
415
Process token 16 on line 4 [lvl:0;]: T_WHITESPACE => \n
389
416
Process token 17 on line 5 [lvl:0;]: T_CLOSE_TAG => ?>\n
390
417
*** END LEVEL MAP ***
418
+
```
391
419
392
-
393
420
The level map output above shows the following pieces of information about the file:
394
421
* A scope opener, an open curly brace, was found at token 7 and opened the scope for an if statement, defined at token 1.
395
422
* Tokens 8 - 15 are all included in the scope set by the scope opener at token 7, the open curly brace. All these tokens are at level 1, indicating that they are enclosed in 1 scope condition, and all these tokens are enclosed in a single condition; an if statement.
@@ -406,14 +433,16 @@ PHP_CodeSniffer contains multiple verbosity levels. Level 3 (indicated by the co
406
433
407
434
The token processing output is best explained with an example. For the following file:
408
435
409
-
<?php
410
-
if ($condition) {
411
-
echo 'Condition was true';
412
-
}
413
-
?>
436
+
```php
437
+
<?php
438
+
if ($condition) {
439
+
echo 'Condition was true';
440
+
}
441
+
?>
442
+
```
414
443
415
444
The token processing output is:
416
-
445
+
```text
417
446
*** START TOKEN PROCESSING ***
418
447
Process token 0: T_OPEN_TAG => <?php\n
419
448
Processing PEAR_Sniffs_Commenting_FileCommentSniff... DONE in 0 seconds
@@ -451,6 +480,7 @@ The token processing output is:
451
480
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
452
481
Process token 17: T_CLOSE_TAG => ?>\n
453
482
*** END TOKEN PROCESSING ***
483
+
```
454
484
455
485
Every token processed is shown, along with its ID, type and contents. For each token, all sniffs that were executed on the token are displayed, along with the running time.
0 commit comments