Skip to content

Unique rule has an issue with SORT_REGULAR flag #1533

Open
@dvdheiden

Description

@dvdheiden

Let's say we want to validate an array with SemVer numbers:

$array = [
    "14.0",
    "14.1",
    "14.10",
];

These are unique, and array_unique agrees with me:

array_unique($array);

Results in:

[
    "14.0",
    "14.1",
    "14.10",
]

But the unique rule in this package uses an additional flag, called SORT_REGULAR. According to the PHP docs:

SORT_REGULAR - compare items normally (don't change types)

So based on that it makes sense to use the flag. But unfortunately, it doesn't...

array_unique($array, SORT_REGULAR):

Results in:

[
    "14.0",
    "14.1",
]

As you can see 14.1 and 14.10 are considered the same. That's because SORT_REGULAR triggers a loose comparison, see: https://stackoverflow.com/a/14803087.

With loose comparison:

"14.1" == "14.10";

Results in true, but with strict comparison:

"14.1" === "14.10"

Results in false.

That's why I think we shouldn't use the SORT_REGULAR flag, but just the regular array_unique. Please let me know if there are some other considerations.

There are multiple solutions:

  1. Provide the flag as parameter and default to SORT_REGULAR flag.
  2. Drop the SORT_REGULAR flag.
  3. Something else I can't think of right now 🤔

I'm willing to create a PR for this, so just let me know.

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