Skip to content

Commit 0c10516

Browse files
authored
Document site pass by ref (#71)
Document `sitePassByRefFunctions` option
1 parent 6d66956 commit 0c10516

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ The available options are as follows:
6969
- `ignoreUnusedRegexp` (string, default `null`): a PHP regexp string (note that this requires explicit delimiters) for variables that you want to ignore from unused variable warnings. For example, to ignore the variables `$_junk` and `$_unused`, this could be set to `'/^_/'`.
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.
72+
- `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.
7273

7374
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:
7475

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ class VariableAnalysisSniff implements Sniff {
2323
private $scopes = [];
2424

2525
/**
26-
* Allows an install to extend the list of known pass-by-reference functions
27-
* by defining generic.codeanalysis.variableanalysis.sitePassByRefFunctions.
26+
* An associative array of additional pass-by-reference functions. The keys
27+
* are the function names, and the values are indexed arrays containing the
28+
* argument numbers (starting from 1) of arguments which should be considered
29+
* variable definitions. The special value `'...'` in the arguments array
30+
* will cause all arguments after the last number to be considered variable
31+
* definitions.
2832
*/
2933
public $sitePassByRefFunctions = null;
3034

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,33 @@ public function testFunctionWithReferenceWarnings() {
249249
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
250250
$phpcsFile->process();
251251
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
252+
$expectedWarnings = [
253+
8,
254+
20,
255+
32,
256+
33,
257+
34,
258+
36,
259+
37,
260+
39,
261+
40,
262+
46,
263+
59,
264+
60,
265+
];
266+
$this->assertEquals($expectedWarnings, $lines);
267+
}
268+
269+
public function testFunctionWithReferenceWarningsAllowsCustomFunctions() {
270+
$fixtureFile = $this->getFixture('FunctionWithReferenceFixture.php');
271+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
272+
$phpcsFile->ruleset->setSniffProperty(
273+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
274+
'sitePassByRefFunctions',
275+
'my_reference_function:2,3 another_reference_function:2,...'
276+
);
277+
$phpcsFile->process();
278+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
252279
$expectedWarnings = [
253280
8,
254281
20,

VariableAnalysis/Tests/CodeAnalysis/fixtures/FunctionWithReferenceFixture.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ function function_with_pass_by_reference_calls() {
5353
function function_with_pass_by_ref_assign_only_arg(&$return_value) {
5454
$return_value = 42;
5555
}
56+
57+
function function_with_ignored_reference_call() {
58+
$foo = 'bar';
59+
my_reference_function($foo, $baz, $bip);
60+
another_reference_function($foo, $foo2, $foo3);
61+
}

0 commit comments

Comments
 (0)