Skip to content

Commit 6f17427

Browse files
authored
Merge pull request #119 from jrfnl/feature/use-phpcs
Implement CS checking based on the WPCliCS ruleset
2 parents 8faae21 + e1e54fd commit 6f17427

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

.distignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
.travis.yml
77
behat.yml
88
circle.yml
9+
phpcs.xml.dist
10+
phpunit.xml.dist
911
bin/
1012
features/
1113
utils/

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ vendor/
77
*.swp
88
composer.lock
99
*.log
10+
phpunit.xml
11+
phpcs.xml
12+
.phpcs.xml

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"wp-cli/db-command": "^1.3 || ^2",
1919
"wp-cli/entity-command": "^1.3 || ^2",
2020
"wp-cli/extension-command": "^1.2 || ^2",
21-
"wp-cli/wp-cli-tests": "^2.0.7"
21+
"wp-cli/wp-cli-tests": "^2.1"
2222
},
2323
"config": {
2424
"process-timeout": 7200,

phpcs.xml.dist

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-search-replace">
3+
<description>Custom ruleset for WP-CLI search-replace-command</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+
<!-- Can't be helped as WP itself uses these functions for the data being adjusted. -->
33+
<exclude name="WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize"/>
34+
<exclude name="WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize"/>
35+
</rule>
36+
37+
<!--
38+
#############################################################################
39+
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
40+
#############################################################################
41+
-->
42+
43+
<!-- For help understanding the `testVersion` configuration setting:
44+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
45+
<config name="testVersion" value="5.4-"/>
46+
47+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
48+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
49+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
50+
<properties>
51+
<property name="prefixes" type="array">
52+
<element value="WP_CLI\Search"/><!-- Namespaces. -->
53+
<element value="wpcli_search"/><!-- Global variables and such. -->
54+
</property>
55+
</properties>
56+
</rule>
57+
58+
<!-- Exclude existing classes and namespaces from the prefix rule as it would break BC to prefix them now. -->
59+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
60+
<exclude-pattern>*/src/Search_Replace_Command\.php$</exclude-pattern>
61+
</rule>
62+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">
63+
<exclude-pattern>*/src/WP_CLI/SearchReplacer\.php$</exclude-pattern>
64+
</rule>
65+
66+
<!-- Allow for some MySQL native non-snake-case properties.
67+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#mixed-case-property-name-exceptions
68+
Related: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1623 -->
69+
<rule ref="WordPress.NamingConventions.ValidVariableName">
70+
<properties>
71+
<property name="customPropertiesWhitelist" type="array">
72+
<element value="Key"/>
73+
<element value="Field"/>
74+
<element value="Type"/>
75+
</property>
76+
</properties>
77+
</rule>
78+
79+
</ruleset>

src/Search_Replace_Command.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ public function __invoke( $args, $assoc_args ) {
200200
$search_regex .= $old;
201201
$search_regex .= $this->regex_delimiter;
202202
$search_regex .= $this->regex_flags;
203+
204+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Preventing a warning when testing the regex.
203205
if ( false === @preg_match( $search_regex, '' ) ) {
204206
if ( $default_regex_delimiter ) {
205207
$flags_msg = $this->regex_flags ? "flags '$this->regex_flags'" : 'no flags';
@@ -236,6 +238,7 @@ public function __invoke( $args, $assoc_args ) {
236238
}
237239
}
238240
$export_insert_size = WP_CLI\Utils\get_flag_value( $assoc_args, 'export_insert_size', 50 );
241+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- See the code, this is deliberate.
239242
if ( (int) $export_insert_size == $export_insert_size && $export_insert_size > 0 ) {
240243
$this->export_insert_size = $export_insert_size;
241244
}
@@ -691,7 +694,7 @@ private static function esc_like( $old ) {
691694
// 4.0
692695
$old = $wpdb->esc_like( $old );
693696
} else {
694-
// 3.9 or less
697+
// phpcs:ignore WordPress.WP.DeprecatedFunctions.like_escapeFound -- BC-layer for WP 3.9 or less.
695698
$old = like_escape( esc_sql( $old ) ); // Note: this double escaping is actually necessary, even though `esc_like()` will be used in a `prepare()`.
696699
}
697700

src/WP_CLI/SearchReplacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
136136
if ( $serialised ) {
137137
return serialize( $data );
138138
}
139-
} catch ( Exception $error ) {
139+
} catch ( Exception $error ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Deliberally empty.
140140

141141
}
142142

0 commit comments

Comments
 (0)