11<?php
22
33declare (strict_types=1 );
4-
4+ /*
5+ * This file is part of PHPUnit.
6+ *
7+ * (c) Sebastian Bergmann <sebastian@phpunit.de>
8+ *
9+ * For the full copyright and license information, please view the LICENSE
10+ * file that was distributed with this source code.
11+ */
512namespace PHPUnit \Framework \Constraint \Dictionary ;
613
14+ use function array_key_exists ;
15+ use function assert ;
16+ use function in_array ;
17+ use function is_array ;
18+ use function is_object ;
19+ use function sprintf ;
20+ use function str_replace ;
21+ use function substr_replace ;
22+ use function trim ;
723use PHPUnit \Framework \Constraint \Constraint ;
824use PHPUnit \Framework \ExpectationFailedException ;
925use SebastianBergmann \Comparator \ComparisonFailure ;
@@ -42,11 +58,13 @@ public function evaluate(mixed $other, string $description = '', bool $returnRes
4258 if ($ returnResult ) {
4359 return false ;
4460 }
61+
4562 throw new ExpectationFailedException (
4663 trim ($ description . "\n" . $ f ->getMessage ()),
47- $ f
64+ $ f,
4865 );
4966 }
67+
5068 return true ;
5169 }
5270
@@ -60,15 +78,15 @@ public function toString(): string
6078
6179 /**
6280 * cribbed from `vendor/sebastian/comparator/src/ArrayComparator.php`
63- * This potentially should be a dictionarycomparator or type-strict arraycomparator
81+ * This potentially should be a dictionarycomparator or type-strict arraycomparator.
6482 */
6583 private function compareDictionary (array $ expected , array $ actual , array &$ processed = []): void
6684 {
67- $ remaining = $ actual ;
68- $ actualAsString = "Array ( \n" ;
85+ $ remaining = $ actual ;
86+ $ actualAsString = "Array ( \n" ;
6987 $ expectedAsString = "Array ( \n" ;
70- $ equal = true ;
71- $ exporter = new Exporter ;
88+ $ equal = true ;
89+ $ exporter = new Exporter ;
7290
7391 foreach ($ expected as $ key => $ value ) {
7492 unset($ remaining [$ key ]);
@@ -80,6 +98,7 @@ private function compareDictionary(array $expected, array $actual, array &$proce
8098 $ exporter ->shortenedExport ($ value ),
8199 );
82100 $ equal = false ;
101+
83102 continue ;
84103 }
85104
@@ -94,12 +113,13 @@ private function compareDictionary(array $expected, array $actual, array &$proce
94113 $ exporter ->export ($ actual [$ key ]),
95114 );
96115
97- // expected array, got array
116+ // expected array, got array
98117 case is_array ($ value ) && is_array ($ actual [$ key ]):
99118 $ this ->compareDictionary ($ value , $ actual [$ key ]);
119+
100120 break ;
101121
102- // type mismatch, expected object, got something else
122+ // type mismatch, expected object, got something else
103123 case is_object ($ value ) && !is_object ($ actual [$ key ]):
104124 throw new ComparisonFailure (
105125 $ value ,
@@ -108,16 +128,18 @@ private function compareDictionary(array $expected, array $actual, array &$proce
108128 $ exporter ->export ($ actual [$ key ]),
109129 );
110130
111- // type mismatch, expected object, got object
131+ // type mismatch, expected object, got object
112132 case is_object ($ value ) && is_object ($ actual [$ key ]):
113133 $ this ->compareObjects ($ value , $ actual [$ key ], $ processed );
134+
114135 break ;
115136
116- // both are not array, both are not objects, strict comparison check
137+ // both are not array, both are not objects, strict comparison check
117138 default :
118139 if ($ value === $ actual [$ key ]) {
119140 continue 2 ;
120141 }
142+
121143 throw new ComparisonFailure (
122144 $ value ,
123145 $ actual [$ key ],
@@ -141,14 +163,14 @@ private function compareDictionary(array $expected, array $actual, array &$proce
141163 " %s => %s \n" ,
142164 $ exporter ->export ($ key ),
143165 $ e ->getExpectedAsString () !== '' ? $ this ->indent (
144- $ e ->getExpectedAsString ()
166+ $ e ->getExpectedAsString (),
145167 ) : $ exporter ->shortenedExport ($ e ->getExpected ()),
146168 );
147169 $ actualAsString .= sprintf (
148170 " %s => %s \n" ,
149171 $ exporter ->export ($ key ),
150172 $ e ->getActualAsString () !== '' ? $ this ->indent (
151- $ e ->getActualAsString ()
173+ $ e ->getActualAsString (),
152174 ) : $ exporter ->shortenedExport ($ e ->getActual ()),
153175 );
154176 $ equal = false ;
@@ -180,9 +202,9 @@ private function compareDictionary(array $expected, array $actual, array &$proce
180202
181203 /**
182204 * cribbed from `vendor/sebastian/comparator/src/ObjectComparator.php`
183- * this potentially should be a type-strict objectcomparator
205+ * this potentially should be a type-strict objectcomparator.
184206 */
185- private function compareObjects (object $ expected , object $ actual , array &$ processed = [])
207+ private function compareObjects (object $ expected , object $ actual , array &$ processed = []): void
186208 {
187209 if ($ actual ::class !== $ expected ::class) {
188210 $ exporter = new Exporter ;
@@ -207,9 +229,11 @@ private function compareObjects(object $expected, object $actual, array &$proces
207229 }
208230
209231 $ processed [] = [$ actual , $ expected ];
232+
210233 if ($ actual === $ expected ) {
211234 return ;
212235 }
236+
213237 try {
214238 $ this ->compareDictionary ($ this ->toArray ($ expected ), $ this ->toArray ($ actual ), $ processed );
215239 } catch (ComparisonFailure $ e ) {
@@ -225,15 +249,15 @@ private function compareObjects(object $expected, object $actual, array &$proces
225249 }
226250
227251 /**
228- * cribbed from `vendor/sebastian/comparator/src/ObjectComparator.php`
252+ * cribbed from `vendor/sebastian/comparator/src/ObjectComparator.php`.
229253 */
230254 private function toArray (object $ object ): array
231255 {
232256 return (new Exporter )->toArray ($ object );
233257 }
234258
235259 /**
236- * cribbed from `vendor/sebastian/comparator/src/ArrayComparator.php`
260+ * cribbed from `vendor/sebastian/comparator/src/ArrayComparator.php`.
237261 */
238262 private function indent (string $ lines ): string
239263 {
0 commit comments