Skip to content

Commit e118581

Browse files
committed
Refactor code documentation and type hints across multiple files
- Updated PHPDoc comments to include parameter types for better clarity and type safety. - Changed return types in `RequestConfigurator` interface to specify `ClientHandler`. - Enhanced type hints in `RequestExecutor` interface methods. - Improved consistency in null checks and array handling in various traits and classes. - Adjusted method signatures in `CacheManager`, `ManagesDebugAndProfiling`, `ManagesRetries`, and `PerformsHttpRequests` traits to reflect accurate parameter types. - Ensured proper handling of optional parameters and improved readability of the codebase.
1 parent cb88e18 commit e118581

8 files changed

Lines changed: 336 additions & 360 deletions

File tree

src/Fetch/Cache/CacheManager.php

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,20 @@ final class CacheManager
8080
/**
8181
* Create a new cache manager.
8282
*
83-
* @param CacheInterface|null $cache Cache backend (defaults to MemoryCache)
84-
* @param array<string, mixed> $options Cache options
85-
* @param LoggerInterface|null $logger Logger for events
86-
* @param string $logLevel Log level for cache events
83+
* @param CacheInterface|null $cache Cache backend (defaults to MemoryCache)
84+
* @param array<string, mixed> $options Cache options
85+
* @param LoggerInterface|null $logger Logger for events
86+
* @param string $logLevel Log level for cache events
8787
*/
8888
public function __construct(
8989
?CacheInterface $cache = null,
9090
array $options = [],
9191
?LoggerInterface $logger = null,
9292
string $logLevel = 'debug',
9393
) {
94-
$this->cache = $cache ?? new MemoryCache;
94+
$this->cache = $cache ?? new MemoryCache();
9595
$this->options = array_merge(self::DEFAULT_OPTIONS, $options);
96-
$this->logger = $logger ?? new NullLogger;
96+
$this->logger = $logger ?? new NullLogger();
9797
$this->logLevel = $logLevel;
9898

9999
$this->keyGenerator = new CacheKeyGenerator(
@@ -105,7 +105,7 @@ public function __construct(
105105
/**
106106
* Create a cache manager with custom configuration.
107107
*
108-
* @param array<string, mixed> $options
108+
* @param array<string, mixed> $options
109109
*/
110110
public static function create(
111111
?CacheInterface $cache = null,
@@ -120,7 +120,7 @@ public static function create(
120120
*/
121121
public static function disabled(): self
122122
{
123-
return new self(new MemoryCache, ['enabled' => false]);
123+
return new self(new MemoryCache(), ['enabled' => false]);
124124
}
125125

126126
/**
@@ -152,13 +152,13 @@ public function getOptions(): array
152152
/**
153153
* Determine if the request should use caching.
154154
*
155-
* @param string $method HTTP method
156-
* @param bool $isAsync Whether the request is async
157-
* @param array<string, mixed> $requestOptions Per-request options
155+
* @param string $method HTTP method
156+
* @param bool $isAsync Whether the request is async
157+
* @param array<string, mixed> $requestOptions Per-request options
158158
*/
159159
public function shouldUseCache(string $method, bool $isAsync, array $requestOptions = []): bool
160160
{
161-
if (! $this->isEnabled()) {
161+
if (!$this->isEnabled()) {
162162
return false;
163163
}
164164

@@ -170,7 +170,7 @@ public function shouldUseCache(string $method, bool $isAsync, array $requestOpti
170170
}
171171

172172
// Check if method is cacheable
173-
if (! $this->isCacheableMethod($method)) {
173+
if (!$this->isCacheableMethod($method)) {
174174
return false;
175175
}
176176

@@ -189,14 +189,15 @@ public function shouldUseCache(string $method, bool $isAsync, array $requestOpti
189189
/**
190190
* Try to get a cached response for the request.
191191
*
192-
* @param string $method HTTP method
193-
* @param string $uri Request URI
194-
* @param array<string, mixed> $requestOptions Request options
192+
* @param string $method HTTP method
193+
* @param string $uri Request URI
194+
* @param array<string, mixed> $requestOptions Request options
195+
*
195196
* @return array{response: Response|null, cached: CachedResponse|null, status: string}
196197
*/
197198
public function getCachedResponse(string $method, string $uri, array $requestOptions = []): array
198199
{
199-
if (! $this->shouldUseCache($method, $requestOptions['async'] ?? false, $requestOptions)) {
200+
if (!$this->shouldUseCache($method, $requestOptions['async'] ?? false, $requestOptions)) {
200201
$this->logEvent('BYPASS', $method, $uri);
201202

202203
return ['response' => null, 'cached' => null, 'status' => 'BYPASS'];
@@ -213,7 +214,7 @@ public function getCachedResponse(string $method, string $uri, array $requestOpt
213214
$key = $this->generateKey($method, $uri, $requestOptions);
214215
$cached = $this->cache->get($key);
215216

216-
if ($cached === null) {
217+
if (null === $cached) {
217218
$this->logEvent('MISS', $method, $uri);
218219

219220
return ['response' => null, 'cached' => null, 'status' => 'MISS'];
@@ -254,31 +255,31 @@ public function getCachedResponse(string $method, string $uri, array $requestOpt
254255
/**
255256
* Store a response in the cache.
256257
*
257-
* @param string $method HTTP method
258-
* @param string $uri Request URI
259-
* @param ResponseInterface $response The response to cache
260-
* @param array<string, mixed> $requestOptions Request options
258+
* @param string $method HTTP method
259+
* @param string $uri Request URI
260+
* @param ResponseInterface $response The response to cache
261+
* @param array<string, mixed> $requestOptions Request options
261262
*/
262263
public function storeResponse(
263264
string $method,
264265
string $uri,
265266
ResponseInterface $response,
266267
array $requestOptions = [],
267268
): void {
268-
if (! $this->shouldUseCache($method, $requestOptions['async'] ?? false, $requestOptions)) {
269+
if (!$this->shouldUseCache($method, $requestOptions['async'] ?? false, $requestOptions)) {
269270
return;
270271
}
271272

272273
// Parse Cache-Control headers
273274
$cacheControl = CacheControl::fromResponse($response);
274275

275-
if (! $this->shouldStoreResponse($method, $response, $cacheControl, $requestOptions)) {
276+
if (!$this->shouldStoreResponse($method, $response, $cacheControl, $requestOptions)) {
276277
return;
277278
}
278279

279280
// Calculate TTL
280281
$ttl = $this->calculateTtl($response, $cacheControl, $requestOptions);
281-
if ($ttl !== null && $ttl <= 0) {
282+
if (null !== $ttl && $ttl <= 0) {
282283
return;
283284
}
284285

@@ -292,29 +293,30 @@ public function storeResponse(
292293
/**
293294
* Add conditional headers for revalidation.
294295
*
295-
* @param array<string, mixed> $options Request options
296-
* @param CachedResponse|null $cached Cached response to revalidate
296+
* @param array<string, mixed> $options Request options
297+
* @param CachedResponse|null $cached Cached response to revalidate
298+
*
297299
* @return array<string, mixed> Modified options
298300
*/
299301
public function addConditionalHeaders(array $options, ?CachedResponse $cached): array
300302
{
301-
if ($cached === null) {
303+
if (null === $cached) {
302304
return $options;
303305
}
304306

305-
if (! isset($options['headers'])) {
307+
if (!isset($options['headers'])) {
306308
$options['headers'] = [];
307309
}
308310

309311
// Add If-None-Match for ETag
310312
$etag = $cached->getETag();
311-
if ($etag !== null) {
313+
if (null !== $etag) {
312314
$options['headers']['If-None-Match'] = $etag;
313315
}
314316

315317
// Add If-Modified-Since for Last-Modified
316318
$lastModified = $cached->getLastModified();
317-
if ($lastModified !== null) {
319+
if (null !== $lastModified) {
318320
$options['headers']['If-Modified-Since'] = $lastModified;
319321
}
320322

@@ -324,8 +326,8 @@ public function addConditionalHeaders(array $options, ?CachedResponse $cached):
324326
/**
325327
* Handle a 304 Not Modified response.
326328
*
327-
* @param CachedResponse $cached The cached response
328-
* @param ResponseInterface $notModifiedResponse The 304 response
329+
* @param CachedResponse $cached The cached response
330+
* @param ResponseInterface $notModifiedResponse The 304 response
329331
*/
330332
public function handleNotModified(
331333
CachedResponse $cached,
@@ -355,11 +357,11 @@ public function handleNotModified(
355357
/**
356358
* Handle stale-if-error: serve stale response on error.
357359
*
358-
* @param CachedResponse|null $cached The cached response
360+
* @param CachedResponse|null $cached The cached response
359361
*/
360362
public function handleStaleIfError(?CachedResponse $cached, string $method, string $uri): ?Response
361363
{
362-
if ($cached === null) {
364+
if (null === $cached) {
363365
return null;
364366
}
365367

@@ -368,7 +370,7 @@ public function handleStaleIfError(?CachedResponse $cached, string $method, stri
368370
return null;
369371
}
370372

371-
if (! $cached->isUsableAsStale($staleIfError)) {
373+
if (!$cached->isUsableAsStale($staleIfError)) {
372374
return null;
373375
}
374376

@@ -381,6 +383,8 @@ public function handleStaleIfError(?CachedResponse $cached, string $method, stri
381383

382384
/**
383385
* Convenience alias matching legacy trait naming.
386+
*
387+
* @param array<string, mixed> $requestOptions
384388
*/
385389
public function cacheResponse(string $method, string $uri, ResponseInterface $response, array $requestOptions = []): void
386390
{
@@ -390,7 +394,7 @@ public function cacheResponse(string $method, string $uri, ResponseInterface $re
390394
/**
391395
* Generate a cache key for the request.
392396
*
393-
* @param array<string, mixed> $options
397+
* @param array<string, mixed> $options
394398
*/
395399
public function generateKey(string $method, string $uri, array $options = []): string
396400
{
@@ -413,6 +417,8 @@ public function clear(): void
413417

414418
/**
415419
* Delete a specific cache entry.
420+
*
421+
* @param array<string, mixed> $options
416422
*/
417423
public function delete(string $method, string $uri, array $options = []): bool
418424
{
@@ -444,19 +450,19 @@ private function isCacheableStatusCode(int $statusCode): bool
444450
/**
445451
* Determine if a response should be stored.
446452
*
447-
* @param array<string, mixed> $requestOptions
453+
* @param array<string, mixed> $requestOptions
448454
*/
449455
private function shouldStoreResponse(
450456
string $method,
451457
ResponseInterface $response,
452458
CacheControl $cacheControl,
453459
array $requestOptions = [],
454460
): bool {
455-
if (! $this->isCacheableMethod($method)) {
461+
if (!$this->isCacheableMethod($method)) {
456462
return false;
457463
}
458464

459-
if (! $this->isCacheableStatusCode($response->getStatusCode())) {
465+
if (!$this->isCacheableStatusCode($response->getStatusCode())) {
460466
return false;
461467
}
462468

@@ -466,7 +472,7 @@ private function shouldStoreResponse(
466472
? ($cacheConfig['respect_headers'] ?? ($this->options['respect_cache_headers'] ?? true))
467473
: ($this->options['respect_cache_headers'] ?? true);
468474

469-
if (! $respectHeaders) {
475+
if (!$respectHeaders) {
470476
return true;
471477
}
472478

@@ -480,13 +486,13 @@ private function shouldStoreResponse(
480486
/**
481487
* Calculate TTL for a response.
482488
*
483-
* @param array<string, mixed> $requestOptions
489+
* @param array<string, mixed> $requestOptions
484490
*/
485491
private function calculateTtl(
486492
ResponseInterface $response,
487493
CacheControl $cacheControl,
488494
array $requestOptions = [],
489-
): ?int {
495+
): int {
490496
// Check for per-request TTL
491497
$cacheConfig = $requestOptions['cache'] ?? [];
492498
if (is_array($cacheConfig) && isset($cacheConfig['ttl'])) {
@@ -497,7 +503,7 @@ private function calculateTtl(
497503
if ($this->options['respect_cache_headers'] ?? true) {
498504
$isSharedCache = $this->options['is_shared_cache'] ?? false;
499505
$headerTtl = $cacheControl->getTtl($response, $isSharedCache);
500-
if ($headerTtl !== null) {
506+
if (null !== $headerTtl) {
501507
return $headerTtl;
502508
}
503509
}

0 commit comments

Comments
 (0)