website/errors/function.strict.md
<?php declare(strict_types = 1);
function findItem(string $needle, array $haystack): bool
{
return in_array($needle, $haystack);
}
This rule is provided by the package phpstan/phpstan-strict-rules.
Certain PHP functions perform loose comparison by default, which can lead to unexpected results due to type juggling. The strict rules package requires the strict comparison parameter to be explicitly set to true for the following functions:
in_array() -- parameter #3 ($strict)array_search() -- parameter #3 ($strict)array_keys() -- parameter #3 ($strict), when searching for a valuebase64_decode() -- parameter #2 ($strict)Without strict mode, in_array(0, ['foo']) returns true because 0 == 'foo' under loose comparison. This is almost never the intended behaviour.
Pass true as the strict comparison parameter:
<?php declare(strict_types = 1);
function findItem(string $needle, array $haystack): bool
{
- return in_array($needle, $haystack);
+ return in_array($needle, $haystack, true);
}
The same applies to array_search():
<?php declare(strict_types = 1);
/** @param array<string, mixed> $data */
function findKey(mixed $value, array $data): string|false
{
- return array_search($value, $data);
+ return array_search($value, $data, true);
}