Skip to content

Unique rule has an issue with SORT_REGULAR flag #1533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dvdheiden opened this issue Mar 27, 2025 · 3 comments · May be fixed by #1534
Open

Unique rule has an issue with SORT_REGULAR flag #1533

dvdheiden opened this issue Mar 27, 2025 · 3 comments · May be fixed by #1534

Comments

@dvdheiden
Copy link

dvdheiden commented Mar 27, 2025

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.

@dvdheiden dvdheiden changed the title Unique rule should not use the SORT_REGULAR flag Unique rule should not use the SORT_REGULAR flag by default Mar 27, 2025
@dvdheiden dvdheiden changed the title Unique rule should not use the SORT_REGULAR flag by default Unique rule has some issue with SORT_REGULAR flag Mar 27, 2025
@dvdheiden dvdheiden changed the title Unique rule has some issue with SORT_REGULAR flag Unique rule has an issue with SORT_REGULAR flag Mar 27, 2025
@henriquemoody
Copy link
Member

Hi @dvdheiden !

I totally agree with you that we have a bug here. I'm inclined to DROP the flag altogether, unless there's a good reason not to. If you would be up for creating a pull request, I would be happy to review and merge it!

🐼

@dvdheiden
Copy link
Author

Thanks for your reply. I will create a PR for it.

@dvdheiden dvdheiden linked a pull request Mar 28, 2025 that will close this issue
@dvdheiden
Copy link
Author

Done, see: #1534

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants