Skip to content

Commit af81d64

Browse files
authored
Add allowWordPressPassByRefFunctions option (#69)
* Test: Add test for allowWordPressPassByRefFunctions * Add allowWordPressPassByRefFunctions option * Add wp_cache_get to WP reference functions
1 parent 0c10516 commit af81d64

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ The available options are as follows:
7070
- `validUndefinedVariableNames` (string, default `null`): a space-separated list of names of placeholder variables that you want to ignore from undefined variable warnings. For example, to ignore the variables `$post` and `$undefined`, this could be set to `'post undefined'`.
7171
- `allowUnusedForeachVariables` (bool, default `false`): if set to true, unused keys or values created by the `as` statement in a `foreach` loop will never be marked as unused.
7272
- `sitePassByRefFunctions` (string, default `null`): a list of custom functions which pass in variables to be initialized by reference (eg `preg_match()`) and therefore should not require those variables to be defined ahead of time. The list is space separated and each entry is of the form `functionName:1,2`. The function name comes first followed by a colon and a comma-separated list of argument numbers (starting from 1) which should be considered variable definitions. The special value `...` in the arguments list will cause all arguments after the last number to be considered variable definitions.
73+
- `allowWordPressPassByRefFunctions` (bool, default `false`): if set to true, a list of common WordPress pass-by-reference functions will be added to the list of PHP ones so that passing undefined variables to these functions (to be initialized by reference) will be allowed.
7374

7475
To set these these options, you must use XML in your ruleset. For details, see the [phpcs customizable sniff properties page](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties). Here is an example that ignores all variables that start with an underscore:
7576

VariableAnalysis/Lib/Constants.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ public static function getPassByReferenceFunctions() {
238238
];
239239
}
240240

241+
public static function getWordPressPassByReferenceFunctions() {
242+
return [
243+
'wp_parse_str' => [2],
244+
'wp_cache_get' => [4],
245+
];
246+
}
247+
241248
/**
242249
* A regexp for matching variable names in double-quoted strings.
243250
*/

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class VariableAnalysisSniff implements Sniff {
3232
*/
3333
public $sitePassByRefFunctions = null;
3434

35+
/**
36+
* If set, allows common WordPress pass-by-reference functions in addition to
37+
* the standard PHP ones.
38+
*/
39+
public $allowWordPressPassByRefFunctions = false;
40+
3541
/**
3642
* Allows exceptions in a catch block to be unused without provoking unused-var warning.
3743
* Set generic.codeanalysis.variableanalysis.allowUnusedCaughtExceptions to a true value.
@@ -89,13 +95,15 @@ public function register() {
8995

9096
private function getPassByReferenceFunction($functionName) {
9197
$passByRefFunctions = Constants::getPassByReferenceFunctions();
92-
// Magic to modfy $passByRefFunctions with any site-specific settings.
9398
if (!empty($this->sitePassByRefFunctions)) {
9499
foreach (preg_split('/\s+/', trim($this->sitePassByRefFunctions)) as $line) {
95100
list ($function, $args) = explode(':', $line);
96101
$passByRefFunctions[$function] = explode(',', $args);
97102
}
98103
}
104+
if ($this->allowWordPressPassByRefFunctions) {
105+
$passByRefFunctions = array_merge($passByRefFunctions, Constants::getWordPressPassByReferenceFunctions());
106+
}
99107
return isset($passByRefFunctions[$functionName]) ? $passByRefFunctions[$functionName] : null;
100108
}
101109

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public function testFunctionWithReferenceWarnings() {
262262
46,
263263
59,
264264
60,
265+
64,
265266
];
266267
$this->assertEquals($expectedWarnings, $lines);
267268
}
@@ -287,6 +288,34 @@ public function testFunctionWithReferenceWarningsAllowsCustomFunctions() {
287288
39,
288289
40,
289290
46,
291+
64,
292+
];
293+
$this->assertEquals($expectedWarnings, $lines);
294+
}
295+
296+
public function testFunctionWithReferenceWarningsAllowsWordPressFunctionsIfSet() {
297+
$fixtureFile = $this->getFixture('FunctionWithReferenceFixture.php');
298+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
299+
$phpcsFile->ruleset->setSniffProperty(
300+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
301+
'allowWordPressPassByRefFunctions',
302+
'true'
303+
);
304+
$phpcsFile->process();
305+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
306+
$expectedWarnings = [
307+
8,
308+
20,
309+
32,
310+
33,
311+
34,
312+
36,
313+
37,
314+
39,
315+
40,
316+
46,
317+
59,
318+
60,
290319
];
291320
$this->assertEquals($expectedWarnings, $lines);
292321
}

VariableAnalysis/Tests/CodeAnalysis/fixtures/FunctionWithReferenceFixture.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ function function_with_ignored_reference_call() {
5959
my_reference_function($foo, $baz, $bip);
6060
another_reference_function($foo, $foo2, $foo3);
6161
}
62+
63+
function function_with_wordpress_reference_calls() {
64+
wp_parse_str('foo=bar', $vars);
65+
}

0 commit comments

Comments
 (0)