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/About-Standards-for-PHP_CodeSniffer.md
-3Lines changed: 0 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,7 +109,6 @@ In that case, you can write your own sniff to enforce that rule.
109
109
> [!IMPORTANT]
110
110
> All sniffs in a standard are automatically included. There is no need to include the sniff(s) in the `ruleset.xml` via a `<rule ref=.../>`.
111
111
112
-
113
112
There is a [Coding Standard Tutorial](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Coding-Standard-Tutorial) available on how to write a sniff.
114
113
115
114
<palign="right"><ahref="#table-of-contents">back to top</a></p>
@@ -156,7 +155,6 @@ This means that - taking the example directory structure above into account - th
156
155
157
156
#### Examples
158
157
159
-
160
158
##### Valid:
161
159
```php
162
160
<?php
@@ -207,7 +205,6 @@ The same note about setting the `namespace` attribute in the `ruleset.xml` file
207
205
208
206
Also make sure that the `installed_paths` configuration option is set correctly and points to the `MyStandard` directory.
*[Ignoring Files and Folders](#ignoring-files-and-folders)
4
5
*[Ignoring Parts of a File](#ignoring-parts-of-a-file)
@@ -19,6 +20,7 @@
19
20
***
20
21
21
22
## Specifying Valid File Extensions
23
+
22
24
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.
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.
39
42
40
43
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.
> 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.
49
+
> 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.
47
50
48
51
You can also tell PHP_CodeSniffer to ignore a file using a special comment inserted at the top of the file. This will stop the file being checked even if it does not match the ignore pattern.
49
52
@@ -78,6 +81,7 @@ $xmlPackage->send();
78
81
79
82
80
83
## Ignoring Parts of a File
84
+
81
85
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.
82
86
83
87
```php
@@ -166,6 +170,7 @@ bar($foo,false);
166
170
167
171
168
172
## Limiting Results to Specific Sniffs
173
+
169
174
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.
## Filtering Errors and Warnings Based on Severity
205
+
200
206
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.
201
207
202
208
To hide errors and warnings with a severity less than 3:
@@ -219,6 +225,7 @@ This feature is particularly useful during manual code reviews. During normal de
219
225
220
226
221
227
## Replacing Tabs with Spaces
228
+
222
229
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.
223
230
224
231
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.
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.
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.
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.
255
265
256
266
> [!IMPORTANT]
@@ -265,6 +275,7 @@ The `phpcs.xml` file has exactly the same format as a normal [[ruleset.xml file|
265
275
266
276
267
277
## Specifying php.ini Settings
278
+
268
279
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.
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]].
285
297
286
298
To set a configuration option, use the `--config-set` command line argument.
PHP_CodeSniffer allows you to delete any configuration option, reverting it to its default value. [[View a full list of configuration options|Configuration Options]].
306
319
307
320
To delete a configuration option, use the `--config-delete` command line argument.
To view the currently set configuration options, use the `--config-show` command line argument.
318
332
319
333
```bash
@@ -329,13 +343,15 @@ Array
329
343
330
344
331
345
## Printing Verbose Tokeniser Output
346
+
332
347
This feature is provided for debugging purposes only. Using this feature will dramatically increase screen output and script running time.
333
348
334
349
PHP_CodeSniffer contains multiple verbosity levels. Level 2 (indicated by the command line argument `-vv`) will print all verbosity information for level 1 (file specific token and line counts with running times) as well as verbose tokeniser output.
335
350
336
351
The output of the PHP_CodeSniffer tokeniser shows the step-by-step creation of the scope map and the level map.
337
352
338
353
### The Scope Map
354
+
339
355
The scope map is best explained with an example. For the following file:
340
356
341
357
```php
@@ -376,7 +392,8 @@ The scope map output above shows the following pieces of information about the f
376
392
377
393
The scope map output is most useful when debugging PHP_CodeSniffer's scope map, which is critically important to the successful checking of a file, but is also useful for checking the type of a particular token. For example, if you are unsure of the token type for an opening curly brace, the scope map output shows you that the type is T_OPEN_CURLY_BRACKET and not, for example, T_OPEN_CURLY_BRACE.
378
394
379
-
### The Level Map
395
+
### The Level Map
396
+
380
397
The level map is best explained with an example. For the following file:
381
398
382
399
```php
@@ -427,6 +444,7 @@ The level map is most commonly used to determine indentation rules (e.g., a toke
427
444
428
445
429
446
## Printing Verbose Token Processing Output
447
+
430
448
This feature is provided for debugging purposes only. Using this feature will dramatically increase screen output and script running time.
431
449
432
450
PHP_CodeSniffer contains multiple verbosity levels. Level 3 (indicated by the command line argument `-vvv`) will print all verbosity information for level 1 (file specific token and line counts with running times), level 2 (tokeniser output) as well as token processing output with sniff running times.
@@ -494,6 +512,7 @@ For example, the ScopeIndentSniff executes on the if statement's token only, but
494
512
495
513
496
514
## Quieting Output
515
+
497
516
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.
498
517
499
518
<palign="right"><ahref="#table-of-contents">back to top</a></p>
0 commit comments