Skip to content

Commit

Permalink
fix: static typing and final classes (#621)
Browse files Browse the repository at this point in the history
* fix: static typing and final classes

* remove legacy code in Configuration class
  • Loading branch information
usu committed May 2, 2024
1 parent d0c6be7 commit 31ae54c
Show file tree
Hide file tree
Showing 26 changed files with 73 additions and 95 deletions.
4 changes: 2 additions & 2 deletions src/DependencyInjection/Compiler/HashGeneratorPass.php
Expand Up @@ -19,9 +19,9 @@
/**
* Add tagged provider to the hash generator for user context.
*/
class HashGeneratorPass implements CompilerPassInterface
final class HashGeneratorPass implements CompilerPassInterface
{
const TAG_NAME = 'fos_http_cache.user_context_provider';
public const TAG_NAME = 'fos_http_cache.user_context_provider';

/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/LoggerPass.php
Expand Up @@ -17,7 +17,7 @@
/**
* Announce the log listener to the symfony event system.
*/
class LoggerPass implements CompilerPassInterface
final class LoggerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/SessionListenerPass.php
Expand Up @@ -20,7 +20,7 @@
* AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER from being leaked to
* the client.
*/
class SessionListenerPass implements CompilerPassInterface
final class SessionListenerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
Expand Down
56 changes: 17 additions & 39 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -18,7 +18,6 @@
use JeanBeru\HttpCacheCloudFront\Proxy\CloudFront;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
Expand All @@ -34,7 +33,7 @@
* @author David de Boer <david@driebit.nl>
* @author David Buchmann <mail@davidbu.ch>
*/
class Configuration implements ConfigurationInterface
final class Configuration implements ConfigurationInterface
{
/**
* @var bool
Expand All @@ -55,13 +54,7 @@ public function __construct($debug)
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('fos_http_cache');

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->root('fos_http_cache');
} else {
$rootNode = $treeBuilder->getRootNode();
}
$rootNode = $treeBuilder->getRootNode();

$rootNode
->validate()
Expand Down Expand Up @@ -240,7 +233,7 @@ private function isFastly(array $v): bool
&& array_key_exists('fastly', $v['proxy_client']);
}

private function addCacheableResponseSection(ArrayNodeDefinition $rootNode)
private function addCacheableResponseSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -278,7 +271,7 @@ private function addCacheableResponseSection(ArrayNodeDefinition $rootNode)
/**
* Cache header control main section.
*/
private function addCacheControlSection(ArrayNodeDefinition $rootNode)
private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
{
$rules = $rootNode
->children()
Expand Down Expand Up @@ -366,7 +359,7 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode)
*
* @param bool $matchResponse whether to also add fields to match response
*/
private function addMatch(NodeBuilder $rules, $matchResponse = false)
private function addMatch(NodeBuilder $rules, $matchResponse = false): void
{
$match = $rules
->arrayNode('match')
Expand Down Expand Up @@ -610,19 +603,11 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode): void

/**
* Get the configuration node for a HTTP dispatcher in a proxy client.
*
* @return NodeDefinition
*/
private function getHttpDispatcherNode()
private function getHttpDispatcherNode(): ArrayNodeDefinition
{
$treeBuilder = new TreeBuilder('http');

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
$node = $treeBuilder->root('http');
} else {
$node = $treeBuilder->getRootNode();
}
$node = $treeBuilder->getRootNode();

$node
->fixXmlConfig('server')
Expand Down Expand Up @@ -650,16 +635,10 @@ private function getHttpDispatcherNode()
return $node;
}

private function getCloudflareHttpDispatcherNode()
private function getCloudflareHttpDispatcherNode(): ArrayNodeDefinition
{
$treeBuilder = new TreeBuilder('http');

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
$node = $treeBuilder->root('http');
} else {
$node = $treeBuilder->getRootNode();
}
$node = $treeBuilder->getRootNode();

$node
->addDefaultsIfNotSet()
Expand All @@ -680,10 +659,9 @@ private function getCloudflareHttpDispatcherNode()
return $node;
}

private function getFastlyHttpDispatcherNode()
private function getFastlyHttpDispatcherNode(): ArrayNodeDefinition
{
$treeBuilder = new TreeBuilder('http');

$node = $treeBuilder->getRootNode();

$node
Expand All @@ -709,7 +687,7 @@ private function getFastlyHttpDispatcherNode()
return $node;
}

private function addTestSection(ArrayNodeDefinition $rootNode)
private function addTestSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -752,7 +730,7 @@ private function addTestSection(ArrayNodeDefinition $rootNode)
/**
* Cache manager main section.
*/
private function addCacheManagerSection(ArrayNodeDefinition $rootNode)
private function addCacheManagerSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -792,7 +770,7 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode)
;
}

