Skip to content

Commit cb0e2de

Browse files
committed
4.0 | Wiki: updates for change of the default standard from PEAR to PSR12
Includes automating various output example blocks via the automated replacement mechanism. :warning: This commit should only be merged AFTER the PHPCS 4.0 release! The code samples used for the auto-generated output are specifically crafted to give errors/warnings against PSR12, not against the PEAR standard. :warning: Note: this also means that the artifact containing the preprocessed output will need to be regenerated after the PHPCS 4.0 release to get an accurate representation of the updated wiki pages. Where auto-generated output blocks was not an option, like when the output block in the documentation only displays part of a larger output stream, the command used to manually update the existing output block has been added as an inline comment. Regarding the "diff" report - even with manually installing DiffUtils in GH Actions, the report does not display, so unless someone figures out why, this will need to be manually updated. As for the "info" report, while this could be auto-generated using something like `{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --report=info --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileB.php build/wiki-code-samples/path/to/code/fileC.php"}}`, I believe the existing report example demonstrates the usecase for this report better, so I have deliberately not updated it. Also not that for the "interactive" reporting code sample, I'm cheating a bit as interactive mode obviously cannot be auto-generated without user interaction, but we do know what the report should look like ;-)
1 parent 14b60aa commit cb0e2de

File tree

11 files changed

+175
-368
lines changed

11 files changed

+175
-368
lines changed

.github/workflows/basic-qa.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ jobs:
5858
uses: actions/checkout@v4
5959

6060
- name: Spellcheck
61-
id: spellcheck
62-
continue-on-error: true
6361
uses: streetsidesoftware/cspell-action@v7
6462
with:
6563
# Define glob patterns to filter the files to be checked. Use a new line between patterns to define multiple patterns.
@@ -74,11 +72,6 @@ jobs:
7472
# Limit the files checked to the ones in the pull request or push.
7573
incremental_files_only: false
7674

77-
# Note: if less issues than expected were found, these numbers should be updated in the PR which fixes the issues!
78-
- name: Fail the build when more spelling issues were found than expected
79-
if: ${{ always() && ( steps.spellcheck.outputs.number_of_issues != 3 || steps.spellcheck.outputs.number_of_files_with_issues != 2 ) }}
80-
run: exit 1
81-
8275
shellcheck:
8376
name: 'ShellCheck'
8477
runs-on: ubuntu-latest
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* File docblock.
4+
*/
5+
6+
namespace Foo;
7+
8+
class Bar
9+
{
10+
public function baz( String $param)
11+
{
12+
echo 'hello' . 'world and a lot more text to make this line way too long - trying to reach 120 characters, but that takes work';
13+
return FALSE;
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
NAMESPACE Foo;
4+
5+
if ($foo) {
6+
} else if ($bar) {
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
if ($foo) {
4+
} else if ($bar) {
5+
echo 'hello' . 'world and a lot more text to make this line way too long - trying to reach 120 characters, but that takes work';
6+
}

wiki/About-Standards-for-PHP_CodeSniffer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ You may also find the [Customisable Sniff Properties](https://github.com/PHPCSSt
5050
3. If no (project) ruleset file is found, PHP_CodeSniffer will check if a default standard has been set via `--config-set default_standard [Name]`.
5151
If so, PHP_CodeSniffer will use that standard.
5252
:warning: Setting the default standard is a user-specific setting, so should not be relied upon.
53-
4. If no system-default standard was set, PHP_CodeSniffer will apply the PEAR standard.
53+
4. If no system-default standard was set, PHP_CodeSniffer will apply the PSR12 standard.
5454

5555
<p align="right"><a href="#table-of-contents">back to top</a></p>
5656

wiki/Advanced-Usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ $ phpcs --config-show
328328
Using config file: path/to/PHP_CodeSniffer/CodeSniffer.conf
329329

330330
colors: 1
331-
default_standard: PEAR
331+
default_standard: PSR12
332332
report_width: 150
333333
```
334334

wiki/Configuration-Options.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## Setting the default coding standard
99

10-
By default, PHP_CodeSniffer will use the PEAR coding standard if no standard is supplied on the command line. You can change the default standard by setting the `default_standard` configuration option.
10+
By default, PHP_CodeSniffer will use the PSR12 coding standard if no standard is supplied on the command line. You can change the default standard by setting the `default_standard` configuration option.
1111

1212
```bash
1313
$ phpcs --config-set default_standard Squiz
@@ -16,6 +16,9 @@ $ phpcs --config-set default_standard Squiz
1616
> [!NOTE]
1717
> This configuration option cannot be set using the `--runtime-set` command line argument. To set the coding standard for a single run only, use the `--standard` command line argument.
1818
19+
> [!NOTE]
20+
> Prior to PHP_CodeSniffer 4.0.0, PHP_CodeSniffer would default to the PEAR coding standard.
21+
1922
<p align="right"><a href="#table-of-contents">back to top</a></p>
2023

2124

wiki/Fixing-Errors-Automatically.md

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,8 @@ PHP_CodeSniffer is able to fix many errors and warnings automatically. The PHP C
1212
Screen-based reports, such as the [full](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Reporting#printing-full-and-summary-reports), [summary](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Reporting#printing-full-and-summary-reports) and [source](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Reporting#printing-a-source-report) reports, provide information about how many errors and warnings are found. If any of the issues can be fixed automatically by `phpcbf`, this will be annotated in the report with the `[x]` markings:
1313

1414
```bash
15-
$ phpcs /path/to/code/myfile.php
16-
17-
FILE: /path/to/code/myfile.php
18-
--------------------------------------------------------------------------------
19-
FOUND 5 ERRORS AFFECTING 4 LINES
20-
--------------------------------------------------------------------------------
21-
2 | ERROR | [ ] Missing file doc comment
22-
3 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but
23-
| | found "FALSE"
24-
5 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 1
25-
8 | ERROR | [ ] Missing function doc comment
26-
8 | ERROR | [ ] Opening brace should be on a new line
27-
--------------------------------------------------------------------------------
28-
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
29-
--------------------------------------------------------------------------------
15+
$ phpcs path/to/code/fileA.php
16+
{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileA.php"}}
3017
```
3118

3219
<p align="right"><a href="#table-of-contents">back to top</a></p>
@@ -97,32 +84,55 @@ A TOTAL OF 984 ERRORS WERE FIXED IN 2 FILES
9784

9885
To see the fixes that are being made to a file, specify the `-vv` command line argument when generating a diff report. There is quite a lot of debug output concerning the standard being used and the tokenizing of the file, but the end of the output will look like this:
9986

87+
<!--
88+
Regenerate the below output snippet by running the following command:
89+
$ phpcs -vv --parallel=1 --no-cache --no-colors --report-width=100 --report=diff --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileB.php
90+
-->
10091
```bash
10192
$ phpcs /path/to/file --report=diff -vv
10293
..snip..
10394
*** START FILE FIXING ***
104-
E: [Line 3] Expected 1 space after "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter)
105-
Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff (line 259) replaced token 4 (T_EQUAL) "=" => ""
95+
E: [Line 3] PHP keywords must be lowercase; expected "namespace" but found "NAMESPACE" (Generic.PHP.LowerCaseKeyword.Found)
96+
Generic.PHP.LowerCaseKeyword:76 replaced token 3 (T_NAMESPACE on line 3) "NAMESPACE" => "namespace"
10697
* fixed 1 violations, starting loop 2 *
10798
*** END FILE FIXING ***
10899
```
109100

110101
Sometimes the file may need to be processed multiple times in order to fix all the violations. This can happen when multiple sniffs need to modify the same part of a file, or if a fix causes a new sniff violation somewhere else in the standard. When this happens, the output will look like this:
111102

103+
<!--
104+
Regenerate the below output snippet by running the following command:
105+
$ phpcs -vv --parallel=1 --no-cache --no-colors --report-width=100 --report=diff --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileA.php
106+
-->
112107
```bash
113108
$ phpcs /path/to/file --report=diff -vv
114109
..snip..
115110
*** START FILE FIXING ***
116-
E: [Line 3] Expected 1 space before "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore)
117-
Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff (line 228) replaced token 3 (T_EQUAL) "=" => "·="
118-
E: [Line 3] Expected 1 space after "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter)
119-
* token 3 has already been modified, skipping *
120-
E: [Line 3] Equals sign not aligned correctly; expected 1 space but found 0 spaces (Generic.Formatting.MultipleStatementAlignment.Incorrect)
121-
* token 3 has already been modified, skipping *
122-
* fixed 1 violations, starting loop 2 *
123-
E: [Line 3] Expected 1 space after "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter)
124-
Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff (line 259) replaced token 4 (T_EQUAL) "=" => ""
125-
* fixed 1 violations, starting loop 3 *
111+
E: [Line 1] Header blocks must be separated by a single blank line (PSR12.Files.FileHeader.SpacingAfterTagBlock)
112+
PSR12.Files.FileHeader:311 replaced token 0 (T_OPEN_TAG on line 1) "<?php" => "<?php\n"
113+
E: [Line 13] Line indented incorrectly; expected at least 8 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.Incorrect)
114+
Generic.WhiteSpace.ScopeIndent:1353 replaced token 51 (T_WHITESPACE on line 13) " return" => " return"
115+
E: [Line 10] PHP parameter type declarations must be lowercase; expected "string" but found "String" (Generic.PHP.LowerCaseType.ParamTypeFound)
116+
Generic.PHP.LowerCaseType:353 replaced token 33 (T_STRING on line 10) "String" => "string"
117+
E: [Line 10] Expected 1 space between type hint and argument "$param"; 2 found (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint)
118+
=> Changeset started by Squiz.Functions.FunctionDeclarationArgumentSpacing:291
119+
Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:292 replaced token 33 (T_STRING on line 10) "string" => "string "
120+
Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:295 replaced token 34 (T_WHITESPACE on line 10) " $param" => "$param"
121+
* token 33 has already been modified, skipping *
122+
=> Changeset failed to apply
123+
E: [Line 10] Expected 0 spaces after opening parenthesis; 1 found (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen)
124+
Squiz.Functions.FunctionDeclarationArgumentSpacing:560 replaced token 32 (T_WHITESPACE on line 10) " string" => "string"
125+
E: [Line 13] TRUE, FALSE and NULL must be lowercase; expected "false" but found "FALSE" (Generic.PHP.LowerCaseConstant.Found)
126+
Generic.PHP.LowerCaseConstant:262 replaced token 54 (T_FALSE on line 13) "FALSE" => "false"
127+
* fixed 5 violations, starting loop 2 *
128+
E: [Line 11] Expected 1 space between type hint and argument "$param"; 2 found (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint)
129+
=> Changeset started by Squiz.Functions.FunctionDeclarationArgumentSpacing:291
130+
Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:292 replaced token 33 (T_STRING on line 11) "string" => "string "
131+
Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:295 replaced token 34 (T_WHITESPACE on line 11) " $param" => "$param"
132+
A: Squiz.Functions.FunctionDeclarationArgumentSpacing:298 replaced token 33 (T_STRING on line 11) "string" => "string "
133+
A: Squiz.Functions.FunctionDeclarationArgumentSpacing:298 replaced token 34 (T_WHITESPACE on line 11) " $param" => "$param"
134+
=> Changeset ended: 2 changes applied
135+
* fixed 2 violations, starting loop 3 *
126136
*** END FILE FIXING ***
127137
```
128138

wiki/Home.md

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,21 @@ PHP_CodeSniffer is an essential development tool that ensures your code remains
66

77
A coding standard in PHP_CodeSniffer is a collection of sniff files. Each sniff file checks one part of the coding standard only. Each sniff can yield multiple error codes, a different one for each aspect of the code which was checked and found non-compliant.
88

9-
Multiple coding standards can be used within PHP_CodeSniffer so that the one installation can be used across multiple projects. The default coding standard used by PHP_CodeSniffer is the PEAR coding standard.
9+
Multiple coding standards can be used within PHP_CodeSniffer so that the one installation can be used across multiple projects.
10+
As of PHP_CodeSniffer 4.0.0, the default coding standard used by PHP_CodeSniffer is the PSR12 coding standard (previously, this was the PEAR standard).
1011

1112
## Example
1213

13-
To check a file against the PEAR coding standard, simply specify the file's location.
14+
To check a file against the PSR12 coding standard, simply specify the file's location.
1415

1516
```bash
16-
$ phpcs /path/to/code/myfile.php
17-
18-
FILE: /path/to/code/myfile.php
19-
--------------------------------------------------------------------------------
20-
FOUND 5 ERROR(S) AFFECTING 2 LINE(S)
21-
--------------------------------------------------------------------------------
22-
2 | ERROR | Missing file doc comment
23-
20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
24-
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
25-
51 | ERROR | Missing function doc comment
26-
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
27-
--------------------------------------------------------------------------------
17+
$ phpcs path/to/code/fileA.php
18+
{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileA.php"}}
2819
```
2920

3021
Or, if you wish to check an entire directory, you can specify the directory location instead of a file.
3122

3223
```bash
3324
$ phpcs /path/to/code
34-
35-
FILE: /path/to/code/myfile.php
36-
--------------------------------------------------------------------------------
37-
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
38-
--------------------------------------------------------------------------------
39-
2 | ERROR | Missing file doc comment
40-
20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
41-
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
42-
51 | ERROR | Missing function doc comment
43-
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
44-
--------------------------------------------------------------------------------
45-
46-
FILE: /path/to/code/yourfile.php
47-
--------------------------------------------------------------------------------
48-
FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
49-
--------------------------------------------------------------------------------
50-
21 | ERROR | PHP keywords must be lowercase; expected "false" but found
51-
| | "FALSE"
52-
21 | WARNING | Equals sign not aligned with surrounding assignments
53-
--------------------------------------------------------------------------------
25+
{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code"}}
5426
```

0 commit comments

Comments
 (0)