Giter VIP home page Giter VIP logo

Comments (5)

sirbrillig avatar sirbrillig commented on September 27, 2024

Thanks for the report!

It seems it has to do with array destructuring the result, because if changed to $bar it disappears.

I think you're right because this passes:

static function foo(DBC $db, int $notificationId): void {
	list($bar) = $db->queryGetRowIndexed('SELECT blah... ', [ 'n.notificationId' => $notificationId ]);
	echo $bar;
}

Whereas this fails:

static function foo(DBC $db, int $notificationId): void {
	[$bar] = $db->queryGetRowIndexed('SELECT blah... ', [ 'n.notificationId' => $notificationId ]);
	echo $bar;
}

Even though both are destructuring. What's really weird is that the warning is Unused function parameter $notificationId. which has nothing to do with $bar.

Here's a more minimal case with the same bug:

function foo(int $baz) {
	[$bar] = doSomething([$baz]);
	return $bar;
}

Investigating...

from phpcs-variable-analysis.

sirbrillig avatar sirbrillig commented on September 27, 2024

#318 should fix the issue you found, although you'll still get an unused variable warning in your first example unless you do something with $bar like print it or return it since otherwise $bar is not used.

As for the second issue,

function dotToMultiArray( array $array ): array {
	$multi = [];
	foreach($array as $key => $value){
		$level = &$multi;                // $level is assigned
		foreach(explode(".", $key) as $node)
			$level = &$level[$node]; // $level is assigned again
		$level = $value;                 // $level is assigned a third time
	}
	return $multi;                           // We ignore $level and return $multi
}

In this case, $level is marked as unused because it is. It's only ever assigned a value but that value is never used for anything. Maybe I'm misunderstanding something?

I think that example is equivalent to:

function dotToMultiArray( array $array ): array {
	$multi = [];
	return $multi;
}

from phpcs-variable-analysis.

djibarian avatar djibarian commented on September 27, 2024

...although you'll still get an unused variable warning in your first example unless you do something with $bar like print it or return it since otherwise $bar is not used.

Yes, the function wasn't complete and $bar is used afterwards. Thanks for the quick fix!

Re the second case, $level is indeed used, look carefully. It is used as a pointer to traverse a multi-dimensional array (first and second) and set the value of one of its nodes (third). Probably the assignments by reference are confusing the linter.

from phpcs-variable-analysis.

sirbrillig avatar sirbrillig commented on September 27, 2024

Interesting... Does that actually work to run $level = $value and assign to a sub element of $multi? I thought that assigning directly to the variable would overwrite the reference itself? Is it because the reference is pointing to an array element and not the array?

from phpcs-variable-analysis.

sirbrillig avatar sirbrillig commented on September 27, 2024

Oh! It does. Wild. I made #319 to investigate that one.

from phpcs-variable-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.