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
+30-14Lines changed: 30 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,7 @@ In the following example, PHP_CodeSniffer will skip all files inside the package
36
36
37
37
$ phpcs --ignore=*/tests/*,*/data/* /path/to/code
38
38
39
+
> [!IMPORTANT]
39
40
> 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.
40
41
41
42
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.
> Note: Before PHP_CodeSniffer version 3.2.0, use `// @codingStandardsIgnoreFile` instead of `// phpcs:ignoreFile`. The `@codingStandards` syntax is deprecated and will be removed in PHP_CodeSniffer version 4.0.
52
+
> [!NOTE]
53
+
> Before PHP_CodeSniffer version 3.2.0, use `// @codingStandardsIgnoreFile` instead of `// phpcs:ignoreFile`. The `@codingStandards` syntax is deprecated and will be removed in PHP_CodeSniffer version 4.0.
52
54
53
-
> Note: The `phpcs:ignoreFile` comment syntax does not allow for a specific set of sniffs to be ignored for a file. Use the `phpcs:disable` comment syntax if you want to disable a specific set of sniffs for the entire file.
55
+
> [!NOTE]
56
+
> The `phpcs:ignoreFile` comment syntax does not allow for a specific set of sniffs to be ignored for a file. Use the `phpcs:disable` comment syntax if you want to disable a specific set of sniffs for the entire file.
54
57
55
58
If required, you can add a note explaining why the file is being ignored by using the `--` separator.
> Note: The comment syntax note feature is only available from PHP_CodeSniffer version 3.2.0 onwards.
68
+
> [!NOTE]
69
+
> The comment syntax note feature is only available from PHP_CodeSniffer version 3.2.0 onwards.
66
70
67
71
## Ignoring Parts of a File
68
72
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.
@@ -75,7 +79,8 @@ $xmlPackage->send();
75
79
// phpcs:enable
76
80
```
77
81
78
-
> Note: Before PHP_CodeSniffer version 3.2.0, use `// @codingStandardsIgnoreStart` instead of `// phpcs:disable`, and use `// @codingStandardsIgnoreEnd` instead of `// phpcs:enable`. The `@codingStandards` syntax is deprecated and will be removed in PHP_CodeSniffer version 4.0.
82
+
> [!NOTE]
83
+
> Before PHP_CodeSniffer version 3.2.0, use `// @codingStandardsIgnoreStart` instead of `// phpcs:disable`, and use `// @codingStandardsIgnoreEnd` instead of `// phpcs:enable`. The `@codingStandards` syntax is deprecated and will be removed in PHP_CodeSniffer version 4.0.
79
84
80
85
If you don't want to disable all coding standard errors, you can selectively disable and re-enable specific error message codes, sniffs, categories of sniffs, or entire coding standards. The following example disables the specific `Generic.Commenting.Todo.Found` message and then re-enables all checks at the end.
81
86
@@ -99,9 +104,11 @@ bar($foo,false);
99
104
// phpcs:enable
100
105
```
101
106
102
-
> Note: All `phpcs:disable` and `phpcs:enable` comments only apply to the file they are contained within. After the file has finished processing all sniffs are re-enabled for future files.
107
+
> [!NOTE]
108
+
> All `phpcs:disable` and `phpcs:enable` comments only apply to the file they are contained within. After the file has finished processing all sniffs are re-enabled for future files.
103
109
104
-
> Note: Selective disabling and re-enabling of codes/sniffs/categories/standards is only available from PHP_CodeSniffer version 3.2.0 onwards.
110
+
> [!NOTE]
111
+
> Selective disabling and re-enabling of codes/sniffs/categories/standards is only available from PHP_CodeSniffer version 3.2.0 onwards.
105
112
106
113
You can also ignore a single line using the `phpcs:ignore` comment. If placed on a line by itself, this comment will ignore the line that the comment is on and the following line. It is typically used like this:
> Note: Before PHP_CodeSniffer version 3.2.0, use `// @codingStandardsIgnoreLine` instead of `// phpcs:ignore`. The `@codingStandards` syntax is deprecated and will be removed in PHP_CodeSniffer version 4.0.
128
+
> [!NOTE]
129
+
> Before PHP_CodeSniffer version 3.2.0, use `// @codingStandardsIgnoreLine` instead of `// phpcs:ignore`. The `@codingStandards` syntax is deprecated and will be removed in PHP_CodeSniffer version 4.0.
122
130
123
131
Again, you can selectively ignore one or more specific error message codes, sniffs, categories of sniffs, or entire standards.
124
132
@@ -128,7 +136,8 @@ $foo = [1,2,3];
128
136
bar($foo, false);
129
137
```
130
138
131
-
> Note: Selective ignoring of codes/sniffs/categories/standards is only available from PHP_CodeSniffer version 3.2.0 onwards.
139
+
> [!NOTE]
140
+
> Selective ignoring of codes/sniffs/categories/standards is only available from PHP_CodeSniffer version 3.2.0 onwards.
132
141
133
142
If required, you can add a note explaining why sniffs are being disable and re-enabled by using the `--` separator.
134
143
@@ -141,12 +150,14 @@ bar($foo,false);
141
150
// phpcs:enable -- this is out code again, so turn everything back on
142
151
```
143
152
144
-
> Note: The comment syntax note feature is only available from PHP_CodeSniffer version 3.2.0 onwards.
153
+
> [!NOTE]
154
+
> The comment syntax note feature is only available from PHP_CodeSniffer version 3.2.0 onwards.
145
155
146
156
## Limiting Results to Specific Sniffs
147
157
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.
148
158
149
-
> Note: All sniffs specified on the command line must be used in the coding standard you are using to check your files.
159
+
> [!NOTE]
160
+
> All sniffs specified on the command line must be used in the coding standard you are using to check your files.
150
161
151
162
The following example will only run two sniffs over the code instead of all sniffs in the PEAR standard:
152
163
@@ -156,7 +167,8 @@ The following example will run all sniffs in the PEAR standard except for the tw
> Note: If you use both the `--sniffs` and `--exclude` command line arguments together, the `--exclude` list will be ignored.
170
+
> [!WARNING]
171
+
> If you use both the `--sniffs` and `--exclude` command line arguments together, the `--exclude` list will be ignored.
160
172
161
173
## Filtering Errors and Warnings Based on Severity
162
174
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.
@@ -182,7 +194,8 @@ In the following example, PHP_CodeSniffer will replace all tabs in the files bei
182
194
183
195
$ phpcs --tab-width=4 /path/to/code
184
196
185
-
> Note: 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.
197
+
> [!NOTE]
198
+
> 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.
186
199
187
200
## Specifying an Encoding
188
201
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.
@@ -197,10 +210,12 @@ PHP_CodeSniffer can optionally include one or more custom bootstrap files before
197
210
## Using a Default Configuration File
198
211
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.
199
212
200
-
> Note: If multiple default configuration files are found, PHP_CodeSniffer will select one using the following order: `.phpcs.xml`, `phpcs.xml`, `.phpcs.xml.dist`, `phpcs.xml.dist`
213
+
> [!IMPORTANT]
214
+
> If multiple default configuration files are found, PHP_CodeSniffer will select one using the following order: `.phpcs.xml`, `phpcs.xml`, `.phpcs.xml.dist`, `phpcs.xml.dist`
201
215
202
216
The `phpcs.xml` file has exactly the same format as a normal [[ruleset.xml file|Annotated ruleset]], so all the same options are available in it. The `phpcs.xml` file essentially acts as a default coding standard and configuration file for a code base, and is typically used to allow the `phpcs` command to be run on a repository without specifying any arguments.
203
217
218
+
> [!NOTE]
204
219
> 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)
205
220
206
221
## Specifying php.ini Settings
@@ -223,7 +238,8 @@ Configuration options are written to a global configuration file. If you want to
> Note: 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`.
241
+
> [!NOTE]
242
+
> 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`.
227
243
228
244
## Deleting Configuration Options
229
245
PHP_CodeSniffer allows you to delete any configuration option, reverting it to its default value. [[View a full list of configuration options|Configuration Options]].
Copy file name to clipboardExpand all lines: wiki/Coding-Standard-Tutorial.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,7 @@ The content of the `ruleset.xml` file should be the following:
29
29
</ruleset>
30
30
```
31
31
32
+
> [!NOTE]
32
33
> The ruleset.xml can be left quite small, as it is in this example coding standard. For information about the other features that the ruleset.xml provides, see the [[Annotated ruleset]].
33
34
34
35
## Creating the Sniff
@@ -39,6 +40,7 @@ A sniff requires a single PHP file that must be placed into a sub-directory to c
39
40
$ mkdir Commenting
40
41
$ touch Commenting/DisallowHashCommentsSniff.php
41
42
43
+
> [!NOTE]
42
44
> It does not matter what sub-directories you use for categorising your sniffs. Just make them descriptive enough so you can find your sniffs again later when you want to modify them.
43
45
44
46
Each sniff must implement the `PHP_CodeSniffer\Sniffs\Sniff` interface so that PHP_CodeSniffer knows that it should instantiate the sniff once it's invoked. The interface defines two methods that must be implemented; `register` and `process`.
0 commit comments