Skip to content

auto-fix "Call to function in_array() requires parameter #3 to be set/true." #347

Open

Description

@staabm

if we know native haystack and needle types for a in_array, array_keys or array_search call, it would be great we could fix add a true as 3rd parameter in case its missing or can always be true. we should only fix when haystacks' iterable type is a super type of needle (strict comparison). the fixer must not change calls where equivalence cannot be proven.

Example: in_array($needle, X::VALS) into its strict counterpart in_array($needle, X::VALS, true).
https://phpstan.org/r/e288cf2a-08e5-4c06-9255-1a98eefc99fe

named arguments should be supported and calls using the splat operator should be skipped.

we should not fix calls which get their iterable-value type from a phpdoc, e.g.:

/** @return int[] */ 
function getArray(): array {}

function doFoo(int $i):bool {
  return in_array($i, getArray());
}

For array_keys(), only consider calls with at least two arguments, since the strictness flag matters only when a search value is supplied. Calls without a search value should remain unchanged.

The rule also reports calls to other functions, such as base64_decode(). Those calls should not be made fixable by this change.

Activity

  1. changed the title [-]auto-fix "Call to function in_array() requires parameter #3 to be set."[/-] [+]auto-fix "Call to function in_array() requires parameter #3 to be true."[/+] on Nov 3, 2026
  2. changed the title [-]auto-fix "Call to function in_array() requires parameter #3 to be true."[/-] [+]auto-fix "Call to function in_array() requires parameter #3 to be set/true."[/+] on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions