Skip to content

Allow for functions which set variables in the current scope #172

Open
@jrfnl

Description

@jrfnl

In PHP there are a number of functions which can set variables in the current scope. Sometimes by reference to a passed parameter (first examples), sometimes just plainly in the current scope (second examples).

While the first set of examples is handled correctly by the sniff, the second set of examples is not.

Now, I'm not saying it will be easy to handle this or that I have a ready-made solution for this, but I figured opening an issue as a reminder to have a look at this at some point would be a good idea nevertheless.

1. Functions setting a variable by reference.

	parse_str($str, $output);
	echo $output['first']; 

	preg_match('`.*`', $first, $matches);
	var_dump($matches);

The sniff correctly doesn't throw an error for $matches or $output being used.

2. Functions setting variables in the current scope.

function foo() {
	$str = "first=value&second=value&arr[]=foo+bar&arr[]=baz";
	parse_str($str);
	echo $first;  // value
	echo $second;  // value
	echo $arr[0]; // foo bar
	echo $arr[1]; // baz
	
	$array = [
		'a' => 1,
		'b' => 'test',
	];
	extract($array);
	echo $a, $b;
}

The sniff as-is will throw errors for use of "undefined" $first, $second, $arr, $a, $b variables, while these are in actual fact all defined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions