Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alexndlm
Copy link

@alexndlm
Copy link
Author

Would you please fix CI or suggest how to do this?

@sebastianbergmann
Copy link
Owner

Would you please fix CI or suggest how to do this?

Please rebase against main.

@alexndlm alexndlm force-pushed the feature/support-multidimensional-and-associative-arrays-when-canonicalize branch from fdff613 to 5b1cbe3 Compare August 14, 2023 10:29
@alexndlm
Copy link
Author

Thanks. Done.

@codecov
Copy link

codecov bot commented Aug 14, 2023

Codecov Report

Merging #108 (501f687) into main (a261237) will increase coverage by 0.05%.
Report is 20 commits behind head on main.
The diff coverage is 100.00%.

❗ Current head 501f687 differs from pull request most recent head bc39224. Consider uploading reports for the commit bc39224 to get more accurate results

@@             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              
Files Coverage Δ
src/ArrayComparator.php 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

}

foreach ($array as &$element) {
if (is_array($element)) {

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.

Copy link
Author

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' => [
                ....
            ],
        ],
    ],
];

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?

Copy link
Author

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!

@alexndlm alexndlm requested a review from cosmastech October 15, 2023 12:55
@senaria
Copy link

senaria commented May 2, 2024

@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

@cosmastech
Copy link

@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.

@senaria
Copy link

senaria commented May 6, 2024

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

Comment on lines +132 to +136
if (array_is_list($array)) {
sort($array);
} else {
ksort($array);
}
Copy link
Contributor

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 arrays as well?

Copy link
Author

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.

SnakeCharm3r pushed a commit to SnakeCharm3r/comparator that referenced this pull request Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants