sebastianbergmann / comparator

Provides the functionality to compare PHP values for equality.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ArrayComparator ignores order - Part 2

panvid opened this issue · comments

As in #44 mentioned the ArrayComparator ignore the order of two arrays, also if the attribute $canonicalize = false is set.

Woops, missed to bump it.

commented

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

I bump it.

There is a test for your use case so I think it is intended.

See:

['a' => 1, 'b' => 2],

Fixed now?

There is nothing to fix here as @1blankz7 explained in #57 (comment).

If you consider two associative arrays that have the same keys and the same values associated with these keys but a different order of elements to be different (I do not, the code does not) then you need to implement a custom comparator and use that.

But isn't the definiton of equal wrong here?
There should be a way to make sure they are actually the same order.

Take for example this completely constructed code:

$html = [
  "h1" => "Greetings",
  "p" => "How are you today?"
]
for ($html as $tag => $content) {
  echo "<$tag>$content</$tag>";
}

One would output:

<h1>Greetings</h1>
<p>How are you today</p>

Greetings

How are you today

But if the equal test ignores order, we might end up with:

<p>How are you today</p>
<h1>Greetings</h1>

How are you today

Greetings


What is the intended way to check that $html is correct here?

And how should the $canonicalize = false work? This attribute forces to be in correct order.

The description says:

/**
 * @param $canonicalize bool  Arrays are sorted before comparison when set to true
 */

This indicates it would check the order. But it doesn't. Which is the problem noted here.

I believe both code and test case are wrong here.

When having $canonicalize = true, ignoring the order makes perfectly sense.

However, if $canonicalize is set to false, It should allow us to really compare the values, including order.

"Arrays are sorted before comparison when set to true" is meant to (only) apply to non-associative arrays.