Skip to content

Commit 6ea47cf

Browse files
Copilotswissspidy
andcommitted
Use template for phpcs.xml.dist with customizable prefix
Co-authored-by: swissspidy <[email protected]>
1 parent 92e6abe commit 6ea47cf

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

features/scaffold-package.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Feature: Scaffold WP-CLI commands
2323
"""
2424
WP_CLI_CS
2525
"""
26+
And the {PACKAGE_PATH}/local/wp-cli/foo/phpcs.xml.dist file should contain:
27+
"""
28+
wpcli_foo
29+
"""
2630
And the {PACKAGE_PATH}/local/wp-cli/foo/composer.json file should exist
2731
And the {PACKAGE_PATH}/local/wp-cli/foo/composer.json file should contain:
2832
"""

src/ScaffoldPackageCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public function package( $args, $assoc_args ) {
8787
WP_CLI::error( "'{$assoc_args['name']}' is an invalid package name. Package scaffold expects '<author>/<package>'." );
8888
}
8989

90+
// Generate a slug from the package name for use in prefixes.
91+
$assoc_args['package_name_slug'] = str_replace( '-', '_', $bits[1] );
92+
9093
if ( ! empty( $assoc_args['dir'] ) ) {
9194
$package_dir = $assoc_args['dir'];
9295
} else {
@@ -118,7 +121,7 @@ public function package( $args, $assoc_args ) {
118121
"{$package_dir}/.gitignore" => file_get_contents( "{$package_root}/.gitignore" ),
119122
"{$package_dir}/.editorconfig" => file_get_contents( "{$package_root}/.editorconfig" ),
120123
"{$package_dir}/.distignore" => file_get_contents( "{$package_root}/.distignore" ),
121-
"{$package_dir}/phpcs.xml.dist" => file_get_contents( "{$package_root}/phpcs.xml.dist" ),
124+
"{$package_dir}/phpcs.xml.dist" => Utils\mustache_render( "{$template_path}/phpcs.xml.dist.mustache", $assoc_args ),
122125
"{$package_dir}/CONTRIBUTING.md" => file_get_contents( "{$package_root}/CONTRIBUTING.md" ),
123126
"{$package_dir}/wp-cli.yml" => $wp_cli_yml,
124127
"{$package_dir}/hello-world-command.php" => Utils\mustache_render( "{$template_path}/hello-world-command.mustache", $assoc_args ),

templates/phpcs.xml.dist.mustache

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-{{package_name_slug}}">
3+
<description>Custom ruleset for WP-CLI {{name}}</description>
4+
5+
<!--
6+
#############################################################################
7+
COMMAND LINE ARGUMENTS
8+
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
9+
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
10+
#############################################################################
11+
-->
12+
13+
<!-- What to scan. -->
14+
<file>.</file>
15+
16+
<!-- Show progress. -->
17+
<arg value="p"/>
18+
19+
<!-- Strip the filepaths down to the relevant bit. -->
20+
<arg name="basepath" value="./"/>
21+
22+
<!-- Check up to 8 files simultaneously. -->
23+
<arg name="parallel" value="8"/>
24+
25+
<!--
26+
#############################################################################
27+
USE THE WP_CLI_CS RULESET
28+
#############################################################################
29+
-->
30+
31+
<rule ref="WP_CLI_CS"/>
32+
33+
<!--
34+
#############################################################################
35+
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
36+
#############################################################################
37+
-->
38+
39+
<!-- For help understanding the `testVersion` configuration setting:
40+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41+
<config name="testVersion" value="7.2-"/>
42+
43+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
44+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
45+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
46+
<properties>
47+
<property name="prefixes" type="array">
48+
<element value="WP_CLI"/><!-- Namespaces. -->
49+
<element value="wpcli_{{package_name_slug}}"/><!-- Global variables and such. -->
50+
</property>
51+
</properties>
52+
</rule>
53+
54+
</ruleset>

0 commit comments

Comments
 (0)