Skip to content

Commit

Permalink
static typing tests and remove legacy things
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 23, 2024
1 parent 797ae1c commit 838f471
Show file tree
Hide file tree
Showing 45 changed files with 279 additions and 463 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"phpunit/phpunit": "^9"
},
"conflict": {
"toflar/psr6-symfony-http-cache-store": "<2.2.1"
"toflar/psr6-symfony-http-cache-store": "<2.2.1",
"phpunit/phpunit": "<8"
},
"suggest": {
"friendsofsymfony/http-cache-bundle": "For integration with the Symfony framework",
Expand Down
32 changes: 14 additions & 18 deletions src/CacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ public function __construct(ProxyClient $cache)
*
* @param string $operation one of the class constants
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function supports($operation)
public function supports(string $operation): bool
{
switch ($operation) {
case self::PATH:
Expand Down Expand Up @@ -123,7 +121,7 @@ public function supports($operation)
*
* @throws \Exception when trying to override the event dispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
if ($this->eventDispatcher) {
// if you want to set a custom event dispatcher, do so right after instantiating
Expand All @@ -135,10 +133,8 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)

/**
* Get the event dispatcher used by the cache invalidator.
*
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
public function getEventDispatcher(): EventDispatcherInterface|EventDispatcher
{
if (!$this->eventDispatcher) {
$this->eventDispatcher = new EventDispatcher();
Expand All @@ -157,7 +153,7 @@ public function getEventDispatcher()
*
* @throws UnsupportedProxyOperationException
*/
public function invalidatePath($path, array $headers = [])
public function invalidatePath($path, array $headers = []): static
{
if (!$this->cache instanceof PurgeCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('PURGE');
Expand All @@ -180,7 +176,7 @@ public function invalidatePath($path, array $headers = [])
*
* @throws UnsupportedProxyOperationException
*/
public function refreshPath($path, array $headers = [])
public function refreshPath($path, array $headers = []): static
{
if (!$this->cache instanceof RefreshCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('REFRESH');
Expand All @@ -205,7 +201,7 @@ public function refreshPath($path, array $headers = [])
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests
*/
public function invalidate(array $headers)
public function invalidate(array $headers): static
{
if (!$this->cache instanceof BanCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN');
Expand All @@ -227,7 +223,7 @@ public function invalidate(array $headers)
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support Tags invalidation
*/
public function invalidateTags(array $tags)
public function invalidateTags(array $tags): static
{
if (!$this->cache instanceof TagCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('Tags');
Expand All @@ -250,20 +246,20 @@ public function invalidateTags(array $tags)
* ['example.com', 'other.net']. If the parameter is empty, all hosts
* are matched.
*
* @see BanCapable::banPath()
*
* @param string $path Regular expression pattern for URI to
* invalidate
* @param string $contentType Regular expression pattern for the content
* @param string|null $contentType Regular expression pattern for the content
* type to limit banning, for instance 'text'
* @param array|string $hosts Regular expression of a host name or list of
* @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()
*
*/
public function invalidateRegex($path, $contentType = null, $hosts = null)
public function invalidateRegex(string $path, ?string $contentType = null, array|string|null $hosts = null): static
{
if (!$this->cache instanceof BanCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN');
Expand All @@ -281,7 +277,7 @@ public function invalidateRegex($path, $contentType = null, $hosts = null)
*
* @throws UnsupportedProxyOperationException if HTTP cache does not support clearing the cache completely
*/
public function clearCache()
public function clearCache(): static
{
if (!$this->cache instanceof ClearCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('CLEAR');
Expand All @@ -299,7 +295,7 @@ public function clearCache()
*
* @throws ExceptionCollection if any errors occurred during flush
*/
public function flush()
public function flush(): int
{
try {
return $this->cache->flush();
Expand Down
14 changes: 3 additions & 11 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@

class Event extends BaseEvent
{
private $exception;
private \Throwable $exception;

/**
* Set exception.
*/
public function setException(\Exception $exception)
public function setException(\Throwable $exception): void
{
$this->exception = $exception;
}

/**
* Get exception.
*
* @return \Exception
*/
public function getException()
public function getException(): \Throwable
{
return $this->exception;
}
Expand Down
22 changes: 8 additions & 14 deletions src/ResponseTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct(array $options = [])
*
* @return string
*/
public function getTagsHeaderName()
public function getTagsHeaderName(): string
{
return $this->headerFormatter->getTagsHeaderName();
}
Expand All @@ -89,10 +89,8 @@ public function getTagsHeaderName()
* Get the value for the HTTP tag header.
*
* This concatenates all tags and ensures correct encoding.
*
* @return string
*/
public function getTagsHeaderValue()
public function getTagsHeaderValue(): string
{
return $this->headerFormatter->getTagsHeaderValue($this->tags);
}
Expand All @@ -104,7 +102,7 @@ public function getTagsHeaderValue()
*
* @return string[]
*/
protected function parseTagsHeaderValue($headers): array
protected function parseTagsHeaderValue(array|string $headers): array
{
if ($this->headerFormatter instanceof TagHeaderParser) {
return $this->headerFormatter->parseTagsHeaderValue($headers);
Expand All @@ -117,10 +115,8 @@ protected function parseTagsHeaderValue($headers): array

/**
* Check whether the tag handler has any tags to set on the response.
*
* @return bool True if this handler will set at least one tag
*/
public function hasTags()
public function hasTags(): bool
{
return 0 < count($this->tags);
}
Expand All @@ -136,7 +132,7 @@ public function hasTags()
*
* @throws InvalidTagException
*/
public function addTags(array $tags)
public function addTags(array $tags): static
{
$filtered = array_filter($tags, 'is_string');
$filtered = array_filter($filtered, 'strlen');
Expand All @@ -156,7 +152,7 @@ public function addTags(array $tags)
* This is usually called after adding the tags header to a response. It is
* automatically called by the tagResponse method.
*/
public function clear()
public function clear(): void
{
$this->tags = [];
}
Expand All @@ -165,12 +161,10 @@ public function clear()
* Set tags on a response and then clear the tags.
*
* @param ResponseInterface $response Original response
* @param bool $replace Whether to replace the current tags
* @param bool $replace Whether to replace the current tags
* on the response
*
* @return ResponseInterface Tagged response
*/
public function tagResponse(ResponseInterface $response, $replace = false)
public function tagResponse(ResponseInterface $response, bool $replace = false): ResponseInterface
{
if (!$this->hasTags()) {
return $response;
Expand Down
51 changes: 25 additions & 26 deletions src/SymfonyCache/CacheInvalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,35 @@

namespace FOS\HttpCache\SymfonyCache;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;

if (interface_exists(CacheInvalidation::class)) {
return;
}

/*
* Symfony 6 introduced a BC break in the signature of the protected method HttpKernelInterface::fetch.
* Load the correct interface to match the signature.
/**
* Interface for a HttpCache that supports active cache invalidation.
*/
if (\class_exists(Kernel::class) && Kernel::MAJOR_VERSION >= 6) {
// Load class for Symfony >=6.0
\class_alias(
Compatibility\CacheInvalidationS6::class,
CacheInvalidation::class
);
} else {
// Load class for any other cases
\class_alias(
Compatibility\CacheInvalidationLegacy::class,
CacheInvalidation::class
);
}
interface CacheInvalidation extends HttpKernelInterface
{
/**
* Forwards the Request to the backend and determines whether the response should be stored.
*
* This methods is triggered when the cache missed or a reload is required.
*
* This method is present on HttpCache but must be public to allow event listeners to do
* refresh operations.
*
* @param Request $request A Request instance
* @param bool $catch Whether to process exceptions
*
* @return Response A Response instance
*/
public function fetch(Request $request, bool $catch = false): Response;

if (!interface_exists(CacheInvalidation::class)) {
/**
* Provide an empty interface for code scanners.
* Gets the store for cached responses.
*
* @return StoreInterface $store The store used by the HttpCache
*/
interface CacheInvalidation extends HttpKernelInterface
{
}
public function getStore(): StoreInterface;
}
47 changes: 0 additions & 47 deletions src/SymfonyCache/Compatibility/CacheInvalidationLegacy.php

This file was deleted.

47 changes: 0 additions & 47 deletions src/SymfonyCache/Compatibility/CacheInvalidationS6.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Functional/CacheInvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class CacheInvalidatorTest extends VarnishTestCase
{
public function testInvalidateTags()
public function testInvalidateTags(): void
{
if (getenv('VARNISH_MODULES_VERSION')) {
$uri = '/tags_xkey.php';
Expand Down
Loading

0 comments on commit 838f471

Please sign in to comment.