From 9453889ee0abf0536963ecdec32396e075f04dcd Mon Sep 17 00:00:00 2001 From: aubes <3941035+aubes@users.noreply.github.com> Date: Tue, 3 Mar 2026 17:40:55 +0100 Subject: [PATCH] Symfony 7 compatibility --- README.md | 4 ++-- composer.json | 8 ++++---- src/CSP.php | 4 ++-- src/DependencyInjection/CSPExtension.php | 6 +----- src/DependencyInjection/Configuration.php | 4 +--- src/Twig/CSPExtension.php | 6 +++--- 6 files changed, 13 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a30adc6..12e8676 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ This Symfony bundle provides helper to configure [Content-Security-Policy](https://developer.mozilla.org/fr/docs/Web/HTTP/CSP) headers. It is compatible with : - * PHP 7.4 - * Symfony 5.4 + * PHP 7.4 | 8 + * Symfony 5.4 | 6 | 7 ## Installation diff --git a/composer.json b/composer.json index 002e060..974562a 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,11 @@ ], "require": { "php": ">=7.4", - "symfony/framework-bundle": "^5.4 |^6.0", + "symfony/framework-bundle": "^5.4 | ^6.0 | ^7.0", "symfony/polyfill-php80": "^1.0", - "symfony/http-foundation": "^5.4 |^6.0", - "symfony/http-kernel": "^5.4 |^6.0", - "symfony/twig-bundle": "^5.4 |^6.0" + "symfony/http-foundation": "^5.4 | ^6.0 | ^7.0", + "symfony/http-kernel": "^5.4 | ^6.0 | ^7.0", + "symfony/twig-bundle": "^5.4 | ^6.0 | ^7.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.1", diff --git a/src/CSP.php b/src/CSP.php index 99b89de..51e84c4 100644 --- a/src/CSP.php +++ b/src/CSP.php @@ -40,12 +40,12 @@ public function isEnabled(): bool return $this->enabled; } - public function addGroup(CSPPolicy $policy, string $groupName = null): void + public function addGroup(CSPPolicy $policy, ?string $groupName = null): void { $this->policies[$groupName ?? $this->defaultGroup] = $policy; } - public function addDirective(string $directive, string $value, string $groupName = null): void + public function addDirective(string $directive, string $value, ?string $groupName = null): void { $this->policies[$groupName ?? $this->defaultGroup]->addPolicy($directive, $value); } diff --git a/src/DependencyInjection/CSPExtension.php b/src/DependencyInjection/CSPExtension.php index 65e6eff..c3b571d 100644 --- a/src/DependencyInjection/CSPExtension.php +++ b/src/DependencyInjection/CSPExtension.php @@ -22,9 +22,6 @@ */ class CSPExtension extends Extension { - /** - * {@inheritdoc} - */ public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); @@ -38,9 +35,8 @@ public function load(array $configs, ContainerBuilder $container): void if ($config['default_group'] === null) { if (\count($config['groups']) > 1) { throw new \InvalidArgumentException('You must set default group when multiple groups are defined'); - } else { - $defaultGroup = \array_key_first($config['groups']); } + $defaultGroup = \array_key_first($config['groups']); } else { $defaultGroup = $config['default_group']; } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index e636960..657b843 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -10,11 +10,9 @@ class Configuration implements ConfigurationInterface { /** - * {@inheritdoc} - * * @psalm-suppress UndefinedMethod */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('csp'); $rootNode = $treeBuilder->getRootNode(); diff --git a/src/Twig/CSPExtension.php b/src/Twig/CSPExtension.php index 348daa1..2140b6a 100644 --- a/src/Twig/CSPExtension.php +++ b/src/Twig/CSPExtension.php @@ -30,7 +30,7 @@ public function getFunctions() ]; } - public function nonce(string $directive, string $groupName = null, string $nonce = null): string + public function nonce(string $directive, ?string $groupName = null, ?string $nonce = null): string { if ($nonce === null) { $nonce = \base64_encode($this->generator->generate(8)); @@ -41,12 +41,12 @@ public function nonce(string $directive, string $groupName = null, string $nonce return 'nonce="' . $nonce . '"'; } - public function scriptNonce(string $groupName = null, string $nonce = null): string + public function scriptNonce(?string $groupName = null, ?string $nonce = null): string { return $this->nonce(CSPDirective::SCRIPT_SRC, $groupName, $nonce); } - public function styleNonce(string $groupName = null, string $nonce = null): string + public function styleNonce(?string $groupName = null, ?string $nonce = null): string { return $this->nonce(CSPDirective::STYLE_SRC, $groupName, $nonce); }