Skip to content

Commit 8c7a2e7

Browse files
authored
Merge pull request #1690 from WordPress-Coding-Standards/develop
Release version 2.1.0
2 parents c9eaada + 975613f commit 8c7a2e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1643
-236
lines changed

.phpcs.xml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
<arg value="sp"/>
99
<arg name="extensions" value="php"/>
10+
<arg name="basepath" value="."/>
11+
<arg name="parallel" value="8"/>
1012

1113
<exclude-pattern>/bin/class-ruleset-test.php</exclude-pattern>
1214
<!-- Exclude Composer vendor directory. -->
1315
<exclude-pattern>*/vendor/*</exclude-pattern>
1416

15-
<rule ref="WordPress-Extra">
17+
<rule ref="WordPress">
1618
<exclude name="WordPress.Files.FileName"/>
1719
<exclude name="WordPress.NamingConventions.ValidVariableName"/>
1820
<exclude name="WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition"/>
1921
</rule>
2022

21-
<rule ref="WordPress-Docs"/>
22-
2323
<!-- Enforce PSR1 compatible namespaces. -->
2424
<rule ref="PSR1.Classes.ClassDeclaration"/>
2525

.travis.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
sudo: false
2-
31
dist: trusty
42

53
cache:
@@ -21,7 +19,7 @@ php:
2119
- 7.1
2220
- 7.2
2321
- 7.3
24-
- nightly
22+
- "7.4snapshot"
2523

2624
env:
2725
# `master` is now 3.x.
@@ -42,7 +40,7 @@ matrix:
4240

4341
allow_failures:
4442
# Allow failures for unstable builds.
45-
- php: nightly
43+
- php: "7.4snapshot"
4644

4745
before_install:
4846
# Speed up build time by disabling Xdebug.
@@ -60,12 +58,20 @@ before_install:
6058
# The above require already does the install.
6159
$(pwd)/vendor/bin/phpcs --config-set installed_paths $(pwd)
6260
fi
61+
# Download PHPUnit 7.x for builds on PHP >= 7.2 as the PHPCS
62+
# test suite is currently not compatible with PHPUnit 8.x.
63+
- if [[ ${TRAVIS_PHP_VERSION:0:3} > "7.1" ]]; then wget -P $PHPUNIT_DIR https://phar.phpunit.de/phpunit-7.phar && chmod +x $PHPUNIT_DIR/phpunit-7.phar; fi
6364

6465
script:
6566
# Lint the PHP files against parse errors.
6667
- if [[ "$LINT" == "1" ]]; then if find . -path ./vendor -prune -o -path ./bin -prune -o -name "*.php" -exec php -l {} \; | grep "^[Parse error|Fatal error]"; then exit 1; fi; fi
6768
# Run the unit tests.
68-
- phpunit --filter WordPress --bootstrap="$(pwd)/vendor/squizlabs/php_codesniffer/tests/bootstrap.php" $(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php
69+
- |
70+
if [[ ${TRAVIS_PHP_VERSION:0:3} > "7.1" ]]; then
71+
php $PHPUNIT_DIR/phpunit-7.phar --filter WordPress --bootstrap="$(pwd)/vendor/squizlabs/php_codesniffer/tests/bootstrap.php" $(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php
72+
else
73+
phpunit --filter WordPress --bootstrap="$(pwd)/vendor/squizlabs/php_codesniffer/tests/bootstrap.php" $(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php
74+
fi
6975
# Test for fixer conflicts by running the auto-fixers of the complete WPCS over the test case files.
7076
# This is not an exhaustive test, but should give an early indication for typical fixer conflicts.
7177
# For the first run, the exit code will be 1 (= all fixable errors fixed).

CHANGELOG.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,82 @@ This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a
88

99
_No documentation available about unreleased changes as of yet._
1010

11+
## [2.1.0] - 2019-04-08
12+
13+
### Added
14+
- New `WordPress.PHP.IniSet` sniff to the `WordPress-Extra` ruleset.
15+
This sniff will detect calls to `ini_set()` and `ini_alter()` and warn against their use as changing configuration values at runtime leads to an unpredictable runtime environment, which can result in conflicts between core/plugins/themes.
16+
- The sniff will not throw notices about a very limited set of "safe" ini directives.
17+
- For a number of ini directives for which there are alternative, non-conflicting ways to achieve the same available, the sniff will throw an `error` and advise using the alternative.
18+
- `doubleval()`, `count()` and `sizeof()` to `Sniff::$unslashingSanitizingFunctions` property.
19+
While `count()` and its alias `sizeof()`, don't actually unslash or sanitize, the output of these functions is safe to use without unslashing or sanitizing.
20+
This affects the `WordPress.Security.ValidatedSanitizedInput` and the `WordPress.Security.NonceVerification` sniffs.
21+
- The new WP 5.1 `WP_UnitTestCase_Base` class to the `Sniff::$test_class_whitelist` property.
22+
- New `Sniff::get_array_access_keys()` utility method to retrieve all array keys for a variable using multi-level array access.
23+
- New `Sniff::is_class_object_call()`, `Sniff::is_token_namespaced()` utility methods.
24+
These should help make the checking of whether or not a function call is a global function, method call or a namespaced function call more consistent.
25+
This also implements allowing for the [namespace keyword being used as an operator](https://www.php.net/manual/en/language.namespaces.nsconstants.php#example-258).
26+
- New `Sniff::is_in_function_call()` utility method to facilitate checking whether a token is (part of) a parameter passed to a specific (set of) function(s).
27+
- New `Sniff::is_in_type_test()` utility method to determine if a variable is being type tested, along with a `Sniff::$typeTestFunctions` property containing the names of the functions this applies to.
28+
- New `Sniff::is_in_array_comparison()` utility method to determine if a variable is (part of) a parameter in an array-value comparison, along with a `Sniff::$arrayCompareFunctions` property containing the names of the relevant functions.
29+
- New `Sniff::$arrayWalkingFunctions` property containing the names of array functions which apply a callback to the array, but don't change the array by reference.
30+
- New `Sniff::$unslashingFunctions` property containing the names of functions which unslash data passed to them and return the unslashed result.
31+
32+
### Changed
33+
- Moved the `WordPress.PHP.StrictComparisons`, `WordPress.PHP.StrictInArray` and the `WordPress.CodeAnalysis.AssignmentInCondition` sniff from the `WordPress-Extra` to the `WordPress-Core` ruleset.
34+
- The `Squiz.Commenting.InlineComment.SpacingAfter` error is no longer included in the `WordPress-Docs` ruleset.
35+
- The default value for `minimum_supported_wp_version`, as used by a [number of sniffs detecting usage of deprecated WP features](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#minimum-wp-version-to-check-for-usage-of-deprecated-functions-classes-and-function-parameters), has been updated to `4.8`.
36+
- The `WordPress.WP.DeprecatedFunctions` sniff will now detect functions deprecated in WP 5.1.
37+
- The `WordPress.Security.NonceVerification` sniff now allows for variable type testing, comparisons, unslashing and sanitization before the nonce check. A nonce check within the same scope, however, is still required.
38+
- The `WordPress.Security.ValidatedSanitizedInput` sniff now allows for using a superglobal in an array-value comparison without sanitization, same as when the superglobal is used in a scalar value comparison.
39+
- `WordPress.NamingConventions.PrefixAllGlobals`: some of the error messages have been made more explicit.
40+
- The error messages for the `WordPress.Security.ValidatedSanitizedInput` sniff will now contain information on the index keys accessed.
41+
- The error message for the `WordPress.Security.ValidatedSanitizedInput.InputNotValidated` has been reworded to make it more obvious what the actual issue being reported is.
42+
- The error message for the `WordPress.Security.ValidatedSanitizedInput.MissingUnslash` has been reworded.
43+
- The `Sniff::is_comparison()` method now has a new `$include_coalesce` parameter to allow for toggling whether the null coalesce operator should be seen as a comparison operator. Defaults to `true`.
44+
- All sniffs are now also being tested against PHP 7.4 (unstable) for consistent sniff results.
45+
- The recommended version of the suggested DealerDirect PHPCS Composer plugin is now `^0.5.0`.
46+
- Various minor code tweaks and clean up.
47+
48+
### Removed
49+
- `ini_set` and `ini_alter` from the list of functions detected by the `WordPress.PHP.DiscouragedFunctions` sniff.
50+
These are now covered via the new `WordPress.PHP.IniSet` sniff.
51+
- `in_array()` and `array_key_exists()` from the list of `Sniff::$sanitizingFunctions`. These are now handled differently.
52+
53+
### Fixed
54+
- The `WordPress.NamingConventions.PrefixAllGlobals` sniff would underreport when global functions would be autoloaded via a Composer autoload `files` configuration.
55+
- The `WordPress.Security.EscapeOutput` sniff will now recognize `map_deep()` for escaping the values in an array via a callback to an output escaping function. This should prevent false positives.
56+
- The `WordPress.Security.NonceVerification` sniff will no longer inadvertently allow for a variable to be sanitized without a nonce check within the same scope.
57+
- The `WordPress.Security.ValidatedSanitizedInput` sniff will no longer throw errors when a variable is only being type tested.
58+
- The `WordPress.Security.ValidatedSanitizedInput` sniff will now correctly recognize the null coalesce (PHP 7.0) and null coalesce equal (PHP 7.4) operators and will now throw errors for missing unslashing and sanitization where relevant.
59+
- The `WordPress.WP.AlternativeFunctions` sniff will no longer recommend using the WP_FileSystem when PHP native input streams, like `php://input`, or the PHP input stream constants are being read or written to.
60+
- The `WordPress.WP.AlternativeFunctions` sniff will no longer report on usage of the `curl_version()` function.
61+
- The `WordPress.WP.CronInterval` sniff now has improved function recognition which should lower the chance of false positives.
62+
- The `WordPress.WP.EnqueuedResources` sniff will no longer throw false positives for inline jQuery code trying to access a stylesheet link tag.
63+
- Various bugfixes for the `Sniff::has_nonce_check()` method:
64+
- The method will no longer incorrectly identify methods/namespaced functions mirroring the name of WP native nonce verification functions as if they were the global functions.
65+
This will prevent some false negatives.
66+
- The method will now skip over nested closed scopes, such as closures and anonymous classes. This should prevent some false negatives for nonce verification being done while not in the correct scope.
67+
68+
These fixes affect the `WordPress.Security.NonceVerification` sniff.
69+
- The `Sniff::is_in_isset_or_empty()` method now also checks for usage of `array_key_exist()` and `key_exists()` and will regard these as correct ways to validate a variable.
70+
This should prevent false positives for the `WordPress.Security.ValidatedSanitizedInput` and the `WordPress.Security.NonceVerification` sniffs.
71+
- Various bugfixes for the `Sniff::is_sanitized()` method:
72+
- The method presumed the WordPress coding style regarding code layout, which could lead to false positives.
73+
- The method will no longer incorrectly identify methods/namespaced functions mirroring the name of WP/PHP native unslashing/sanitization functions as if they were the global functions.
74+
This will prevent some false negatives.
75+
- The method will now recognize `map_deep()` for sanitizing an array via a callback to a sanitization function. This should prevent false positives.
76+
- The method will now recognize `stripslashes_deep()` and `stripslashes_from_strings_only()` as valid unslashing functions. This should prevent false positives.
77+
All these fixes affect both the `WordPress.Security.ValidatedSanitizedInput` and the `WordPress.Security.NonceVerification` sniff.
78+
- Various bugfixes for the `Sniff::is_validated()` method:
79+
- The method did not verify correctly whether a variable being validated was the same variable as later used which could lead to false negatives.
80+
- The method did not verify correctly whether a variable being validated had the same array index keys as the variable as later used which could lead to both false negatives as well as false positives.
81+
- The method now also checks for usage of `array_key_exist()` and `key_exists()` and will regard these as correct ways to validate a variable. This should prevent some false positives.
82+
- The methods will now recognize the null coalesce and the null coalesce equal operators as ways to validate a variable. This prevents some false positives.
83+
The results from the `WordPress.Security.ValidatedSanitizedInput` sniff should be more accurate because of these fixes.
84+
- A potential "Undefined index" notice from the `Sniff::is_assignment()` method.
85+
86+
1187
## [2.0.0] - 2019-01-16
1288

1389
### Important information about this release:
@@ -994,6 +1070,7 @@ See the comparison for full list.
9941070
Initial tagged release.
9951071

9961072
[Unreleased]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/master...HEAD
1073+
[2.1.0]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/2.0.0...2.1.0
9971074
[2.0.0]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/2.0.0-RC1...2.0.0
9981075
[2.0.0-RC1]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/1.2.1...2.0.0-RC1
9991076
[1.2.1]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/1.2.0...1.2.1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ When installing the WordPress Coding Standards as a dependency in a larger proje
8282

8383
There are two actively maintained Composer plugins which can handle the registration of standards with PHP_CodeSniffer for you:
8484
* [composer-phpcodesniffer-standards-plugin](https://github.com/higidi/composer-phpcodesniffer-standards-plugin)
85-
* [phpcodesniffer-composer-installer](https://github.com/DealerDirect/phpcodesniffer-composer-installer):"^0.4.3"
85+
* [phpcodesniffer-composer-installer](https://github.com/DealerDirect/phpcodesniffer-composer-installer):"^0.5.0"
8686

8787
It is strongly suggested to `require` one of these plugins in your project to handle the registration of external standards with PHPCS for you.
8888

WordPress-Core/ruleset.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,18 @@
392392
<rule ref="Squiz.PHP.DisallowMultipleAssignments"/>
393393
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>
394394

395+
<!-- Rule: Unless absolutely necessary, loose comparisons should not be used,
396+
as their behaviour can be misleading. -->
397+
<rule ref="WordPress.PHP.StrictComparisons"/>
398+
<rule ref="WordPress.PHP.StrictInArray"/>
399+
400+
<!-- Rule: Assignments must not be placed in placed in conditionals.
401+
Note: sniff is a duplicate of upstream. Can be removed once minimum PHPCS requirement has gone up.
402+
https://github.com/squizlabs/PHP_CodeSniffer/pull/1594
403+
Update: the "assignment in ternary" part of the sniff is currently not yet covered in
404+
the upstream version. This needs to be pulled first before we can defer to upstream. -->
405+
<rule ref="WordPress.CodeAnalysis.AssignmentInCondition"/>
406+
395407
<!-- Rule: In a switch statement... If a case contains a block, then falls through
396408
to the next block, this must be explicitly commented. -->
397409
<!-- Covered by the PSR2.ControlStructures.SwitchDeclaration sniff. -->

WordPress-Docs/ruleset.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
<exclude name="Squiz.Commenting.InlineComment.DocBlock"/>
7272
<!-- Excluded to allow /* translators: ... */ comments -->
7373
<exclude name="Squiz.Commenting.InlineComment.NotCapital"/>
74+
<!-- WP handbook doesn't clarify one way or another, so ignore -->
75+
<exclude name="Squiz.Commenting.InlineComment.SpacingAfter"/>
7476

7577
<!-- Not in Inline Docs standard, and a code smell -->
7678
<exclude name="Squiz.Commenting.LongConditionClosingComment"/>
@@ -86,7 +88,7 @@
8688
<exclude name="Squiz.Commenting.VariableComment.VarOrder"/>
8789
</rule>
8890

89-
<rule ref="Generic.Commenting">
91+
<rule ref="Generic.Commenting.DocComment">
9092
<!-- WP has different alignment of tag values -->
9193
<exclude name="Generic.Commenting.DocComment.TagValueIndent"/>
9294
<!-- WP has a different prefered order of tags -->
@@ -103,8 +105,5 @@
103105
<exclude name="Generic.Commenting.DocComment.SpacingBeforeShort"/>
104106
<!-- Exclude to allow duplicate hooks to be documented -->
105107
<exclude name="Generic.Commenting.DocComment.ContentBeforeClose"/>
106-
107-
<!-- WP allows @todo's in comments -->
108-
<exclude name="Generic.Commenting.Todo"/>
109108
</rule>
110109
</ruleset>

WordPress-Extra/ruleset.xml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424
</properties>
2525
</rule>
2626

27-
<!-- Duplicate of upstream. Can be removed once minimum PHPCS requirement has gone up.
28-
https://github.com/squizlabs/PHP_CodeSniffer/pull/1594
29-
Note: the "assignment in ternary" part of the sniff is currently not yet covered in
30-
the upstream version. This needs to be pulled first before we can defer to upstream. -->
31-
<rule ref="WordPress.CodeAnalysis.AssignmentInCondition"/>
32-
3327
<!-- More generic PHP best practices.
3428
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/607 -->
3529
<rule ref="Squiz.PHP.NonExecutableCode"/>
@@ -109,14 +103,9 @@
109103
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/26 -->
110104
<rule ref="WordPress.WP.GlobalVariablesOverride"/>
111105

112-
<!-- Encourage the use of strict ( === and !== ) comparisons.
113-
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/242 -->
114-
<rule ref="WordPress.PHP.StrictComparisons"/>
115-
116-
<!-- Check that in_array() and array_search() use strict comparisons.
117-
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/399
118-
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/503 -->
119-
<rule ref="WordPress.PHP.StrictInArray"/>
106+
<!-- Detect incorrect or risky use of the `ini_set()` function.
107+
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1447 -->
108+
<rule ref="WordPress.PHP.IniSet"/>
120109

121110
<!-- Check enqueue and register styles and scripts to have version and in_footer parameters explicitly set.
122111
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1146 -->

WordPress/AbstractFunctionRestrictionsSniff.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,15 @@ public function process_token( $stackPtr ) {
213213
public function is_targetted_token( $stackPtr ) {
214214

215215
// Exclude function definitions, class methods, and namespaced calls.
216-
if ( \T_STRING === $this->tokens[ $stackPtr ]['code'] && isset( $this->tokens[ ( $stackPtr - 1 ) ] ) ) {
216+
if ( \T_STRING === $this->tokens[ $stackPtr ]['code'] ) {
217+
if ( $this->is_class_object_call( $stackPtr ) === true ) {
218+
return false;
219+
}
220+
221+
if ( $this->is_token_namespaced( $stackPtr ) === true ) {
222+
return false;
223+
}
224+
217225
$prev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
218226

219227
if ( false !== $prev ) {
@@ -222,21 +230,11 @@ public function is_targetted_token( $stackPtr ) {
222230
\T_FUNCTION => \T_FUNCTION,
223231
\T_CLASS => \T_CLASS,
224232
\T_AS => \T_AS, // Use declaration alias.
225-
\T_DOUBLE_COLON => \T_DOUBLE_COLON,
226-
\T_OBJECT_OPERATOR => \T_OBJECT_OPERATOR,
227233
);
228234

229235
if ( isset( $skipped[ $this->tokens[ $prev ]['code'] ] ) ) {
230236
return false;
231237
}
232-
233-
// Skip namespaced functions, ie: \foo\bar() not \bar().
234-
if ( \T_NS_SEPARATOR === $this->tokens[ $prev ]['code'] ) {
235-
$pprev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $prev - 1 ), null, true );
236-
if ( false !== $pprev && \T_STRING === $this->tokens[ $pprev ]['code'] ) {
237-
return false;
238-
}
239-
}
240238
}
241239

242240
return true;

0 commit comments

Comments
 (0)