private function addTagSection(ArrayNodeDefinition $rootNode)
private function addTagSection(ArrayNodeDefinition $rootNode): void
{
$rules = $rootNode
->children()
Expand Down Expand Up @@ -848,7 +826,7 @@ private function addTagSection(ArrayNodeDefinition $rootNode)
;
}

private function addInvalidationSection(ArrayNodeDefinition $rootNode)
private function addInvalidationSection(ArrayNodeDefinition $rootNode): void
{
$rules = $rootNode
->children()
Expand Down Expand Up @@ -889,7 +867,7 @@ private function addInvalidationSection(ArrayNodeDefinition $rootNode)
/**
* User context main section.
*/
private function addUserContextListenerSection(ArrayNodeDefinition $rootNode)
private function addUserContextListenerSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -958,7 +936,7 @@ private function addUserContextListenerSection(ArrayNodeDefinition $rootNode)
;
}

private function addFlashMessageSection(ArrayNodeDefinition $rootNode)
private function addFlashMessageSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -988,7 +966,7 @@ private function addFlashMessageSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addDebugSection(ArrayNodeDefinition $rootNode)
private function addDebugSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down
12 changes: 6 additions & 6 deletions src/DependencyInjection/FOSHttpCacheExtension.php
Expand Up @@ -31,7 +31,7 @@
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class FOSHttpCacheExtension extends Extension
final class FOSHttpCacheExtension extends Extension
{
public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
{
Expand Down Expand Up @@ -579,7 +579,7 @@ private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loa
);
}

private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config): void
private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config): void
{
$loader->load('varnish_proxy.xml');
foreach ($config as $key => $value) {
Expand All @@ -590,7 +590,7 @@ private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoad
}
}

private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config): void
private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config): void
{
$loader->load('nginx_proxy.xml');
foreach ($config as $key => $value) {
Expand Down Expand Up @@ -627,7 +627,7 @@ private function loadInvalidatorRules(ContainerBuilder $container, array $config
}
}

private function validateUrl($url, $msg): void
private function validateUrl(string $url, string $msg): void
{
$prefixed = $this->prefixSchema($url);

Expand All @@ -636,7 +636,7 @@ private function validateUrl($url, $msg): void
}
}

