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
+48-2Lines changed: 48 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,9 @@ To check .php, .inc and .lib files:
29
29
30
30
$ phpcs --extensions=php,inc,lib /path/to/code
31
31
32
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
33
+
34
+
32
35
## Ignoring Files and Folders
33
36
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.
34
37
@@ -68,6 +71,9 @@ $xmlPackage->send();
68
71
> [!NOTE]
69
72
> The comment syntax note feature is only available from PHP_CodeSniffer version 3.2.0 onwards.
70
73
74
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
75
+
76
+
71
77
## Ignoring Parts of a File
72
78
Some parts of your code may be unable to conform to your coding standard. For example, you might have to break your standard to integrate with an external library or web service. To stop PHP_CodeSniffer generating errors for this code, you can wrap it in special comments. PHP_CodeSniffer will then hide all errors and warnings that are generated for these lines of code.
73
79
@@ -153,6 +159,9 @@ bar($foo,false);
153
159
> [!NOTE]
154
160
> The comment syntax note feature is only available from PHP_CodeSniffer version 3.2.0 onwards.
155
161
162
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
163
+
164
+
156
165
## Limiting Results to Specific Sniffs
157
166
By default, PHP_CodeSniffer will check your code using all sniffs in the specified standard. Sometimes you may want to find all occurrences of a single error to eliminate it more quickly, or to exclude sniffs to see if they are causing conflicts in your standard. PHP_CodeSniffer allows you to specify a list of sniffs to limit results to using the `--sniffs` command line argument, or a list of sniffs to exclude using the `--exclude` command line argument. Sniff codes are separated by commas.
158
167
@@ -170,6 +179,9 @@ The following example will run all sniffs in the PEAR standard except for the tw
170
179
> [!WARNING]
171
180
> If you use both the `--sniffs` and `--exclude` command line arguments together, the `--exclude` list will be ignored.
172
181
182
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
183
+
184
+
173
185
## Filtering Errors and Warnings Based on Severity
174
186
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.
175
187
@@ -187,6 +199,9 @@ Setting the severity of warnings to `0` is the same as using the `-n` command li
187
199
188
200
This feature is particularly useful during manual code reviews. During normal development, or an automated build, you may want to only check code formatting issues. But while during a code review, you may wish to show less severe errors and warnings that may need manual peer review.
189
201
202
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
203
+
204
+
190
205
## Replacing Tabs with Spaces
191
206
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.
192
207
@@ -197,16 +212,25 @@ In the following example, PHP_CodeSniffer will replace all tabs in the files bei
197
212
> [!NOTE]
198
213
> 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.
199
214
215
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
216
+
217
+
200
218
## Specifying an Encoding
201
219
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.
202
220
203
221
$ phpcs --encoding=windows-1251 /path/to/code
204
222
223
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
224
+
225
+
205
226
## Using a Bootstrap File
206
227
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>
232
+
233
+
210
234
## Using a Default Configuration File
211
235
If you run PHP_CodeSniffer without specifying a coding standard, PHP_CodeSniffer will look in the current directory, and all parent directories, for a file called either `.phpcs.xml`, `phpcs.xml`, `.phpcs.xml.dist`, or `phpcs.xml.dist`. If found, configuration information will be read from this file, including the files to check, the coding standard to use, and any command line arguments to apply.
212
236
@@ -218,6 +242,9 @@ The `phpcs.xml` file has exactly the same format as a normal [[ruleset.xml file|
218
242
> [!NOTE]
219
243
> An example `phpcs.xml` file can be found in the PHP_CodeSniffer repository: [phpcs.xml.dist](https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xml.dist)
220
244
245
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
246
+
247
+
221
248
## Specifying php.ini Settings
222
249
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.
223
250
@@ -227,6 +254,9 @@ You can also specific multiple values:
<palign="right"><ahref="#table-of-contents">back to top</a></p>
258
+
259
+
230
260
## Setting Configuration Options
231
261
PHP_CodeSniffer has some configuration options that can be set. Individual coding standards may also require configuration options to be set before functionality can be used. [[View a full list of configuration options|Configuration Options]].
232
262
@@ -241,14 +271,19 @@ Configuration options are written to a global configuration file. If you want to
241
271
> [!NOTE]
242
272
> 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`.
243
273
274
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
275
+
276
+
244
277
## Deleting Configuration Options
245
278
PHP_CodeSniffer allows you to delete any configuration option, reverting it to its default value. [[View a full list of configuration options|Configuration Options]].
246
279
247
280
To delete a configuration option, use the `--config-delete` command line argument.
248
281
249
282
$ phpcs --config-delete <option>
250
283
251
-
284
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
285
+
286
+
252
287
## Viewing Configuration Options
253
288
To view the currently set configuration options, use the `--config-show` command line argument.
254
289
@@ -259,6 +294,9 @@ To view the currently set configuration options, use the `--config-show` command
259
294
[zend_ca_path] => /path/to/ZendCodeAnalyzer
260
295
)
261
296
297
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
298
+
299
+
262
300
## Printing Verbose Tokeniser Output
263
301
This feature is provided for debugging purposes only. Using this feature will dramatically increase screen output and script running time.
264
302
@@ -349,6 +387,9 @@ The level map output above shows the following pieces of information about the f
349
387
350
388
The level map is most commonly used to determine indentation rules (e.g., a token 4 levels deep requires 16 spaces of indentation) or to determine if a particular token is within a particular scope (e.g., a function keyword is within a class scope, making it a method).
351
389
390
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
391
+
392
+
352
393
## Printing Verbose Token Processing Output
353
394
This feature is provided for debugging purposes only. Using this feature will dramatically increase screen output and script running time.
354
395
@@ -410,5 +451,10 @@ The other interesting piece of information we get from the output above is that
410
451
411
452
For example, the ScopeIndentSniff executes on the if statement's token only, but actually checks the indentation of every line within the if statement. The sniff uses the scope map to find all tokens within the if statement.
412
453
454
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
455
+
456
+
413
457
## Quieting Output
414
-
If a coding standard or configuration file includes settings to print progress or verbose output while running PHP_CodeSniffer, it can make it difficult to use the standard with automated checking tools and build scripts as these typically only expect an error report. If you have this problem, or just want less output, you can quieten the output of PHP_CodeSniffer by using the `-q` command line argument. When using this quiet mode, PHP_CodeSniffer will only print report output, and only if errors or warnings are found. No progress or verbose output will be printed.
458
+
If a coding standard or configuration file includes settings to print progress or verbose output while running PHP_CodeSniffer, it can make it difficult to use the standard with automated checking tools and build scripts as these typically only expect an error report. If you have this problem, or just want less output, you can quieten the output of PHP_CodeSniffer by using the `-q` command line argument. When using this quiet mode, PHP_CodeSniffer will only print report output, and only if errors or warnings are found. No progress or verbose output will be printed.
459
+
460
+
<palign="right"><ahref="#table-of-contents">back to top</a></p>
0 commit comments