Skip to content

Commit 00d275a

Browse files
authored
Merge pull request #359 from pscheit/remove-tag-from-nodes
Fix that passing a tag to twig nodes is deprecated since twig 3.12
2 parents de34d69 + 0625e8f commit 00d275a

12 files changed

+64
-20
lines changed

.php-cs-fixer.dist.php

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'static_lambda' => true,
4545
'strict_param' => true,
4646
'ternary_to_null_coalescing' => true,
47+
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
4748
])
4849
->setUsingCache(true)
4950
->setRiskyAllowed(true)

phpstan.neon.dist

+9
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@ parameters:
1818
- '#^Dynamic call to static method Symfony\\Bundle\\FrameworkBundle\\Test\\\S+\(\)\.$#'
1919
# Ignore typing providers in tests
2020
- '#^Method Nelmio\\SecurityBundle\\Tests\\[^:]+Test::(provide\w+|\w+Provider)\(\) return type has no value type specified in iterable type (array|iterable)\.#'
21+
22+
# TODO: twig/twig:>3.12 remove this ignore
23+
-
24+
message: "#^Class Twig\\\\Node\\\\CaptureNode constructor invoked with 3 parameters, 2 required\\.$#"
25+
count: 1
26+
path: src/Twig/Node/CSPNode.php
27+
reportUnmatched: false
28+
2129
dynamicConstantNames:
2230
- Symfony\Component\HttpKernel\Kernel::VERSION
31+
- Twig\Environment::VERSION_ID

src/ContentSecurityPolicy/ContentSecurityPolicyParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function quoteKeywords(array $sourceList): array
5858
return array_map(
5959
static function (string $source) use ($keywords) {
6060
if (\in_array($source, $keywords, true)) {
61-
return sprintf("'%s'", $source);
61+
return \sprintf("'%s'", $source);
6262
}
6363

6464
return $source;

src/ContentSecurityPolicy/DirectiveSet.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private function normalizeSignatures(?array $signatures): ?array
243243
$normalizedSignatures['script-src'] = implode(
244244
' ',
245245
array_map(static function (string $value): string {
246-
return sprintf('\'%s\'', $value);
246+
return \sprintf('\'%s\'', $value);
247247
}, $signatures['script-src'])
248248
);
249249
}
@@ -252,7 +252,7 @@ private function normalizeSignatures(?array $signatures): ?array
252252
$normalizedSignatures['style-src'] = implode(
253253
' ',
254254
array_map(static function (string $value): string {
255-
return sprintf('\'%s\'', $value);
255+
return \sprintf('\'%s\'', $value);
256256
}, $signatures['style-src'])
257257
);
258258
}

