-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
Support multidimensional and associative arrays when canonicalize #108
base: main
Are you sure you want to change the base?
Conversation
Would you please fix CI or suggest how to do this? |
Please rebase against |
fdff613
to
5b1cbe3
Compare
Thanks. Done. |
Codecov Report
@@ Coverage Diff @@
## main #108 +/- ##
============================================
+ Coverage 98.14% 98.20% +0.05%
- Complexity 118 124 +6
============================================
Files 14 14
Lines 324 334 +10
============================================
+ Hits 318 328 +10
Misses 6 6
... and 2 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
src/ArrayComparator.php
Outdated
} | ||
|
||
foreach ($array as &$element) { | ||
if (is_array($element)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an element of the parent $array is an array, won't it get passed to the ArrayComparator@assertEquals() method? It seems like we wouldn't need to recursively sort here if that's true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recursion is needed. After all, nesting can be as big as you want, for example:
$a = [
'k' => [
'h' => [
'n' => [
....
],
],
],
];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but assertEquals()
is going to loop through each sub-element of the array and then call Comparator@assertEquals()
on that.
If the sub-element is an array, that means it's going to get passed to ArrayComparator@canonicalize()
again.
So you're sorting everything, and then sorting it again after creating the factory on lines 70-71.
Do I have that wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I finally got it. It's brilliant!
@cosmastech would you be so nice and review this PR and if its ok merge it? Would be really neat to have this fix in. thanks in advance |
I don't see an issue with the implementation, however, I think it still needs reviewed and merged by the maintainer. |
Thanks @cosmastech for answering so fast back. @sebastianbergmann would you be so nice and merge it in if it is ok to you. Thanks alot |
if (array_is_list($array)) { | ||
sort($array); | ||
} else { | ||
ksort($array); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we traverse into elements here when they are array
s as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we only want to sort the current array at this point.
Fix:
Relates: