False positive: Call to inArray() with different object instances will always evaluate to true
simPod opened this issue ยท 13 comments
class A {
public function __construct(private string $val) {
}
}
function foo(A $a) {
Assert::inArray($a, [new A('a')]);
}gives:
Call to static method Webmozart\Assert\Assert::inArray() with arguments A and array(A) will always evaluate to true
Yeah, this is unfortunate, it means that this code shouldn't exist https://github.com/phpstan/phpstan-src/blob/3c3ea2f21898e8e411b0a7263910722738839288/src/Rules/Comparison/ImpossibleCheckTypeHelper.php#L85-L134 and all of that should be handled directly in https://github.com/phpstan/phpstan-src/blob/master/src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php.
Hi there! ๐
Looks like you opened an issue without following one of the issue templates:
Bug report ๐ (open an issue)
If something isn't working as expected ๐ค.
Feature request ๐ (open an issue)
I have a suggestion (and may want to implement it ๐)!
Support question โ (open a discussion)
I need some help with my code because PHPStan doesn't like it.
The current issue will be closed. This is a precaution to save maintainer's time, I hope you'll understand.
Sincerely, the bot ๐ค
tried working on it, but I don't get how to formulate the unit test to reproduce the problem (I don't get how the LegacyNodeScopeResolverTest works for this case)
You'd need to:
- Create a new test case that looks like https://github.com/phpstan/phpstan-src/blob/master/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRuleTest.php and tests the same rule
- Create a new type-specifying extension that mimics what
in_arraydoes. You can copy and reduce this file to a minimal working example: - Create
foo.neonthat registers this extension - Return the path to
foo.neonin the testcase's (from 1)) methodpublic static function getAdditionalConfigFiles().
Hello, I just came across a similar issue as per the following example:
/**
* @param mixed $value
* @param array<mixed> $array
*/
public function test($value, array $array) : void
{
Assert::inArray($value, $array);
}
phpstan reports:
Call to static method Webmozart\Assert\Assert::inArray() with mixed and array will always evaluate to true.
However this is not true, the webmozart assert function is using the exact same param annotations.
I'm not completely sure if this is related to what @simPod has reported.
@p4veI In fact this is exactly the same issue :) In fact this issue was moved from phpstan-webmozart-repo here because it needs to be fixed in PHPStan's core.
imo fixed in latest release ;)
Please send a regression test, or it didn't happen :-)
Interesting, I just tried to add a regression test for this. The current state is
- the original snippet is still reported and therefore not fixed
- the snippet from @p4veI is not reported and therefore fixed, I assume
Not surprising - there's still a massive codeblock about in_array in ImpossibleCheckTypeHelper: https://github.com/phpstan/phpstan-src/blob/a5f8d2dd0095a80f69fc23364f9cddae67ed7c44/src/Rules/Comparison/ImpossibleCheckTypeHelper.php#L80-L138
It's there to prevent false-positives about in_array calls, but it's not going to work if an extension delegates its method call to in_array extension via TypeSpecifier.
My comment still stands #142 (comment)
ah right, I was just super confused because I could not reproduce it in a phpstan snippet. actually it never reported even when it definitely should. that explains it all :) thx
Maybe I try to look at the in_array stuff in phpstan soon. that feels more like my domain :)
Enjoy :D phpstan/phpstan#6705
Just encounter the issue with
Assert::inArray('from__r', $fields);
Assert::inArray('to__r', $fields);
Assert::inArray('fromEnd__r', $fields);
Assert::inArray('toEnd__r', $fields);
Only the second, third and fourth check are reported as always true.