src/ContentSecurityPolicy/ShaComputer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class ShaComputer implements ShaComputerInterface
2121
public function __construct(string $type)
2222
{
2323
if (!\in_array($type, ['sha256', 'sha384', 'sha512'], true)) {
24-
throw new \InvalidArgumentException(sprintf('Type "%s" is not supported', $type));
24+
throw new \InvalidArgumentException(\sprintf('Type "%s" is not supported', $type));
2525
}
2626

2727
$this->type = $type;
@@ -76,7 +76,7 @@ private function getFavorite(): string
7676

7777
private function compute(string $data): string
7878
{
79-
return sprintf('%s-%s', $this->type, base64_encode($this->computeHash($data)));
79+
return \sprintf('%s-%s', $this->type, base64_encode($this->computeHash($data)));
8080
}
8181

8282
private function computeHash(string $data): string

src/DependencyInjection/Configuration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private function addReferrerPolicyNode(): ArrayNodeDefinition
229229
->always(function (array $values): array {
230230
foreach ($values as $policy) {
231231
if (!\in_array($policy, $this->referrerPolicies, true)) {
232-
throw new \InvalidArgumentException(sprintf('Unknown referrer policy "%s". Possible referrer policies are "%s".', $policy, implode('", "', $this->referrerPolicies)));
232+
throw new \InvalidArgumentException(\sprintf('Unknown referrer policy "%s". Possible referrer policies are "%s".', $policy, implode('", "', $this->referrerPolicies)));
233233
}
234234
}
235235

src/ExternalRedirect/AllowListBasedTargetValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function isTargetAllowed(string $targetUrl): bool
4545
$host = parse_url($targetUrl, \PHP_URL_HOST);
4646

4747
if (!\is_string($host)) {
48-
throw new \InvalidArgumentException(sprintf('Url "%s" does not contain a host name.', $targetUrl));
48+
throw new \InvalidArgumentException(\sprintf('Url "%s" does not contain a host name.', $targetUrl));
4949
}
5050

5151
return preg_match('{^'.$this->allowList.'$}i', $host) > 0;

src/Signer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public function __construct(string $secret, string $algo, ?string $legacyAlgo =
3737
$this->separator = $separator;
3838

3939
if (!\in_array($this->algo, hash_algos(), true)) {
40-
throw new \InvalidArgumentException(sprintf("The supplied hashing algorithm '%s' is not supported by this system.", $this->algo));
40+
throw new \InvalidArgumentException(\sprintf("The supplied hashing algorithm '%s' is not supported by this system.", $this->algo));
4141
}
4242

4343
if (null !== $this->legacyAlgo && !\in_array($this->legacyAlgo, hash_algos(), true)) {
44-
throw new \InvalidArgumentException(sprintf("The supplied legacy hashing algorithm '%s' is not supported by this system.", $this->legacyAlgo));
44+
throw new \InvalidArgumentException(\sprintf("The supplied legacy hashing algorithm '%s' is not supported by this system.", $this->legacyAlgo));
4545
}
4646
}
4747

@@ -78,7 +78,7 @@ public function verifySignedValue(string $signedValue): bool
7878
public function getVerifiedRawValue(string $signedValue): string
7979
{
8080
if (!$this->verifySignedValue($signedValue)) {
81-
throw new \InvalidArgumentException(sprintf("The signature for '%s' was invalid.", $signedValue));
81+
throw new \InvalidArgumentException(\sprintf("The signature for '%s' was invalid.", $signedValue));
8282
}
8383

8484
$valueSignatureTuple = $this->splitSignatureFromSignedValue($signedValue);

src/Twig/Node/CSPNode.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* file that was distributed with this source code.
2323
*/
2424

25+
use Nelmio\SecurityBundle\Twig\Version as TwigVersion;
2526
use Twig\Attribute\YieldReady;
2627
use Twig\Compiler;
2728
use Twig\Node\CaptureNode;
@@ -36,11 +37,19 @@ final class CSPNode extends Node
3637
public function __construct(Node $body, int $lineno, string $tag, string $directive, ?string $sha = null)
3738
{
3839
if (class_exists(CaptureNode::class)) {
39-
$body = new CaptureNode($body, $lineno, $tag);
40+
if (TwigVersion::needsNodeTag()) {
41+
$body = new CaptureNode($body, $lineno, $tag);
42+
} else {
43+
$body = new CaptureNode($body, $lineno);
44+
}
4045
$body->setAttribute('raw', true);
4146
}
4247

43-
parent::__construct(['body' => $body], [], $lineno, $tag);
48+
if (TwigVersion::needsNodeTag()) {
49+
parent::__construct(['body' => $body], [], $lineno, $tag);
50+
} else {
51+
parent::__construct(['body' => $body], [], $lineno);
52+
}
4453
$this->sha = $sha;
4554
$this->directive = $directive;
4655
}
@@ -74,7 +83,7 @@ public function compile(Compiler $compiler): void
7483
} elseif ('style-src' === $this->directive) {
7584
$compiler->write("\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addStyle(\$content);\n");
7685
} else {
77-
throw new \InvalidArgumentException(sprintf('Unable to compile for directive "%s"', $this->directive));
86+
throw new \InvalidArgumentException(\sprintf('Unable to compile for directive "%s"', $this->directive));
7887
}
7988

8089
if (class_exists(CaptureNode::class)) {

src/Twig/Version.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Nelmio SecurityBundle.
7+
*
8+
* (c) Nelmio <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Nelmio\SecurityBundle\Twig;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class Version
20+
{
21+
public static function needsNodeTag(): bool
22+
{
23+
return \Twig\Environment::VERSION_ID < 301200;
24+
}
25+
}

tests/App/AppKernel.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public function registerBundles(): iterable
4242

4343
public function getCacheDir(): string
4444
{
45-
return sprintf('%scache', $this->getBaseDir());
45+
return \sprintf('%scache', $this->getBaseDir());
4646
}
4747

4848
public function getLogDir(): string
4949
{
50-
return sprintf('%slog', $this->getBaseDir());
50+
return \sprintf('%slog', $this->getBaseDir());
5151
}
5252

5353
public function getProjectDir(): string
@@ -62,16 +62,16 @@ public function getProjectDir(): string
6262
*/
6363
protected function configureRoutes($routes): void
6464
{
65-
$routes->import(sprintf('%s/config/routes.yaml', $this->getProjectDir()));
65+
$routes->import(\sprintf('%s/config/routes.yaml', $this->getProjectDir()));
6666
}
6767

6868
protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void
6969
{
70-
$loader->load(sprintf('%s/config/config.yaml', $this->getProjectDir()));
70+
$loader->load(\sprintf('%s/config/config.yaml', $this->getProjectDir()));
7171
}
7272

7373
private function getBaseDir(): string
7474
{
75-
return sprintf('%s/nelmio-security-bundle/var/', sys_get_temp_dir());
75+
return \sprintf('%s/nelmio-security-bundle/var/', sys_get_temp_dir());
7676
}
7777
}

tests/Listener/ExternalRedirectListenerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ public function provideRedirectOverrides(): iterable
9292
'/override',
9393
'redirect_to',
9494
$target,
95-
sprintf('/override?redirect_to=%s', urlencode($target)),
95+
\sprintf('/override?redirect_to=%s', urlencode($target)),
9696
];
9797

9898
yield 'override with parameter and with forwardAs' => [
9999
'/override?param=value',
100100
'redirect_to',
101101
$target,
102-
sprintf('/override?param=value&redirect_to=%s', urlencode($target)),
102+
\sprintf('/override?param=value&redirect_to=%s', urlencode($target)),
103103
];
104104
}
105105

0 commit comments

Comments
 (0)