Completed
Push — master ( 3f5d04...b6de96 )
by
unknown
18s
created
src/Api/CachingResultsSource.php 1 patch
Spacing   +77 added lines, -78 removed lines patch added patch discarded remove patch
@@ -143,14 +143,14 @@  discard block
 block discarded – undo
143 143
 	) {
144 144
 		$results = [];
145 145
 		$metadatas = [];
146
-		if ( $this->canUseStoredResults( $entityIds, $claimIds, $constraintIds, $statuses ) ) {
146
+		if ($this->canUseStoredResults($entityIds, $claimIds, $constraintIds, $statuses)) {
147 147
 			$storedEntityIds = [];
148
-			foreach ( $entityIds as $entityId ) {
149
-				$storedResults = $this->getStoredResults( $entityId );
150
-				if ( $storedResults !== null ) {
151
-					$this->loggingHelper->logCheckConstraintsCacheHit( $entityId );
152
-					foreach ( $storedResults->getArray() as $checkResult ) {
153
-						if ( $this->statusSelected( $statuses, $checkResult ) ) {
148
+			foreach ($entityIds as $entityId) {
149
+				$storedResults = $this->getStoredResults($entityId);
150
+				if ($storedResults !== null) {
151
+					$this->loggingHelper->logCheckConstraintsCacheHit($entityId);
152
+					foreach ($storedResults->getArray() as $checkResult) {
153
+						if ($this->statusSelected($statuses, $checkResult)) {
154 154
 							$results[] = $checkResult;
155 155
 						}
156 156
 					}
@@ -158,19 +158,19 @@  discard block
 block discarded – undo
158 158
 					$storedEntityIds[] = $entityId;
159 159
 				}
160 160
 			}
161
-			$entityIds = array_values( array_diff( $entityIds, $storedEntityIds ) );
161
+			$entityIds = array_values(array_diff($entityIds, $storedEntityIds));
162 162
 		}
163
-		if ( $entityIds !== [] || $claimIds !== [] ) {
164
-			if ( $entityIds !== [] ) {
165
-				$this->loggingHelper->logCheckConstraintsCacheMisses( $entityIds );
163
+		if ($entityIds !== [] || $claimIds !== []) {
164
+			if ($entityIds !== []) {
165
+				$this->loggingHelper->logCheckConstraintsCacheMisses($entityIds);
166 166
 			}
167
-			$response = $this->getAndStoreResults( $entityIds, $claimIds, $constraintIds, $statuses );
168
-			$results = array_merge( $results, $response->getArray() );
167
+			$response = $this->getAndStoreResults($entityIds, $claimIds, $constraintIds, $statuses);
168
+			$results = array_merge($results, $response->getArray());
169 169
 			$metadatas[] = $response->getMetadata();
170 170
 		}
171 171
 		return new CachedCheckResults(
172 172
 			$results,
173
-			Metadata::merge( $metadatas )
173
+			Metadata::merge($metadatas)
174 174
 		);
175 175
 	}
176 176
 
@@ -193,13 +193,13 @@  discard block
 block discarded – undo
193 193
 		?array $constraintIds,
194 194
 		array $statuses
195 195
 	) {
196
-		if ( $claimIds !== [] ) {
196
+		if ($claimIds !== []) {
197 197
 			return false;
198 198
 		}
199
-		if ( $constraintIds !== null ) {
199
+		if ($constraintIds !== null) {
200 200
 			return false;
201 201
 		}
202
-		if ( array_diff( $statuses, self::CACHED_STATUSES ) !== [] ) {
202
+		if (array_diff($statuses, self::CACHED_STATUSES) !== []) {
203 203
 			return false;
204 204
 		}
205 205
 		return true;
@@ -214,8 +214,8 @@  discard block
 block discarded – undo
214 214
 	 * @param CheckResult $result
215 215
 	 * @return bool
216 216
 	 */
217
-	private function statusSelected( array $statuses, CheckResult $result ) {
218
-		return in_array( $result->getStatus(), $statuses, true ) ||
217
+	private function statusSelected(array $statuses, CheckResult $result) {
218
+		return in_array($result->getStatus(), $statuses, true) ||
219 219
 			$result instanceof NullResult;
220 220
 	}
221 221
 
@@ -232,11 +232,11 @@  discard block
 block discarded – undo
232 232
 		?array $constraintIds,
233 233
 		array $statuses
234 234
 	) {
235
-		$results = $this->resultsSource->getResults( $entityIds, $claimIds, $constraintIds, $statuses );
235
+		$results = $this->resultsSource->getResults($entityIds, $claimIds, $constraintIds, $statuses);
236 236
 
237
-		if ( $this->canStoreResults( $entityIds, $claimIds, $constraintIds, $statuses ) ) {
238
-			foreach ( $entityIds as $entityId ) {
239
-				$this->storeResults( $entityId, $results );
237
+		if ($this->canStoreResults($entityIds, $claimIds, $constraintIds, $statuses)) {
238
+			foreach ($entityIds as $entityId) {
239
+				$this->storeResults($entityId, $results);
240 240
 			}
241 241
 		}
242 242
 
@@ -267,10 +267,10 @@  discard block
 block discarded – undo
267 267
 		?array $constraintIds,
268 268
 		array $statuses
269 269
 	) {
270
-		if ( $constraintIds !== null ) {
270
+		if ($constraintIds !== null) {
271 271
 			return false;
272 272
 		}
273
-		if ( array_diff( self::CACHED_STATUSES, $statuses ) !== [] ) {
273
+		if (array_diff(self::CACHED_STATUSES, $statuses) !== []) {
274 274
 			return false;
275 275
 		}
276 276
 		return true;
@@ -284,21 +284,21 @@  discard block
 block discarded – undo
284 284
 	 * May include check results for other entity IDs as well,
285 285
 	 * or check results with statuses that we’re not interested in caching.
286 286
 	 */
287
-	private function storeResults( EntityId $entityId, CachedCheckResults $results ) {
287
+	private function storeResults(EntityId $entityId, CachedCheckResults $results) {
288 288
 		$latestRevisionIds = $this->getLatestRevisionIds(
289 289
 			$results->getMetadata()->getDependencyMetadata()->getEntityIds()
290 290
 		);
291
-		if ( $latestRevisionIds === null ) {
291
+		if ($latestRevisionIds === null) {
292 292
 			return;
293 293
 		}
294 294
 
295 295
 		$resultSerializations = [];
296
-		foreach ( $results->getArray() as $checkResult ) {
297
-			if ( $checkResult->getContextCursor()->getEntityId() !== $entityId->getSerialization() ) {
296
+		foreach ($results->getArray() as $checkResult) {
297
+			if ($checkResult->getContextCursor()->getEntityId() !== $entityId->getSerialization()) {
298 298
 				continue;
299 299
 			}
300
-			if ( $this->statusSelected( self::CACHED_STATUSES, $checkResult ) ) {
301
-				$resultSerializations[] = $this->checkResultSerializer->serialize( $checkResult );
300
+			if ($this->statusSelected(self::CACHED_STATUSES, $checkResult)) {
301
+				$resultSerializations[] = $this->checkResultSerializer->serialize($checkResult);
302 302
 			}
303 303
 		}
304 304
 
@@ -307,11 +307,11 @@  discard block
 block discarded – undo
307 307
 			'latestRevisionIds' => $latestRevisionIds,
308 308
 		];
309 309
 		$futureTime = $results->getMetadata()->getDependencyMetadata()->getFutureTime();
310
-		if ( $futureTime !== null ) {
310
+		if ($futureTime !== null) {
311 311
 			$value['futureTime'] = $futureTime->getArrayValue();
312 312
 		}
313 313
 
314
-		$this->cache->set( $entityId, $value, $this->ttlInSeconds );
314
+		$this->cache->set($entityId, $value, $this->ttlInSeconds);
315 315
 	}
316 316
 
317 317
 	/**
@@ -326,33 +326,32 @@  discard block
 block discarded – undo
326 326
 		$forRevision = 0
327 327
 	) {
328 328
 		$cacheInfo = WANObjectCache::PASS_BY_REF;
329
-		$value = $this->cache->get( $entityId, $curTTL, [], $cacheInfo );
330
-		$now = ( $this->microtime )( true );
329
+		$value = $this->cache->get($entityId, $curTTL, [], $cacheInfo);
330
+		$now = ($this->microtime)(true);
331 331
 
332
-		$dependencyMetadata = $this->checkDependencyMetadata( $value,
333
-			[ $entityId->getSerialization() => $forRevision ] );
334
-		if ( $dependencyMetadata === null ) {
332
+		$dependencyMetadata = $this->checkDependencyMetadata($value,
333
+			[$entityId->getSerialization() => $forRevision]);
334
+		if ($dependencyMetadata === null) {
335 335
 			return null;
336 336
 		}
337 337
 
338 338
 		// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
339 339
 		$asOf = $cacheInfo[WANObjectCache::KEY_AS_OF];
340
-		$ageInSeconds = (int)ceil( $now - $asOf );
340
+		$ageInSeconds = (int) ceil($now - $asOf);
341 341
 		$cachingMetadata = $ageInSeconds > 0 ?
342
-			CachingMetadata::ofMaximumAgeInSeconds( $ageInSeconds ) :
343
-			CachingMetadata::fresh();
342
+			CachingMetadata::ofMaximumAgeInSeconds($ageInSeconds) : CachingMetadata::fresh();
344 343
 
345 344
 		$results = [];
346
-		foreach ( $value['results'] as $resultSerialization ) {
347
-			$results[] = $this->deserializeCheckResult( $resultSerialization, $cachingMetadata );
345
+		foreach ($value['results'] as $resultSerialization) {
346
+			$results[] = $this->deserializeCheckResult($resultSerialization, $cachingMetadata);
348 347
 		}
349 348
 
350 349
 		return new CachedCheckResults(
351 350
 			$results,
352
-			Metadata::merge( [
353
-				Metadata::ofCachingMetadata( $cachingMetadata ),
354
-				Metadata::ofDependencyMetadata( $dependencyMetadata ),
355
-			] )
351
+			Metadata::merge([
352
+				Metadata::ofCachingMetadata($cachingMetadata),
353
+				Metadata::ofDependencyMetadata($dependencyMetadata),
354
+			])
356 355
 		);
357 356
 	}
358 357
 
@@ -368,43 +367,43 @@  discard block
 block discarded – undo
368 367
 	 * @return DependencyMetadata|null the dependency metadata,
369 368
 	 * or null if $value should no longer be used
370 369
 	 */
371
-	private function checkDependencyMetadata( $value, $paramRevs ) {
372
-		if ( $value === false ) {
370
+	private function checkDependencyMetadata($value, $paramRevs) {
371
+		if ($value === false) {
373 372
 			return null;
374 373
 		}
375 374
 
376
-		if ( array_key_exists( 'futureTime', $value ) ) {
377
-			$futureTime = TimeValue::newFromArray( $value['futureTime'] );
378
-			if ( !$this->timeValueComparer->isFutureTime( $futureTime ) ) {
375
+		if (array_key_exists('futureTime', $value)) {
376
+			$futureTime = TimeValue::newFromArray($value['futureTime']);
377
+			if (!$this->timeValueComparer->isFutureTime($futureTime)) {
379 378
 				return null;
380 379
 			}
381
-			$futureTimeDependencyMetadata = DependencyMetadata::ofFutureTime( $futureTime );
380
+			$futureTimeDependencyMetadata = DependencyMetadata::ofFutureTime($futureTime);
382 381
 		} else {
383 382
 			$futureTimeDependencyMetadata = DependencyMetadata::blank();
384 383
 		}
385 384
 
386
-		foreach ( $paramRevs as $id => $revision ) {
387
-			if ( $revision > 0 ) {
388
-				$value['latestRevisionIds'][$id] = min( $revision, $value['latestRevisionIds'][$id] ?? PHP_INT_MAX );
385
+		foreach ($paramRevs as $id => $revision) {
386
+			if ($revision > 0) {
387
+				$value['latestRevisionIds'][$id] = min($revision, $value['latestRevisionIds'][$id] ?? PHP_INT_MAX);
389 388
 			}
390 389
 		}
391 390
 
392 391
 		$dependedEntityIds = array_map(
393
-			[ $this->entityIdParser, "parse" ],
394
-			array_keys( $value['latestRevisionIds'] )
392
+			[$this->entityIdParser, "parse"],
393
+			array_keys($value['latestRevisionIds'])
395 394
 		);
396 395
 
397
-		if ( $value['latestRevisionIds'] !== $this->getLatestRevisionIds( $dependedEntityIds ) ) {
396
+		if ($value['latestRevisionIds'] !== $this->getLatestRevisionIds($dependedEntityIds)) {
398 397
 			return null;
399 398
 		}
400 399
 
401 400
 		return array_reduce(
402 401
 			$dependedEntityIds,
403
-			static function ( DependencyMetadata $metadata, EntityId $entityId ) {
404
-				return DependencyMetadata::merge( [
402
+			static function(DependencyMetadata $metadata, EntityId $entityId) {
403
+				return DependencyMetadata::merge([
405 404
 					$metadata,
406
-					DependencyMetadata::ofEntityId( $entityId ),
407
-				] );
405
+					DependencyMetadata::ofEntityId($entityId),
406
+				]);
408 407
 			},
409 408
 			$futureTimeDependencyMetadata
410 409
 		);
@@ -424,13 +423,13 @@  discard block
 block discarded – undo
424 423
 		array $resultSerialization,
425 424
 		CachingMetadata $cachingMetadata
426 425
 	) {
427
-		$result = $this->checkResultDeserializer->deserialize( $resultSerialization );
428
-		if ( $this->isPossiblyStaleResult( $result ) ) {
426
+		$result = $this->checkResultDeserializer->deserialize($resultSerialization);
427
+		if ($this->isPossiblyStaleResult($result)) {
429 428
 			$result->withMetadata(
430
-				Metadata::merge( [
429
+				Metadata::merge([
431 430
 					$result->getMetadata(),
432
-					Metadata::ofCachingMetadata( $cachingMetadata ),
433
-				] )
431
+					Metadata::ofCachingMetadata($cachingMetadata),
432
+				])
434 433
 			);
435 434
 		}
436 435
 		return $result;
@@ -440,8 +439,8 @@  discard block
 block discarded – undo
440 439
 	 * @param CheckResult $result
441 440
 	 * @return bool
442 441
 	 */
443
-	private function isPossiblyStaleResult( CheckResult $result ) {
444
-		if ( $result instanceof NullResult ) {
442
+	private function isPossiblyStaleResult(CheckResult $result) {
443
+		if ($result instanceof NullResult) {
445 444
 			return false;
446 445
 		}
447 446
 
@@ -456,14 +455,14 @@  discard block
 block discarded – undo
456 455
 	 * @return int[]|null array from entity ID serializations to revision ID,
457 456
 	 * or null to indicate that not all revision IDs could be loaded
458 457
 	 */
459
-	private function getLatestRevisionIds( array $entityIds ) {
460
-		if ( $entityIds === [] ) {
458
+	private function getLatestRevisionIds(array $entityIds) {
459
+		if ($entityIds === []) {
461 460
 			$this->loggingHelper->logEmptyDependencyMetadata();
462 461
 			return [];
463 462
 		}
464
-		if ( count( $entityIds ) > $this->maxRevisionIds ) {
463
+		if (count($entityIds) > $this->maxRevisionIds) {
465 464
 			// one of those entities will probably be edited soon, so might as well skip caching
466
-			$this->loggingHelper->logHugeDependencyMetadata( $entityIds, $this->maxRevisionIds );
465
+			$this->loggingHelper->logHugeDependencyMetadata($entityIds, $this->maxRevisionIds);
467 466
 			return null;
468 467
 		}
469 468
 
@@ -471,7 +470,7 @@  discard block
 block discarded – undo
471 470
 			$entityIds,
472 471
 			LookupConstants::LATEST_FROM_REPLICA
473 472
 		);
474
-		if ( $this->hasFalseElements( $latestRevisionIds ) ) {
473
+		if ($this->hasFalseElements($latestRevisionIds)) {
475 474
 			return null;
476 475
 		}
477 476
 		return $latestRevisionIds;
@@ -481,8 +480,8 @@  discard block
 block discarded – undo
481 480
 	 * @param array $array
482 481
 	 * @return bool
483 482
 	 */
484
-	private function hasFalseElements( array $array ) {
485
-		return in_array( false, $array, true );
483
+	private function hasFalseElements(array $array) {
484
+		return in_array(false, $array, true);
486 485
 	}
487 486
 
488 487
 	/**
@@ -490,7 +489,7 @@  discard block
 block discarded – undo
490 489
 	 *
491 490
 	 * @param callable $microtime
492 491
 	 */
493
-	public function setMicrotimeFunction( callable $microtime ) {
492
+	public function setMicrotimeFunction(callable $microtime) {
494 493
 		$this->microtime = $microtime;
495 494
 	}
496 495
 
Please login to merge, or discard this patch.