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
* Remove mention of creating the standards in the PHPCS `Standards` directory and replace it with information about registering a standard with PHPCS via the `installed_paths` command.
* Add note about the token array containing additional information.
* Modernize the sniff code sample.
- Replace the file docblock with a simpler class docblock.
- Use the correct namespace for the standard.
- Make it a `final` class.
- Use short arrays.
- Remove `//end ...` comments.
- Improve type specificity in the docblocks.
- Remove PHP close tag.
* Improve the test code by including code samples which should *not* be flagged (but would trigger the sniff).
* Use the more common term (class) "property" instead of "member variable".
* Fix grammatical error.
* Use fenced code blocks for bash commands.
* Various other markdown tweaks for readability.
Copy file name to clipboardExpand all lines: wiki/Coding-Standard-Tutorial.md
+60-66Lines changed: 60 additions & 66 deletions
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,29 @@ In this tutorial, we will create a new coding standard with a single sniff. Our
2
2
3
3
## Creating the Coding Standard Directory
4
4
5
-
All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is a directory with a specific sub-directory structure and a ruleset.xml file, so we can create one very easily. Let's call our coding standard _MyStandard_. Run the following commands to create the coding standard directory structure:
5
+
All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is a directory with a specific sub-directory structure and a `ruleset.xml` file, so we can create one very easily. Let's call our coding standard _MyStandard_. Run the following commands to create the coding standard directory structure:
6
6
7
-
$ mkdir MyStandard
8
-
$ mkdir MyStandard/Sniffs
7
+
```bash
8
+
$ mkdir MyStandard
9
+
$ mkdir MyStandard/Sniffs
10
+
```
9
11
10
-
As this coding standard directory sits outside the main PHP_CodeSniffer directory structure, PHP_CodeSniffer will not show it as an installed standard when using the `-i` command line argument. If you want your standard to be shown as installed, create the MyStandard directory inside the PHP_CodeSniffer install:
12
+
As this coding standard directory sits outside the main PHP_CodeSniffer directory structure, PHP_CodeSniffer will not show it as an installed standard when using the `-i` command line argument. If you want your standard to be shown as installed, [register the MyStandard directory as an external standards with PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-installed-standard-paths):
The `MyStandard` directory represents our coding standard. The `Sniffs` sub-directory is used to store all the sniff files for this coding standard.
17
19
18
-
Now that our directory structure is created, we need to add our ruleset.xml file. This file will allow PHP_CodeSniffer to ask our coding standard for information about itself, and also identify this directory as one that contains code sniffs.
20
+
Now that our directory structure is created, we need to add our `ruleset.xml` file. This file will allow PHP_CodeSniffer to ask our coding standard for information about itself, and also identify this directory as one that contains code sniffs.
19
21
20
-
$ cd MyStandard
21
-
$ touch ruleset.xml
22
+
```bash
23
+
$ cd MyStandard
24
+
$ touch ruleset.xml
25
+
```
22
26
23
-
The content of the `ruleset.xml` file should be the following:
27
+
The content of the `ruleset.xml` file should, at a minimum, be the following:
24
28
25
29
```xml
26
30
<?xml version="1.0"?>
@@ -30,15 +34,17 @@ The content of the `ruleset.xml` file should be the following:
30
34
```
31
35
32
36
> [!NOTE]
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]].
37
+
> 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]].
34
38
35
39
## Creating the Sniff
36
40
37
-
A sniff requires a single PHP file that must be placed into a sub-directory to categorise the type of check it performs. It's name should clearly describe the standard that we are enforcing and must end with `Sniff.php`. For our sniff, we will name the PHP file `DisallowHashCommentsSniff.php` and place it into a `Commenting` sub-directory to categorise this sniff as relating to commenting. Run the following commands to create the category and the sniff:
41
+
A sniff requires a single PHP file that must be placed into a sub-directory to categorise the type of check it performs. Its name should clearly describe the standard that we are enforcing and must end with `Sniff.php`. For our sniff, we will name the PHP file `DisallowHashCommentsSniff.php` and place it into a `Commenting` sub-directory to categorise this sniff as relating to commenting. Run the following commands to create the category and the sniff:
38
42
39
-
$ cd Sniffs
40
-
$ mkdir Commenting
41
-
$ touch Commenting/DisallowHashCommentsSniff.php
43
+
```bash
44
+
$ cd Sniffs
45
+
$ mkdir Commenting
46
+
$ touch Commenting/DisallowHashCommentsSniff.php
47
+
```
42
48
43
49
> [!NOTE]
44
50
> 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.
@@ -53,49 +59,43 @@ For our sniff, we are interested in single line comments. The `token_get_all` me
53
59
54
60
## The Token Stack
55
61
56
-
A sniff can gather more information about a token by acquiring the token stack with a call to the `getTokens` method on the `PHP_CodeSniffer\Files\File` object. This method returns an array and is indexed by the position where the token occurs in the token stack. Each element in the array represents a token. All tokens have a `code`, `type` and a `content` index in their array. The `code` value is a unique integer for the type of token. The `type` value is a string representation of the token (e.g., 'T_COMMENT' for comment tokens). The `type` has a corresponding globally defined integer with the same name. Finally, the `content` value contains the content of the token as it appears in the code.
62
+
A sniff can gather more information about a token by acquiring the token stack with a call to the `getTokens` method on the `PHP_CodeSniffer\Files\File` object. This method returns an array and is indexed by the position where the token occurs in the token stack. Each element in the array represents a token. All tokens have a `code`, `type` and a `content` index in their array. The `code` value is a unique integer for the type of token. The `type` value is a string representation of the token (e.g., `'T_COMMENT'` for comment tokens). The `type` has a corresponding globally defined integer with the same name. Finally, the `content` value contains the content of the token as it appears in the code.
63
+
64
+
> [!NOTE]
65
+
> Depending on the token, the token array may contain various additional indexes with further information on a token.
57
66
58
67
## Reporting Errors
59
68
60
-
Once an error is detected, a sniff should indicate that an error has occurred by calling the `addError` method on the `PHP_CodeSniffer\Files\File` object, passing in an appropriate error message as the first argument, the position in the stack where the error was detected as the second, a code to uniquely identify the error within this sniff and an array of data used inside the error message. Alternatively, if the violation is considered not as critical as an error, the `addWarning` method can be used.
69
+
Once an error is detected, a sniff should indicate that an error has occurred by calling the `addError` method on the `PHP_CodeSniffer\Files\File` object, passing in an appropriate error message as the first argument, the position in the stack where the error was detected as the second, a code to uniquely identify the error within this sniff and an array of data used inside the error message.
70
+
Alternatively, if the violation is considered not as critical as an error, the `addWarning` method can be used.
61
71
62
72
## DisallowHashCommentsSniff.php
63
73
64
74
We now have to write the content of our sniff. The content of the `DisallowHashCommentsSniff.php` file should be the following:
65
75
66
76
```php
67
77
<?php
68
-
/**
69
-
* This sniff prohibits the use of Perl style hash comments.
By default, PHP_CodeSniffer assumes all sniffs are designed to check PHP code only. You can specify a list of tokenizers that your sniff supports, allowing it to be used wth PHP, JavaScript or CSS files, or any combination of the three. You do this by setting the `$supportedTokenizers`member variable in your sniff. Adding the following code to your sniff will tell PHP_CodeSniffer that it can be used to check both PHP and JavaScript code:
121
+
By default, PHP_CodeSniffer assumes all sniffs are designed to check PHP code only. You can specify a list of tokenizers that your sniff supports, allowing it to be used wth PHP, JavaScript or CSS files, or any combination of the three. You do this by setting the `$supportedTokenizers`property in your sniff. Adding the following code to your sniff will tell PHP_CodeSniffer that it can be used to check both PHP and JavaScript code:
127
122
128
123
```php
129
124
/**
130
125
* A list of tokenizers this sniff supports.
131
126
*
132
-
* @var array
127
+
* @var array<string>
133
128
*/
134
-
public $supportedTokenizers = array(
129
+
public $supportedTokenizers = [
135
130
'PHP',
136
131
'JS',
137
-
);
132
+
];
138
133
```
139
134
140
135
@@ -145,7 +140,11 @@ Now that we have defined a coding standard, let's validate a file that contains
145
140
```php
146
141
<?php
147
142
148
-
# Check for valid contents.
143
+
// Slash comments should be ignored.
144
+
145
+
/* Star comments should also be ignored. */
146
+
147
+
# Hash comments should be flagged.
149
148
if ($obj->contentsAreValid($array)) {
150
149
$value = $obj->getValue();
151
150
@@ -156,23 +155,18 @@ if ($obj->contentsAreValid($array)) {
156
155
exit();
157
156
}
158
157
}
159
-
160
-
?>
161
158
```
162
159
163
160
When PHP_CodeSniffer is run on the file using our new coding standard, 3 errors will be reported:
Note that we pass the absolute path to our coding standard directory on the command line because our standard is not installed inside the main PHP_CodeSniffer directory structure. If you have created your standard inside PHP_CodeSniffer, you can simply pass the name of the standard:
0 commit comments