diff --git a/.github/workflows/basic-qa.yml b/.github/workflows/basic-qa.yml index 3547499..b06000d 100644 --- a/.github/workflows/basic-qa.yml +++ b/.github/workflows/basic-qa.yml @@ -58,8 +58,6 @@ jobs: uses: actions/checkout@v4 - name: Spellcheck - id: spellcheck - continue-on-error: true uses: streetsidesoftware/cspell-action@v7 with: # 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: # Limit the files checked to the ones in the pull request or push. incremental_files_only: false - # Note: if less issues than expected were found, these numbers should be updated in the PR which fixes the issues! - - name: Fail the build when more spelling issues were found than expected - if: ${{ always() && ( steps.spellcheck.outputs.number_of_issues != 3 || steps.spellcheck.outputs.number_of_files_with_issues != 2 ) }} - run: exit 1 - shellcheck: name: 'ShellCheck' runs-on: ubuntu-latest diff --git a/build/wiki-code-samples/path/to/code/fileA.php b/build/wiki-code-samples/path/to/code/fileA.php new file mode 100644 index 0000000..b4ed04e --- /dev/null +++ b/build/wiki-code-samples/path/to/code/fileA.php @@ -0,0 +1,15 @@ +back to top
diff --git a/wiki/Advanced-Usage.md b/wiki/Advanced-Usage.md index 25bc73e..9e33dbb 100644 --- a/wiki/Advanced-Usage.md +++ b/wiki/Advanced-Usage.md @@ -328,7 +328,7 @@ $ phpcs --config-show Using config file: path/to/PHP_CodeSniffer/CodeSniffer.conf colors: 1 -default_standard: PEAR +default_standard: PSR12 report_width: 150 ``` diff --git a/wiki/Configuration-Options.md b/wiki/Configuration-Options.md index 528bfb3..7ad984c 100644 --- a/wiki/Configuration-Options.md +++ b/wiki/Configuration-Options.md @@ -7,7 +7,7 @@ ## Setting the default coding standard -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. +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. ```bash $ phpcs --config-set default_standard Squiz @@ -16,6 +16,9 @@ $ phpcs --config-set default_standard Squiz > [!NOTE] > 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. +> [!NOTE] +> Prior to PHP_CodeSniffer 4.0.0, PHP_CodeSniffer would default to the PEAR coding standard. + diff --git a/wiki/Fixing-Errors-Automatically.md b/wiki/Fixing-Errors-Automatically.md index 3a93df5..00af35c 100644 --- a/wiki/Fixing-Errors-Automatically.md +++ b/wiki/Fixing-Errors-Automatically.md @@ -12,21 +12,8 @@ PHP_CodeSniffer is able to fix many errors and warnings automatically. The PHP C 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: ```bash -$ phpcs /path/to/code/myfile.php - -FILE: /path/to/code/myfile.php --------------------------------------------------------------------------------- -FOUND 5 ERRORS AFFECTING 4 LINES --------------------------------------------------------------------------------- - 2 | ERROR | [ ] Missing file doc comment - 3 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but - | | found "FALSE" - 5 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 1 - 8 | ERROR | [ ] Missing function doc comment - 8 | ERROR | [ ] Opening brace should be on a new line --------------------------------------------------------------------------------- -PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY --------------------------------------------------------------------------------- +$ phpcs path/to/code/fileA.php +{{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"}} ``` @@ -97,32 +84,55 @@ A TOTAL OF 984 ERRORS WERE FIXED IN 2 FILES 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: + ```bash $ phpcs /path/to/file --report=diff -vv ..snip.. *** START FILE FIXING *** -E: [Line 3] Expected 1 space after "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter) -Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff (line 259) replaced token 4 (T_EQUAL) "=" => "=·" +E: [Line 3] PHP keywords must be lowercase; expected "namespace" but found "NAMESPACE" (Generic.PHP.LowerCaseKeyword.Found) +Generic.PHP.LowerCaseKeyword:76 replaced token 3 (T_NAMESPACE on line 3) "NAMESPACE" => "namespace" * fixed 1 violations, starting loop 2 * *** END FILE FIXING *** ``` 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: + ```bash $ phpcs /path/to/file --report=diff -vv ..snip.. *** START FILE FIXING *** -E: [Line 3] Expected 1 space before "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore) -Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff (line 228) replaced token 3 (T_EQUAL) "=" => "·=" -E: [Line 3] Expected 1 space after "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter) -* token 3 has already been modified, skipping * -E: [Line 3] Equals sign not aligned correctly; expected 1 space but found 0 spaces (Generic.Formatting.MultipleStatementAlignment.Incorrect) -* token 3 has already been modified, skipping * -* fixed 1 violations, starting loop 2 * -E: [Line 3] Expected 1 space after "="; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter) -Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff (line 259) replaced token 4 (T_EQUAL) "=" => "=·" -* fixed 1 violations, starting loop 3 * +E: [Line 1] Header blocks must be separated by a single blank line (PSR12.Files.FileHeader.SpacingAfterTagBlock) +PSR12.Files.FileHeader:311 replaced token 0 (T_OPEN_TAG on line 1) " " " return" +E: [Line 10] PHP parameter type declarations must be lowercase; expected "string" but found "String" (Generic.PHP.LowerCaseType.ParamTypeFound) +Generic.PHP.LowerCaseType:353 replaced token 33 (T_STRING on line 10) "String" => "string" +E: [Line 10] Expected 1 space between type hint and argument "$param"; 2 found (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint) +=> Changeset started by Squiz.Functions.FunctionDeclarationArgumentSpacing:291 + Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:292 replaced token 33 (T_STRING on line 10) "string" => "string " + Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:295 replaced token 34 (T_WHITESPACE on line 10) " $param" => "$param" + * token 33 has already been modified, skipping * +=> Changeset failed to apply +E: [Line 10] Expected 0 spaces after opening parenthesis; 1 found (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen) +Squiz.Functions.FunctionDeclarationArgumentSpacing:560 replaced token 32 (T_WHITESPACE on line 10) " string" => "string" +E: [Line 13] TRUE, FALSE and NULL must be lowercase; expected "false" but found "FALSE" (Generic.PHP.LowerCaseConstant.Found) +Generic.PHP.LowerCaseConstant:262 replaced token 54 (T_FALSE on line 13) "FALSE" => "false" +* fixed 5 violations, starting loop 2 * +E: [Line 11] Expected 1 space between type hint and argument "$param"; 2 found (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint) +=> Changeset started by Squiz.Functions.FunctionDeclarationArgumentSpacing:291 + Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:292 replaced token 33 (T_STRING on line 11) "string" => "string " + Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:295 replaced token 34 (T_WHITESPACE on line 11) " $param" => "$param" + A: Squiz.Functions.FunctionDeclarationArgumentSpacing:298 replaced token 33 (T_STRING on line 11) "string" => "string " + A: Squiz.Functions.FunctionDeclarationArgumentSpacing:298 replaced token 34 (T_WHITESPACE on line 11) " $param" => "$param" +=> Changeset ended: 2 changes applied +* fixed 2 violations, starting loop 3 * *** END FILE FIXING *** ``` diff --git a/wiki/Home.md b/wiki/Home.md index 6d94d67..404615d 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -6,49 +6,21 @@ PHP_CodeSniffer is an essential development tool that ensures your code remains 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. -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. +Multiple coding standards can be used within PHP_CodeSniffer so that the one installation can be used across multiple projects. +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). ## Example -To check a file against the PEAR coding standard, simply specify the file's location. +To check a file against the PSR12 coding standard, simply specify the file's location. ```bash -$ phpcs /path/to/code/myfile.php - -FILE: /path/to/code/myfile.php --------------------------------------------------------------------------------- -FOUND 5 ERROR(S) AFFECTING 2 LINE(S) --------------------------------------------------------------------------------- - 2 | ERROR | Missing file doc comment - 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" - 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 - 51 | ERROR | Missing function doc comment - 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 --------------------------------------------------------------------------------- +$ phpcs path/to/code/fileA.php +{{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"}} ``` Or, if you wish to check an entire directory, you can specify the directory location instead of a file. ```bash $ phpcs /path/to/code - -FILE: /path/to/code/myfile.php --------------------------------------------------------------------------------- -FOUND 5 ERROR(S) AFFECTING 5 LINE(S) --------------------------------------------------------------------------------- - 2 | ERROR | Missing file doc comment - 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" - 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 - 51 | ERROR | Missing function doc comment - 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 --------------------------------------------------------------------------------- - -FILE: /path/to/code/yourfile.php --------------------------------------------------------------------------------- -FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S) --------------------------------------------------------------------------------- - 21 | ERROR | PHP keywords must be lowercase; expected "false" but found - | | "FALSE" - 21 | WARNING | Equals sign not aligned with surrounding assignments --------------------------------------------------------------------------------- +{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code"}} ``` diff --git a/wiki/Reporting.md b/wiki/Reporting.md index 1be376f..2ae00a5 100644 --- a/wiki/Reporting.md +++ b/wiki/Reporting.md @@ -29,64 +29,23 @@ When running a scan, by default, the full report is displayed. ```bash -$ phpcs /path/to/code/myfile.php - -FILE: /path/to/code/myfile.php --------------------------------------------------------------------------------- -FOUND 4 ERRORS AND 1 WARNING AFFECTING 5 LINES --------------------------------------------------------------------------------- - 2 | ERROR | [ ] Missing file doc comment - 4 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but - | | found "FALSE" - 6 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found - | | 1 - 9 | ERROR | [ ] Missing function doc comment - 11 | WARNING | [x] Inline control structures are discouraged --------------------------------------------------------------------------------- -PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY --------------------------------------------------------------------------------- +$ phpcs path/to/code/fileA.php +{{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"}} ``` To see a summary of the errors and warnings per file, use the `--report=summary` option. ```bash $ phpcs --report=summary /path/to/code - -PHP CODE SNIFFER REPORT SUMMARY --------------------------------------------------------------------------------- -FILE ERRORS WARNINGS --------------------------------------------------------------------------------- -/path/to/code/classA.inc 5 0 -/path/to/code/classB.inc 1 1 -/path/to/code/classC.inc 0 2 --------------------------------------------------------------------------------- -A TOTAL OF 6 ERROR(S) AND 3 WARNING(S) WERE FOUND IN 3 FILE(S) --------------------------------------------------------------------------------- +{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --report=summary --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code"}} ``` The full report can also show information about the source of errors and warnings. To include source codes in the report, use the `-s` command line argument. ```bash -$ phpcs -s /path/to/code/myfile.php - -FILE: /path/to/code/myfile.php --------------------------------------------------------------------------------- -FOUND 4 ERRORS AND 1 WARNING AFFECTING 5 LINES --------------------------------------------------------------------------------- - 2 | ERROR | [ ] Missing file doc comment - | | (PEAR.Commenting.FileComment.Missing) - 4 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but - | | found "FALSE" (Generic.PHP.LowerCaseConstant.Found) - 6 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found - | | 1 (PEAR.WhiteSpace.ScopeIndent.Incorrect) - 9 | ERROR | [ ] Missing function doc comment - | | (PEAR.Commenting.FunctionComment.Missing) - 11 | WARNING | [x] Inline control structures are discouraged - | | (Generic.ControlStructures.InlineControlStructure.Discouraged) --------------------------------------------------------------------------------- -PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY --------------------------------------------------------------------------------- +$ phpcs -s path/to/code/fileA.php +{{COMMAND-OUTPUT "phpcs -s --parallel=1 --no-cache --no-colors --report-width=100 --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileA.php"}} ``` Source codes can be used with the `--sniffs` command line argument to only show messages from a specified list of sources @@ -102,42 +61,14 @@ PHP_CodeSniffer can output a summary report showing you the most common errors d ```bash $ phpcs --report=source /path/to/code - -PHP CODE SNIFFER VIOLATION SOURCE SUMMARY ------------------------------------------------------------------------------ -STANDARD CATEGORY SNIFF COUNT ------------------------------------------------------------------------------ -[x] PEAR White space Scope indent incorrect 1 -[x] Generic PHP Lower case constant found 1 -[x] Generic Control structures Inline control structure discouraged 1 -[ ] PEAR Commenting Function comment missing 1 -[ ] PEAR Commenting File comment missing 1 ------------------------------------------------------------------------------ -A TOTAL OF 5 SNIFF VIOLATIONS WERE FOUND IN 5 SOURCES ------------------------------------------------------------------------------ -PHPCBF CAN FIX THE 3 MARKED SOURCES AUTOMATICALLY (3 VIOLATIONS IN TOTAL) ------------------------------------------------------------------------------ +{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --report=source --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileA.php"}} ``` To show source codes instead of friendly names, use the `-s` command line argument. ```bash $ phpcs -s --report=source /path/to/code - -PHP CODE SNIFFER VIOLATION SOURCE SUMMARY ------------------------------------------------------------------------ -SOURCE COUNT ------------------------------------------------------------------------ -[x] Generic.ControlStructures.InlineControlStructure.Discouraged 1 -[x] PEAR.WhiteSpace.ScopeIndent.Incorrect 1 -[x] Generic.PHP.LowerCaseConstant.Found 1 -[ ] PEAR.Commenting.FunctionComment.Missing 1 -[ ] PEAR.Commenting.FileComment.Missing 1 ------------------------------------------------------------------------ -A TOTAL OF 5 SNIFF VIOLATIONS WERE FOUND IN 5 SOURCES ------------------------------------------------------------------------ -PHPCBF CAN FIX THE 3 MARKED SOURCES AUTOMATICALLY (3 VIOLATIONS IN TOTAL) ------------------------------------------------------------------------ +{{COMMAND-OUTPUT "phpcs -s --parallel=1 --no-cache --no-colors --report-width=100 --report=source --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileA.php"}} ``` @@ -201,53 +132,8 @@ In the example above, the `Inline comment style` convention was checked 594 time PHP_CodeSniffer can output a report that shows a code snippet for each error and warning, showing the context in which the violation has occurred. The output will look like this: ```bash -$ phpcs --report=code /path/to/code - -FILE: /path/to/code/classA.php ------------------------------------------------------------------------------------------------- -FOUND 4 ERRORS AND 1 WARNING AFFECTING 5 LINES ------------------------------------------------------------------------------------------------- -LINE 2: ERROR [ ] Missing file doc comment ------------------------------------------------------------------------------------------------- - 1: > 2: - 3: if·($foo·===·null)·{ - 4: ····$foo·=·FALSE; ------------------------------------------------------------------------------------------------- -LINE 4: ERROR [x] TRUE, FALSE and NULL must be lowercase; expected "false" but found "FALSE" ------------------------------------------------------------------------------------------------- - 2: - 3: if·($foo·===·null)·{ ->> 4: ····$foo·=·FALSE; - 5: }·else·{ - 6: ·$foo·=·getFoo(); ------------------------------------------------------------------------------------------------- -LINE 6: ERROR [x] Line indented incorrectly; expected at least 4 spaces, found 1 ------------------------------------------------------------------------------------------------- - 4: ····$foo·=·FALSE; - 5: }·else·{ ->> 6: ·$foo·=·getFoo(); - 7: } - 8: ------------------------------------------------------------------------------------------------- -LINE 9: ERROR [ ] Missing function doc comment ------------------------------------------------------------------------------------------------- - 7: } - 8: ->> 9: function·getFoo() - 10: { - 11: ····if·($foo)·return·'foo'; ------------------------------------------------------------------------------------------------- -LINE 11: WARNING [x] Inline control structures are discouraged ------------------------------------------------------------------------------------------------- - 9: function·getFoo() - 10: { ->> 11: ····if·($foo)·return·'foo'; - 12: ····return·'bar'; - 13: } ------------------------------------------------------------------------------------------------- -PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY ------------------------------------------------------------------------------------------------- +$ phpcs --report=code /path/to/code/fileA.php +{{COMMAND-OUTPUT "phpcs --parallel=1 --no-cache --no-colors --report-width=100 --report=code --basepath=build/wiki-code-samples build/wiki-code-samples/path/to/code/fileA.php"}} ``` > [!NOTE] @@ -264,16 +150,7 @@ PHP_CodeSniffer can output an XML report similar to the one produced by Checksty $ phpcs --report=checkstyle /path/to/code ``` ```xml - -