@@ -16,25 +16,13 @@ final class DataLoader implements DataLoaderInterface
1616 */
1717 private $ batchLoadFunction ;
1818
19- /**
20- * @var array
21- */
22- private $ promiseQueue = [];
19+ private array $ promiseQueue = [];
2320
24- /**
25- * @var CacheMapInterface
26- */
27- private $ promiseCache ;
21+ private CacheMapInterface $ promiseCache ;
2822
29- /**
30- * @var LoopInterface
31- */
32- private $ eventLoop ;
23+ private LoopInterface $ eventLoop ;
3324
34- /**
35- * @var DataLoaderOptions
36- */
37- private $ options ;
25+ private DataLoaderOptions $ options ;
3826
3927 /**
4028 * Initiates a new DataLoader.
@@ -98,9 +86,7 @@ public function loadMany(array $keys): ExtendedPromiseInterface
9886 {
9987 return all (
10088 \array_map (
101- function ($ key ) {
102- return $ this ->load ($ key );
103- },
89+ fn ($ key ) => $ this ->load ($ key ),
10490 $ keys
10591 )
10692 );
@@ -146,9 +132,7 @@ private function scheduleDispatch(): void
146132 {
147133 if ($ this ->options ->shouldBatch ()) {
148134 $ this ->eventLoop ->futureTick (
149- function () {
150- $ this ->dispatchQueue ();
151- }
135+ fn () => $ this ->dispatchQueue ()
152136 );
153137
154138 return ;
@@ -168,12 +152,13 @@ private function dispatchQueue(): void
168152 $ this ->promiseQueue = [];
169153
170154 $ maxBatchSize = $ this ->options ->getMaxBatchSize ();
155+ $ shouldBeDispatchedInMultipleBatches = $ maxBatchSize !== null
156+ && $ maxBatchSize > 0
157+ && $ maxBatchSize < count ($ queue );
171158
172- if ($ maxBatchSize !== null && $ maxBatchSize > 0 && $ maxBatchSize < count ($ queue )) {
173- $ this ->dispatchQueueInMultipleBatches ($ queue , $ maxBatchSize );
174- } else {
175- $ this ->dispatchQueueBatch ($ queue );
176- }
159+ $ shouldBeDispatchedInMultipleBatches
160+ ? $ this ->dispatchQueueInMultipleBatches ($ queue , $ maxBatchSize )
161+ : $ this ->dispatchQueueBatch ($ queue );
177162 }
178163
179164 /**
@@ -195,14 +180,14 @@ private function dispatchQueueBatch($batch)
195180 return $ this ->handleFailedDispatch ($ batch , $ exception );
196181 }
197182
198- $ batchPromise-> then (
199- function ( $ values ) use ( $ batch , $ keys ) {
200- $ this -> validateBatchPromiseOutput ($ values , $ keys );
201- $ this ->handleSuccessfulDispatch ( $ batch , $ values );
202- }
203- )-> then ( null , function ( $ error ) use ( $ batch ) {
204- $ this -> handleFailedDispatch ( $ batch , $ error );
205- } );
183+ $ batchPromise
184+ -> then (
185+ function ($ values) use ( $ batch , $ keys ) {
186+ $ this ->validateBatchPromiseOutput ( $ values , $ keys );
187+ $ this -> handleSuccessfulDispatch ( $ batch , $ values );
188+ }
189+ )
190+ -> then ( null , fn ( $ error ) => $ this -> handleFailedDispatch ( $ batch , $ error ) );
206191 }
207192
208193 /**
@@ -234,11 +219,9 @@ private function handleSuccessfulDispatch(array $batch, array $values): void
234219 {
235220 foreach ($ batch as $ index => $ queueItem ) {
236221 $ value = $ values [$ index ];
237- if ($ value instanceof \Exception) {
238- $ queueItem ['reject ' ]($ value );
239- } else {
240- $ queueItem ['resolve ' ]($ value );
241- }
222+ $ value instanceof \Exception
223+ ? $ queueItem ['reject ' ]($ value )
224+ : $ queueItem ['resolve ' ]($ value );
242225 }
243226 }
244227
0 commit comments