private function prefixSchema($url)
private function prefixSchema(string $url): string
{
if (!str_contains($url, '://')) {
$url = sprintf('%s://%s', 'http', $url);
Expand All @@ -645,7 +645,7 @@ private function prefixSchema($url)
return $url;
}

private function getDefaultProxyClient(array $config)
private function getDefaultProxyClient(array $config): string
{
if (isset($config['default'])) {
return $config['default'];
Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/AbstractRuleListener.php
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractRuleListener
public function addRule(
RequestMatcherInterface $requestMatcher,
array $settings = []
) {
): void {
$this->rulesMap[] = [$requestMatcher, $settings];
}

Expand All @@ -40,7 +40,7 @@ public function addRule(
*
* @return array|false Settings to apply or false if no rule matched
*/
protected function matchRule(Request $request)
protected function matchRule(Request $request): array|false
{
foreach ($this->rulesMap as $elements) {
if ($elements[0]->matches($request)) {
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/AttributesListener.php
Expand Up @@ -15,7 +15,7 @@
*
* @author Yoann Chocteau <yoann@kezaweb.fr>
*/
class AttributesListener implements EventSubscriberInterface
final class AttributesListener implements EventSubscriberInterface
{
public function __construct(
private ControllerResolverInterface $controllerResolver
Expand Down
8 changes: 4 additions & 4 deletions src/EventListener/CacheControlListener.php
Expand Up @@ -26,7 +26,7 @@
* @author Lea Haensenberger <lea.haensenberger@gmail.com>
* @author David Buchmann <mail@davidbu.ch>
*/
class CacheControlListener implements EventSubscriberInterface
final class CacheControlListener implements EventSubscriberInterface
{
/**
* Whether to skip this response and not set any cache headers.
Expand All @@ -52,7 +52,7 @@ class CacheControlListener implements EventSubscriberInterface
* If not empty, add a debug header with that name to all responses,
* telling the cache proxy to add debug output.
*
* @var string|bool Name of the header or false to add no header
* @var string|false Name of the header or false to add no header
*/
private string|false $debugHeader;

Expand Down Expand Up @@ -96,7 +96,7 @@ public function onKernelResponse(ResponseEvent $event): void
$response = $event->getResponse();

if ($this->debugHeader) {
$response->headers->set($this->debugHeader, 1, false);
$response->headers->set($this->debugHeader, '1', false);
}

// do not change cache directives on non-cacheable requests.
Expand Down Expand Up @@ -125,7 +125,7 @@ public function onKernelResponse(ResponseEvent $event): void
&& null !== $options['reverse_proxy_ttl']
&& !$response->headers->has('X-Reverse-Proxy-TTL')
) {
$response->headers->set('X-Reverse-Proxy-TTL', (int) $options['reverse_proxy_ttl'], false);
$response->headers->set('X-Reverse-Proxy-TTL', $options['reverse_proxy_ttl'], false);
}

if (!empty($options['vary'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/InvalidationListener.php
Expand Up @@ -33,7 +33,7 @@
*
* @author David de Boer <david@driebit.nl>
*/
class InvalidationListener extends AbstractRuleListener implements EventSubscriberInterface
final class InvalidationListener extends AbstractRuleListener implements EventSubscriberInterface
{
private CacheManager $cacheManager;
private UrlGeneratorInterface $urlGenerator;
Expand Down Expand Up @@ -136,7 +136,7 @@ private function handleInvalidation(Request $request, Response $response): void
}

// Check configured invalidators
if (!$invalidatorConfigs = $this->matchRule($request, $response)) {
if (!$invalidatorConfigs = $this->matchRule($request)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/SwitchUserListener.php
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\SecurityEvents;

class SwitchUserListener implements EventSubscriberInterface
final class SwitchUserListener implements EventSubscriberInterface
{
private UserContextInvalidator $invalidator;

Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/TagListener.php
Expand Up @@ -27,7 +27,7 @@
*
* @author David de Boer <david@driebit.nl>
*/
class TagListener extends AbstractRuleListener implements EventSubscriberInterface
final class TagListener extends AbstractRuleListener implements EventSubscriberInterface
{
private CacheManager $cacheManager;
private SymfonyResponseTagger $symfonyResponseTagger;
Expand Down Expand Up @@ -108,7 +108,7 @@ public static function getSubscribedEvents(): array
* Get the tags from the attributes on the controller that was used in the
* request.
*
* @return array List of tags affected by the request
* @return string[] List of tags affected by the request
*/
private function getAttributeTags(Request $request): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/EventListener/UserContextListener.php
Expand Up @@ -34,7 +34,7 @@
* @author Stefan Paschke <stefan.paschke@gmail.com>
* @author Joel Wurtz <joel.wurtz@gmail.com>
*/
class UserContextListener implements EventSubscriberInterface
final class UserContextListener implements EventSubscriberInterface
{
private RequestMatcherInterface $requestMatcher;
private HashGenerator $hashGenerator;
Expand Down Expand Up @@ -136,7 +136,7 @@ public function onKernelRequest(RequestEvent $event): void
$response->setPublic();
if ($this->hasSessionListener) {
// header to avoid Symfony SessionListener overwriting the response to private
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 1);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, '1');
}
} else {
$response->setClientTtl(0);
Expand Down Expand Up @@ -201,7 +201,7 @@ public function onKernelResponse(ResponseEvent $event): void
// user hash header was in vary or just added here by "add_vary_on_hash"
if ($this->hasSessionListener && in_array($this->options['user_hash_header'], $vary, true)) {
// header to avoid Symfony SessionListener overwriting the response to private
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 1);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, '1');
}
} elseif ($this->options['add_vary_on_hash']) {
/*
Expand Down

0 comments on commit 31ae54c

Please sign in to comment.