Skip to content

Commit 6affc3e

Browse files
committed
Run phpcs/phpcbf commands to show output
1 parent 396ea12 commit 6affc3e

File tree

5 files changed

+50
-196
lines changed

5 files changed

+50
-196
lines changed

.github/workflows/publish-wiki.yml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,6 @@ jobs:
7272
if: ${{ steps.phar_version.outputs.VERSION != steps.latest_version.outputs.TAG }}
7373
run: exit 1
7474

75-
# ############################
76-
# Prepare output snippets.
77-
# ############################
78-
79-
- name: Create directory to place snippets in
80-
run: mkdir build/output-snippets
81-
82-
- name: "Usage page: phpcs help"
83-
run: phpcs --report-width=110 --no-colors -h > build/output-snippets/phpcs-h.txt
84-
85-
- name: "Fixing page: phpcbf help"
86-
run: phpbfs --report-width=110 --no-colors -h > build/output-snippets/phpcbf-h.txt
87-
88-
89-
9075
# ############################
9176
# Update Wiki files.
9277
# ############################
@@ -98,6 +83,10 @@ jobs:
9883
shell: bash
9984
run: cp -v -a wiki _wiki
10085

86+
- name: Find / replace output example placeholders
87+
shell: bash
88+
run: build/wiki-command-replacer.sh
89+
10190
- name: Update tables of contents
10291
run: doctoc ./_wiki/ --github --maxlevel 4 --update-only
10392

build/wiki-command-replacer.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu -o pipefail
4+
5+
cd "$(dirname "$0")/.."
6+
7+
MARKER_START='{{COMMAND-OUTPUT "'
8+
MARKER_END='"}}'
9+
10+
if [[ -z "${CI:-}" ]]; then
11+
# The `_wiki` directory is created in a previous GitHub Action step.
12+
# This 'if' block is intended to assist with local development activity.
13+
rm -rf _wiki/
14+
cp -r wiki/ _wiki/
15+
fi
16+
17+
grep -lrF "${MARKER_START}" _wiki | while read -r file_to_process; do
18+
echo "Processing markers in ${file_to_process}"
19+
20+
while IFS=$'\n' read -r line; do
21+
if [[ ${line} = ${MARKER_START}*${MARKER_END} ]]; then
22+
COMMAND="${line##"${MARKER_START}"}"
23+
COMMAND="${COMMAND%%"${MARKER_END}"}"
24+
25+
if [[ "${COMMAND}" != "phpcs "* ]] && [[ "${COMMAND}" != "phpcbf "* ]]; then
26+
echo >&2 " ERROR: refusing to run arbitrary command: ${COMMAND}"
27+
exit 1
28+
fi
29+
30+
#FIXME refuse to run commands with a semicolon / pipe / ampersand / sub-shell
31+
32+
echo >&2 " INFO: running: ${COMMAND}"
33+
(
34+
eval "${COMMAND}" </dev/null || true
35+
)
36+
else
37+
echo "${line}"
38+
fi
39+
done < "${file_to_process}" > build/temp.md
40+
41+
mv build/temp.md "${file_to_process}"
42+
done

wiki/Fixing-Errors-Automatically.md

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -36,78 +36,7 @@ PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
3636

3737
To automatically fix as many sniff violations as possible, use the `phpcbf` command instead of the `phpcs` command. While most of the PHPCS command line arguments can be used by PHPCBF, some are specific to reporting and will be ignored. Running PHPCBF with the `-h` or `--help` command line arguments will print a list of commands that PHPCBF will respond to. The output of `phpcbf -h` is shown below.
3838
```text
39-
Usage:
40-
phpcbf [options] <file|directory>
41-
42-
Scan targets:
43-
<file|directory> One or more files and/or directories to check, space separated.
44-
- Check STDIN instead of local files and directories.
45-
--stdin-path=<stdinPath> If processing STDIN, the file path that STDIN will be processed as.
46-
--file-list=<fileList> Check the files and/or directories which are defined in the file to which the
47-
path is provided (one per line).
48-
--filter=<filter> Check based on a predefined file filter. Use either the "GitModified" or
49-
"GitStaged" filter, or specify the path to a custom filter class.
50-
--ignore=<patterns> Ignore files based on a comma-separated list of patterns matching files and/or
51-
directories.
52-
--extensions=<extensions> Check files with the specified file extensions (comma-separated list).
53-
Defaults to php,inc/php,js,css.
54-
The type of the file can be specified using: ext/type; e.g. module/php,es/js.
55-
-l Check local directory only, no recursion.
56-
57-
Rule Selection Options:
58-
--standard=<standard> The name of, or the path to, the coding standard to use. Can be a
59-
comma-separated list specifying multiple standards. If no standard is
60-
specified, PHP_CodeSniffer will look for a [.]phpcs.xml[.dist] custom ruleset
61-
file in the current directory and those above it.
62-
--sniffs=<sniffs> A comma-separated list of sniff codes to limit the scan to. All sniffs must be
63-
part of the standard in use.
64-
--exclude=<sniffs> A comma-separated list of sniff codes to exclude from the scan. All sniffs
65-
must be part of the standard in use.
66-
67-
-i Show a list of installed coding standards.
68-
69-
Run Options:
70-
--bootstrap=<bootstrap> Run the specified file(s) before processing begins. A list of files can be
71-
provided, separated by commas.
72-
--parallel=<processes> The number of files to be checked simultaneously. Defaults to 1 (no parallel
73-
processing).
74-
If enabled, this option only takes effect if the PHP PCNTL (Process Control)
75-
extension is available.
76-
--suffix=<suffix> Write modified files to a filename using this suffix ("diff" and "patch" are
77-
not used in this mode).
78-
79-
-d <key[=value]> Set the [key] php.ini value to [value] or set to [true] if value is omitted.
80-
Note: only php.ini settings which can be changed at runtime are supported.
81-
82-
Reporting Options:
83-
--report-width=<reportWidth> How many columns wide screen reports should be. Set to "auto" to use current
84-
screen width, where supported.
85-
--basepath=<basepath> Strip a path from the front of file paths inside reports.
86-
87-
-w Include both warnings and errors (default).
88-
-n Do not include warnings. Shortcut for "--warning-severity=0".
89-
--severity=<severity> The minimum severity required to display an error or warning. Defaults to 5.
90-
--error-severity=<severity> The minimum severity required to display an error. Defaults to 5.
91-
--warning-severity=<severity> The minimum severity required to display a warning. Defaults to 5.
92-
93-
--ignore-annotations Ignore all "phpcs:..." annotations in code comments.
94-
--colors Use colors in screen output.
95-
--no-colors Do not use colors in screen output (default).
96-
-p Show progress of the run.
97-
-q Quiet mode; disables progress and verbose output.
98-
99-
Configuration Options:
100-
--encoding=<encoding> The encoding of the files being checked. Defaults to "utf-8".
101-
--tab-width=<tabWidth> The number of spaces each tab represents.
102-
103-
--runtime-set <key> <value> Set a configuration option to be applied to the current scan run only.
104-
105-
Miscellaneous Options:
106-
-h, -?, --help Print this help message.
107-
--version Print version information.
108-
-v Verbose output: Print processed files.
109-
-vv Verbose output: Print ruleset and token output.
110-
-vvv Verbose output: Print sniff processing information.
39+
{{COMMAND-OUTPUT "phpcbf --report-width=110 --no-colors -h"}}
11140
```
11241

11342
When using the PHPCBF command, you do not need to specify a report type. PHPCBF will automatically make changes to your source files:

wiki/Home.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,8 @@ Multiple coding standards can be used within PHP_CodeSniffer so that the one ins
1313
To check a file against the PEAR coding standard, simply specify the file's location.
1414

1515
```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-
--------------------------------------------------------------------------------
16+
$ phpcs path/to/code/myfile.php
17+
{{COMMAND-OUTPUT "phpcs --parallel=1 --basepath=build/wiki-code-samples --no-colors --standard=PEAR build/wiki-code-samples/path/to/code/myfile.php"}}
2818
```
2919

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

wiki/Usage.md

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -10,103 +10,7 @@
1010
Running PHP_CodeSniffer with the `-h` or `--help` command line arguments will print a list of commands that PHP_CodeSniffer will respond to. The output of `phpcs -h` is shown below.
1111

1212
```text
13-
Usage:
14-
phpcs [options] <file|directory>
15-
16-
Scan targets:
17-
<file|directory> One or more files and/or directories to check, space separated.
18-
- Check STDIN instead of local files and directories.
19-
--stdin-path=<stdinPath> If processing STDIN, the file path that STDIN will be processed as.
20-
--file-list=<fileList> Check the files and/or directories which are defined in the file to which the
21-
path is provided (one per line).
22-
--filter=<filter> Check based on a predefined file filter. Use either the "GitModified" or
23-
"GitStaged" filter, or specify the path to a custom filter class.
24-
--ignore=<patterns> Ignore files based on a comma-separated list of patterns matching files
25-
and/or directories.
26-
--extensions=<extensions> Check files with the specified file extensions (comma-separated list).
27-
Defaults to php,inc/php,js,css.
28-
The type of the file can be specified using: ext/type; e.g. module/php,es/js.
29-
-l Check local directory only, no recursion.
30-
31-
Rule Selection Options:
32-
--standard=<standard> The name of, or the path to, the coding standard to use. Can be a
33-
comma-separated list specifying multiple standards. If no standard is
34-
specified, PHP_CodeSniffer will look for a [.]phpcs.xml[.dist] custom ruleset
35-
file in the current directory and those above it.
36-
--sniffs=<sniffs> A comma-separated list of sniff codes to limit the scan to. All sniffs must
37-
be part of the standard in use.
38-
--exclude=<sniffs> A comma-separated list of sniff codes to exclude from the scan. All sniffs
39-
must be part of the standard in use.
40-
41-
-i Show a list of installed coding standards.
42-
-e Explain a standard by showing the names of all the sniffs it includes.
43-
--generator=<generator> Show documentation for a standard. Use either the "HTML", "Markdown" or
44-
"Text" generator.
45-
46-
Run Options:
47-
-a Run in interactive mode, pausing after each file.
48-
--bootstrap=<bootstrap> Run the specified file(s) before processing begins. A list of files can be
49-
provided, separated by commas.
50-
--cache[=<cacheFile>] Cache results between runs. Optionally, <cacheFile> can be provided to use a
51-
specific file for caching. Otherwise, a temporary file is used.
52-
--no-cache Do not cache results between runs (default).
53-
--parallel=<processes> The number of files to be checked simultaneously. Defaults to 1 (no parallel
54-
processing).
55-
If enabled, this option only takes effect if the PHP PCNTL (Process Control)
56-
extension is available.
57-
58-
-d <key[=value]> Set the [key] php.ini value to [value] or set to [true] if value is omitted.
59-
Note: only php.ini settings which can be changed at runtime are supported.
60-
61-
Reporting Options:
62-
--report=<report(s)> A comma-separated list of reports to print. Available reports: "full", "xml",
63-
"checkstyle", "csv", "json", "junit", "emacs", "source", "summary", "diff",
64-
"svnblame", "gitblame", "hgblame", "notifysend" or "performance".
65-
Or specify the path to a custom report class. By default, the "full" report
66-
is displayed.
67-
--report-file=<reportFile> Write the report to the specified file path.
68-
--report-<report>=<reportFile> Write the report specified in <report> to the specified file path.
69-
--report-width=<reportWidth> How many columns wide screen reports should be. Set to "auto" to use current
70-
screen width, where supported.
71-
--basepath=<basepath> Strip a path from the front of file paths inside reports.
72-
73-
-w Include both warnings and errors (default).
74-
-n Do not include warnings. Shortcut for "--warning-severity=0".
75-
--severity=<severity> The minimum severity required to display an error or warning. Defaults to 5.
76-
--error-severity=<severity> The minimum severity required to display an error. Defaults to 5.
77-
--warning-severity=<severity> The minimum severity required to display a warning. Defaults to 5.
78-
79-
-s Show sniff error codes in all reports.
80-
--ignore-annotations Ignore all "phpcs:..." annotations in code comments.
81-
--colors Use colors in screen output.
82-
--no-colors Do not use colors in screen output (default).
83-
-p Show progress of the run.
84-
-q Quiet mode; disables progress and verbose output.
85-
-m Stop error messages from being recorded. This saves a lot of memory but stops
86-
many reports from being used.
87-
88-
Configuration Options:
89-
--encoding=<encoding> The encoding of the files being checked. Defaults to "utf-8".
90-
--tab-width=<tabWidth> The number of spaces each tab represents.
91-
92-
Default values for a selection of options can be stored in a user-specific CodeSniffer.conf configuration
93-
file.
94-
This applies to the following options: "default_standard", "report_format", "tab_width", "encoding",
95-
"severity", "error_severity", "warning_severity", "show_warnings", "report_width", "show_progress", "quiet",
96-
"colors", "cache", "parallel", "installed_paths", "php_version", "ignore_errors_on_exit",
97-
"ignore_warnings_on_exit".
98-
--config-show Show the configuration options which are currently stored in the applicable
99-
CodeSniffer.conf file.
100-
--config-set <key> <value> Save a configuration option to the CodeSniffer.conf file.
101-
--config-delete <key> Delete a configuration option from the CodeSniffer.conf file.
102-
--runtime-set <key> <value> Set a configuration option to be applied to the current scan run only.
103-
104-
Miscellaneous Options:
105-
-h, -?, --help Print this help message.
106-
--version Print version information.
107-
-v Verbose output: Print processed files.
108-
-vv Verbose output: Print ruleset and token output.
109-
-vvv Verbose output: Print sniff processing information.
13+
{{COMMAND-OUTPUT "phpcs --report-width=110 --no-colors -h"}}
11014
```
11115

11216
> [!NOTE]

0 commit comments

Comments
 (0)