Skip to content

Commit

Permalink
strict typing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 25, 2024
1 parent cc75403 commit c73473a
Show file tree
Hide file tree
Showing 68 changed files with 444 additions and 899 deletions.
22 changes: 0 additions & 22 deletions .phpstan/classAliases.php

This file was deleted.

2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ parameters:
level: 1
paths:
- src
bootstrapFiles:
- .phpstan/classAliases.php
excludePaths:
analyseAndScan:
# contains code to support legacy phpunit versions
Expand Down
57 changes: 15 additions & 42 deletions src/CacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,10 @@ class CacheInvalidator
*/
public const CLEAR = 'clear';

/**
* @var ProxyClient
*/
private $cache;
private ProxyClient $cache;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
private EventDispatcherInterface $eventDispatcher;

/**
* Constructor.
*
* @param ProxyClient $cache HTTP cache
*/
public function __construct(ProxyClient $cache)
{
$this->cache = $cache;
Expand All @@ -91,11 +80,9 @@ public function __construct(ProxyClient $cache)
*
* @param string $operation one of the class constants
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function supports(string $operation)
public function supports(string $operation): bool
{
switch ($operation) {
case self::PATH:
Expand Down Expand Up @@ -125,7 +112,7 @@ public function supports(string $operation)
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
if ($this->eventDispatcher) {
if (isset($this->eventDispatcher)) {
// if you want to set a custom event dispatcher, do so right after instantiating
// the invalidator.
throw new \Exception('You may not change the event dispatcher once it is set.');
Expand All @@ -135,12 +122,10 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): v

/**
* Get the event dispatcher used by the cache invalidator.
*
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
public function getEventDispatcher(): EventDispatcherInterface
{
if (!$this->eventDispatcher) {
if (!isset($this->eventDispatcher)) {
$this->eventDispatcher = new EventDispatcher();
}

Expand All @@ -150,14 +135,12 @@ public function getEventDispatcher()
/**
* Invalidate a path or URL.
*
* @param string $path Path or URL
* @param array $headers HTTP headers (optional)
*
* @return $this
* @param string $path Path or URL
* @param array<string, string> $headers HTTP headers (optional)
*
* @throws UnsupportedProxyOperationException
*/
public function invalidatePath($path, array $headers = []): static
public function invalidatePath(string $path, array $headers = []): static
{
if (!$this->cache instanceof PurgeCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('PURGE');
Expand All @@ -171,16 +154,14 @@ public function invalidatePath($path, array $headers = []): static
/**
* Refresh a path or URL.
*
* @param string $path Path or URL
* @param array $headers HTTP headers (optional)
* @param string $path Path or URL
* @param array<string, string> $headers HTTP headers (optional)
*
* @see RefreshCapable::refresh()
*
* @return $this
*
* @throws UnsupportedProxyOperationException
*/
public function refreshPath($path, array $headers = []): static
public function refreshPath(string $path, array $headers = []): static
{
if (!$this->cache instanceof RefreshCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('REFRESH');
Expand All @@ -199,9 +180,7 @@ public function refreshPath($path, array $headers = []): static
*
* @see BanCapable::ban()
*
* @param array $headers HTTP headers that path must match to be banned
*
* @return $this
* @param array<string, string> $headers HTTP headers that path must match to be banned
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests
*/
Expand All @@ -221,9 +200,7 @@ public function invalidate(array $headers): static
*
* @see TagCapable::tags()
*
* @param array $tags Tags that should be removed/expired from the cache. An empty tag list is ignored.
*
* @return $this
* @param string[] $tags Tags that should be removed/expired from the cache. An empty tag list is ignored.
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support Tags invalidation
*/
Expand Down Expand Up @@ -257,8 +234,6 @@ public function invalidateTags(array $tags): static
* @param array|string|null $hosts Regular expression of a host name or list of
* exact host names to limit banning
*
* @return $this
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests
*
*@see BanCapable::banPath()
Expand All @@ -277,8 +252,6 @@ public function invalidateRegex(string $path, ?string $contentType = null, array
/**
* Clear the cache completely.
*
* @return $this
*
* @throws UnsupportedProxyOperationException if HTTP cache does not support clearing the cache completely
*/
public function clearCache(): static
Expand All @@ -299,7 +272,7 @@ public function clearCache(): static
*
* @throws ExceptionCollection if any errors occurred during flush
*/
public function flush()
public function flush(): int
{
try {
return $this->cache->flush();
Expand Down
17 changes: 7 additions & 10 deletions src/EventListener/LogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/
class LogListener implements EventSubscriberInterface
{
/**
* @var LoggerInterface
*/
private $logger;
private LoggerInterface $logger;

public function __construct(LoggerInterface $logger)
{
Expand All @@ -40,22 +37,22 @@ public static function getSubscribedEvents(): array
];
}

public function onProxyUnreachableError(Event $event)
public function onProxyUnreachableError(Event $event): void
{
$this->log(LogLevel::CRITICAL, $event->getException());
$this->log($event->getException());
}

public function onProxyResponseError(Event $event)
public function onProxyResponseError(Event $event): void
{
$this->log(LogLevel::CRITICAL, $event->getException());
$this->log($event->getException());
}

private function log($level, \Exception $exception)
private function log(\Throwable $exception): void
{
$context = [
'exception' => $exception,
];

$this->logger->log($level, $exception->getMessage(), $context);
$this->logger->log(LogLevel::CRITICAL, $exception->getMessage(), $context);
}
}
31 changes: 13 additions & 18 deletions src/Exception/ExceptionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
*/
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable, HttpCacheException
{
private $exceptions = [];
/**
* @var \Throwable[]
*/
private array $exceptions = [];

/**
* @param \Throwable[] $exceptions
*/
public function __construct(array $exceptions = [])
{
foreach ($exceptions as $exception) {
Expand All @@ -28,10 +34,8 @@ public function __construct(array $exceptions = [])

/**
* Add an exception to the collection.
*
* @return $this
*/
public function add(\Exception $e)
public function add(\Throwable $e): static
{
if (!$this->message) {
$this->message = $e->getMessage();
Expand All @@ -44,34 +48,25 @@ public function add(\Exception $e)

/**
* Get first exception in collection or null, if there is none.
*
* @return \Exception|null
*/
public function getFirst()
public function getFirst(): ?\Throwable
{
if ($this->count() > 0) {
return $this->exceptions[0];
}

return null;
}

/**
* Get exception iterator.
*
* @return \ArrayIterator
*/
#[\ReturnTypeWillChange]
public function getIterator()
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->exceptions);
}

/**
* Get number of exceptions in collection.
*
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
public function count(): int
{
return count($this->exceptions);
}
Expand Down
16 changes: 6 additions & 10 deletions src/Exception/InvalidUrlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
class InvalidUrlException extends InvalidArgumentException
{
/**
* @param string $url the invalid URL
* @param string $reason Further explanation why the URL was invalid (optional)
*
* @return self
* @param string $url the invalid URL
* @param string|null $reason Further explanation why the URL was invalid (optional)
*/
public static function invalidUrl($url, $reason = null)
public static function invalidUrl(string $url, ?string $reason = null): InvalidUrlException
{
$msg = sprintf('URL "%s" is invalid.', $url);
if ($reason) {
Expand All @@ -33,12 +31,10 @@ public static function invalidUrl($url, $reason = null)
}

/**
* @param string $server Invalid server
* @param array $allowed Allowed URL parts
*
* @return self
* @param string $server Invalid server
* @param string[] $allowed Allowed URL parts
*/
public static function invalidUrlParts($server, array $allowed)
public static function invalidUrlParts(string $server, array $allowed): InvalidUrlException
{
return new self(sprintf(
'Server "%s" is invalid. Only %s URL parts are allowed.',
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/MissingHostException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ class MissingHostException extends \RuntimeException implements HttpCacheExcepti
{
/**
* @param string $path the path that was asked to be invalidated
*
* @return MissingHostException
*/
public static function missingHost($path)
public static function missingHost(string $path): MissingHostException
{
$msg = sprintf(
'Path "%s" cannot be invalidated without a host. '
Expand Down
5 changes: 1 addition & 4 deletions src/Exception/ProxyResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
*/
class ProxyResponseException extends \RuntimeException implements HttpCacheException
{
/**
* @return ProxyResponseException
*/
public static function proxyResponse(HttpException $exception)
public static function proxyResponse(HttpException $exception): ProxyResponseException
{
$message = sprintf(
'%s error response "%s" from caching proxy',
Expand Down
5 changes: 1 addition & 4 deletions src/Exception/ProxyUnreachableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
*/
class ProxyUnreachableException extends \RuntimeException implements HttpCacheException
{
/**
* @return ProxyUnreachableException
*/
public static function proxyUnreachable(NetworkException $requestException)
public static function proxyUnreachable(NetworkException $requestException): ProxyUnreachableException
{
$message = sprintf(
'Request to caching proxy at %s failed with message "%s"',
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/UnsupportedProxyOperationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ class UnsupportedProxyOperationException extends \RuntimeException implements Ht
{
/**
* @param string $method name of the HTTP method that would be required
*
* @return UnsupportedProxyOperationException
*/
public static function cacheDoesNotImplement($method)
public static function cacheDoesNotImplement(string $method): UnsupportedProxyOperationException
{
return new self(sprintf('HTTP cache does not support %s requests', $method));
}
Expand Down
Loading

0 comments on commit c73473a

Please sign in to comment.