Skip to content

SimplifyArraySearchRector is incorrect w.r.t. "strictness" #3404

@guilliamxavier

Description

@guilliamxavier

Bug Report

Subject Details
Rector version v0.7.26
Installed as composer dependency

Rector does the following change:

-    array_search($needle, $haystack) !== false
+    in_array($needle, $haystack, true)

that adds , true which causes a different result e.g. for $needle = 42; $haystack = ['42'];:

-    true
+    false

(https://3v4l.org/ZM5aI)

Minimal PHP Code Causing Issue

https://getrector.org/demo/e226c0b9-4efd-4253-aac5-d87bf457e568

Expected Behaviour

Rector should do the following changes (note: each variable might also be a literal value):

-    array_search($needle, $haystack) !== false
+    in_array($needle, $haystack)
-    array_search($needle, $haystack, $strict) !== false
+    in_array($needle, $haystack, $strict)

and their negative counterparts:

-    array_search($needle, $haystack) === false
+    !in_array($needle, $haystack)
-    array_search($needle, $haystack, $strict) === false
+    !in_array($needle, $haystack, $strict)

i.e. preserve the optional third argument (https://3v4l.org/E44t1).

Moreover, it should not do this refactor for comparisons other than !== and === (e.g. != or ==) nor for values other than false (e.g. true or 0). Akin to StrposToStringsContainsRector.

References:

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