@@ -30,62 +30,50 @@ public function __construct(SerializerInterface $serializer)
3030 /**
3131 * @inheritDoc
3232 */
33- public function isEqual (?AddressInterface $ shippingAddress , ?AddressInterface $ billingAddress ): bool
33+ public function isEqual (?AddressInterface $ address1 , ?AddressInterface $ address2 ): bool
3434 {
35- if ($ shippingAddress === null || $ billingAddress === null ) {
35+ if ($ address1 === null || $ address2 === null ) {
3636 return false ;
3737 }
3838
39- if ($ shippingAddress ->getCustomerAddressId () !== null &&
40- $ billingAddress ->getCustomerAddressId () !== null
39+ if ($ address1 ->getCustomerAddressId () !== null &&
40+ $ address2 ->getCustomerAddressId () !== null
4141 ) {
42- return ((int )$ shippingAddress ->getCustomerAddressId () ===
43- (int )$ billingAddress ->getCustomerAddressId ());
42+ return ((int )$ address1 ->getCustomerAddressId () ===
43+ (int )$ address2 ->getCustomerAddressId ());
4444 } else {
45- $ shippingAddressData = $ shippingAddress ->getData ();
46- $ billingAddressData = $ billingAddress ->getData ();
47- $ billingKeys = array_flip (array_keys ($ billingAddressData ));
48- $ shippingData = array_intersect_key ($ shippingAddressData , $ billingKeys );
45+ $ addressKeys = array_intersect_key ($ address1 ->getData (), $ address2 ->getData ());
4946 $ removeKeys = ['address_type ' , 'region_code ' , 'save_in_address_book ' ];
50- $ billingData = array_diff_key ($ billingAddressData , array_flip ($ removeKeys ));
51- $ diff = $ this ->arrayDiffAssocRecursive ($ billingData , $ shippingData );
47+ $ addressKeys = array_diff_key ($ addressKeys , array_flip ($ removeKeys ));
48+
49+ $ address1Data = array_intersect_key ($ address1 ->getData (), $ addressKeys );
50+ $ address2Data = array_intersect_key ($ address2 ->getData (), $ addressKeys );
51+ $ diff = $ this ->computeArrayDifference ($ address1Data , $ address2Data );
5252 return empty ($ diff );
5353 }
5454 }
5555
5656 /**
57- * Compare two arrays
57+ * Computing the difference of two arrays
5858 *
5959 * @param array $array1
6060 * @param array $array2
6161 * @return array
62- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
6362 */
64- private function arrayDiffAssocRecursive (array $ array1 , array $ array2 ): array
63+ private function computeArrayDifference (array $ array1 , array $ array2 ): array
6564 {
66- $ difference = [];
67- foreach ($ array1 as $ key => $ value ) {
68- if (is_array ($ value )) {
69- if (!isset ($ array2 [$ key ])) {
70- $ difference [$ key ] = $ value ;
71- } elseif (!is_array ($ array2 [$ key ])) {
72- $ difference [$ key ] = $ value ;
73- } else {
74- $ new_diff = $ this ->arrayDiffAssocRecursive ($ value , $ array2 [$ key ]);
75- if (!empty ($ new_diff )) {
76- $ difference [$ key ] = $ new_diff ;
77- }
65+ return array_udiff_assoc (
66+ $ array1 ,
67+ $ array2 ,
68+ function ($ el1 , $ el2 ) {
69+ if (is_object ($ el1 ) || is_array ($ el1 )) {
70+ $ el1 = $ this ->serializer ->serialize ($ el1 );
7871 }
79- } else {
80- $ str1 = is_object ($ array2 [$ key ]) ? $ this ->serializer ->serialize ($ array2 [$ key ]) : (string )$ array2 [$ key ];
81- $ str2 = is_object ($ array2 [$ key ]) ? $ this ->serializer ->serialize ($ value ) : (string )$ value ;
82- if ((!empty ($ str1 ) && !empty ($ str2 ) && $ str1 !== $ str2 )
83- || (!empty ($ str1 ) && empty ($ str2 ))
84- || (!empty ($ str1 ) && empty ($ str2 ))) {
85- $ difference [$ key ] = $ str2 ;
72+ if (is_object ($ el2 ) || is_array ($ el2 )) {
73+ $ el2 = $ this ->serializer ->serialize ($ el2 );
8674 }
75+ return strcmp ((string )$ el1 , (string )$ el2 );
8776 }
88- }
89- return $ difference ;
77+ );
9078 }
9179}
0 commit comments