Skip to content

Commit e5e62be

Browse files
committed
Advanced Usage: document the --generator CLI argument
PHPCS supports displaying the documentation that some sniffs ship with via the `--generator` CLI argument, but until now this feature was only mentioned briefly in the `phpcs --help` output rendered on the Usage page. This commit adds a dedicated "Showing Sniff Documentation" section to the Advanced Usage page, covering what the feature does, the three available output formats, and how `--standard`, `--sniffs` and `--exclude` affect the rendered documentation.
1 parent 4615316 commit e5e62be

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

wiki/Advanced-Usage.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,63 @@ In PHP_CodeSniffer 3.x, the exit codes were:
556556
And the following configuration flags were available: [`ignore_errors_on_exit`](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Configuration-Options#ignoring-errors-when-generating-the-exit-code), [`ignore_warnings_on_exit`](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Configuration-Options#ignoring-warnings-when-generating-the-exit-code).
557557

558558
<p align="right"><a href="#table-of-contents">back to top</a></p>
559+
560+
561+
## Showing Sniff Documentation
562+
563+
Some sniffs ship with documentation that explains what they check, along with valid and invalid code examples. PHP_CodeSniffer can print this documentation for the sniffs in a coding standard using the `--generator` command line argument.
564+
565+
```bash
566+
$ phpcs --standard=PSR12 --generator=Text
567+
```
568+
569+
For example, the documentation for the `PSR1.Files.SideEffects` sniff looks like this:
570+
571+
```text
572+
{{COMMAND-OUTPUT "phpcs --standard=PSR1 --sniffs=PSR1.Files.SideEffects --generator=Text"}}
573+
```
574+
575+
> [!NOTE]
576+
> Not every sniff in a coding standard ships with documentation. Sniffs without a documentation file are silently skipped, so the output may not cover every sniff that the standard would run during a scan.
577+
578+
### Available Documentation Formats
579+
580+
Three documentation formats are supported. The generator name is case-insensitive.
581+
582+
| Generator | Description |
583+
|------------|-------------------------------------------------------------|
584+
| `Text` | Plain text output, suitable for reading in a terminal. |
585+
| `HTML` | HTML document with a table of contents and embedded styles. |
586+
| `Markdown` | Markdown output. |
587+
588+
To save the generated documentation to a file, redirect the output to a file:
589+
590+
```bash
591+
$ phpcs --standard=PSR12 --generator=HTML > psr12-docs.html
592+
```
593+
594+
### Selecting Which Standards and Sniffs to Document
595+
596+
If `--standard` is omitted, PHP_CodeSniffer uses the default coding standard, the same way it does for a normal scan.
597+
598+
Multiple standards can be passed as a comma-separated list. The documentation for each standard is printed in the order the standards are listed:
599+
600+
```bash
601+
$ phpcs --standard=PSR12,PSR1 --generator=Text
602+
```
603+
604+
The `--sniffs` and `--exclude` command line arguments work the same way they do during a normal scan, allowing you to limit or exclude documentation for specific sniffs. See [Limiting Results to Specific Sniffs](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Advanced-Usage#limiting-results-to-specific-sniffs) for the full syntax.
605+
606+
The following example only prints the documentation for a single sniff:
607+
608+
```bash
609+
$ phpcs --standard=PSR1 --sniffs=PSR1.Classes.ClassDeclaration --generator=Text
610+
```
611+
612+
The following example prints the documentation for all sniffs in the `PSR1` standard except for the one specified:
613+
614+
```bash
615+
$ phpcs --standard=PSR1 --exclude=PSR1.Classes.ClassDeclaration --generator=Text
616+
```
617+
618+
<p align="right"><a href="#table-of-contents">back to top</a></p>

0 commit comments

Comments
 (0)