From dd161ac69f98af1e6ad69d3e278ad8f1aa9980ac Mon Sep 17 00:00:00 2001 From: Pierre PLAZANET Date: Mon, 7 Nov 2022 16:08:23 +0100 Subject: [PATCH] release (#180) * Allow Symfony 6.2 (#177) * test: fix phpstan error (#179) * feat: remove support of versions of twig with security notices (#178) * refactor: fix phpstan error Co-authored-by: Tac Tacelosky --- .php-cs-fixer.dist.php | 1 + composer.json | 6 ++--- .../DictionaryRegistrationPassSpec.php | 7 +++-- src/Knp/DictionaryBundle/Dictionary.php | 1 + .../Dictionary/Collection.php | 4 +-- .../DictionaryBundle/Dictionary/Combined.php | 1 + .../DictionaryBundle/Dictionary/Invokable.php | 1 + .../DictionaryBundle/Dictionary/Iterator.php | 1 + .../DictionaryBundle/Dictionary/Simple.php | 1 + .../DictionaryBundle/Dictionary/Traceable.php | 1 + .../DictionaryBundle/Dictionary/Wrapper.php | 1 + .../Validator/Constraints/Dictionary.php | 1 + .../Constraints/DictionaryValidator.php | 14 +++++++++- .../SymfonyCompatibilityTrait.php | 1 + .../ValueTransformer/Constant.php | 26 +++++++++++++++---- 15 files changed, 54 insertions(+), 13 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 23b380e4..f24d441a 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -33,6 +33,7 @@ ->disable('method_chaining_indentation') ->disable('no_break_comment') ->disable('no_superfluous_phpdoc_tags') + ->disable('phpdoc_to_comment') ->getRules() ); diff --git a/composer.json b/composer.json index 074533db..5e6acd81 100644 --- a/composer.json +++ b/composer.json @@ -24,17 +24,17 @@ "symfony/http-foundation": "^5.4 || ^6.0", "symfony/http-kernel": "^5.4 || ^6.0", "symfony/validator": "^5.4 || ^6.0", - "twig/twig": "^2.14.11 || ^3.3.8" + "twig/twig": "^2.15.3 || ^3.4.3" }, "require-dev": { "beberlei/assert": "^3.3", "fakerphp/faker": "^1.19", "friends-of-phpspec/phpspec-code-coverage": "^6.1", - "friendsofphp/php-cs-fixer": "^3.8", + "friendsofphp/php-cs-fixer": "^3.13", "pedrotroller/php-cs-custom-fixer": "^2.28", "phpspec/phpspec": "^7.2", "phpspec/prophecy": "^1.15", - "phpstan/phpstan": "^1.7", + "phpstan/phpstan": "^1.9", "symfony/twig-bridge": "^5.4 || ^6.0", "symfony/var-dumper": "^5.4 || ^6.0" }, diff --git a/spec/Knp/DictionaryBundle/DependencyInjection/Compiler/DictionaryRegistrationPassSpec.php b/spec/Knp/DictionaryBundle/DependencyInjection/Compiler/DictionaryRegistrationPassSpec.php index b9cfc54b..b78c4ec4 100644 --- a/spec/Knp/DictionaryBundle/DependencyInjection/Compiler/DictionaryRegistrationPassSpec.php +++ b/spec/Knp/DictionaryBundle/DependencyInjection/Compiler/DictionaryRegistrationPassSpec.php @@ -19,8 +19,11 @@ function it_is_initializable() $this->shouldHaveType(DictionaryRegistrationPass::class); } - function it_registers_dictionaries(ContainerBuilder $container, Definition $dictionaries, Definition $definition) - { + function it_registers_dictionaries( + ContainerBuilder $container, + Definition $dictionaries, + Definition $definition + ) { $tags = ['foo' => [], 'bar' => [], 'baz' => []]; $container->getDefinition(Collection::class)->willReturn($dictionaries); diff --git a/src/Knp/DictionaryBundle/Dictionary.php b/src/Knp/DictionaryBundle/Dictionary.php index e3c209d9..d2856280 100644 --- a/src/Knp/DictionaryBundle/Dictionary.php +++ b/src/Knp/DictionaryBundle/Dictionary.php @@ -10,6 +10,7 @@ /** * @template E + * * @extends IteratorAggregate * @extends ArrayAccess */ diff --git a/src/Knp/DictionaryBundle/Dictionary/Collection.php b/src/Knp/DictionaryBundle/Dictionary/Collection.php index 0ac8ac48..4dcbde74 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Collection.php +++ b/src/Knp/DictionaryBundle/Dictionary/Collection.php @@ -48,10 +48,10 @@ public function offsetExists($offset): bool } /** - * @throws DictionaryNotFoundException - * * @return Dictionary * {@inheritdoc} + * + * @throws DictionaryNotFoundException */ public function offsetGet($offset): Dictionary { diff --git a/src/Knp/DictionaryBundle/Dictionary/Combined.php b/src/Knp/DictionaryBundle/Dictionary/Combined.php index 127499d1..331a47ec 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Combined.php +++ b/src/Knp/DictionaryBundle/Dictionary/Combined.php @@ -8,6 +8,7 @@ /** * @template E + * * @extends Wrapper */ final class Combined extends Wrapper diff --git a/src/Knp/DictionaryBundle/Dictionary/Invokable.php b/src/Knp/DictionaryBundle/Dictionary/Invokable.php index 75fd183f..bb7d9827 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Invokable.php +++ b/src/Knp/DictionaryBundle/Dictionary/Invokable.php @@ -11,6 +11,7 @@ /** * @template E + * * @implements Dictionary */ final class Invokable implements Dictionary diff --git a/src/Knp/DictionaryBundle/Dictionary/Iterator.php b/src/Knp/DictionaryBundle/Dictionary/Iterator.php index 403f7abe..2489eb49 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Iterator.php +++ b/src/Knp/DictionaryBundle/Dictionary/Iterator.php @@ -8,6 +8,7 @@ /** * @template E + * * @extends Wrapper */ final class Iterator extends Wrapper diff --git a/src/Knp/DictionaryBundle/Dictionary/Simple.php b/src/Knp/DictionaryBundle/Dictionary/Simple.php index a4170ca6..49c1414c 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Simple.php +++ b/src/Knp/DictionaryBundle/Dictionary/Simple.php @@ -10,6 +10,7 @@ /** * @template E + * * @implements Dictionary */ final class Simple implements Dictionary diff --git a/src/Knp/DictionaryBundle/Dictionary/Traceable.php b/src/Knp/DictionaryBundle/Dictionary/Traceable.php index 964c8aec..ca073197 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Traceable.php +++ b/src/Knp/DictionaryBundle/Dictionary/Traceable.php @@ -10,6 +10,7 @@ /** * @template E of mixed + * * @implements Dictionary */ final class Traceable implements Dictionary diff --git a/src/Knp/DictionaryBundle/Dictionary/Wrapper.php b/src/Knp/DictionaryBundle/Dictionary/Wrapper.php index 0c54185d..0d2f0f0f 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Wrapper.php +++ b/src/Knp/DictionaryBundle/Dictionary/Wrapper.php @@ -9,6 +9,7 @@ /** * @template E + * * @implements Dictionary */ abstract class Wrapper implements Dictionary diff --git a/src/Knp/DictionaryBundle/Validator/Constraints/Dictionary.php b/src/Knp/DictionaryBundle/Validator/Constraints/Dictionary.php index 463c3b32..c91f25a5 100644 --- a/src/Knp/DictionaryBundle/Validator/Constraints/Dictionary.php +++ b/src/Knp/DictionaryBundle/Validator/Constraints/Dictionary.php @@ -9,6 +9,7 @@ /** * @Annotation + * * @Target({"PROPERTY", "METHOD"}) */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)] diff --git a/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator.php b/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator.php index 5bf7b065..55b34422 100644 --- a/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator.php +++ b/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator.php @@ -4,6 +4,7 @@ namespace Knp\DictionaryBundle\Validator\Constraints; +use Exception; use Knp\DictionaryBundle\Dictionary\Collection; use Symfony\Component\Validator\ConstraintValidator; @@ -42,6 +43,17 @@ private function varToString($var): string ; } - return (string) $var; + if (\is_object($var) && method_exists($var, '__toString')) { + return $var->__toString(); + } + + if (settype($var, 'string')) { + /** + * @var string $var + */ + return $var; + } + + throw new Exception('Unable to transform var to string.'); } } diff --git a/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator/SymfonyCompatibilityTrait.php b/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator/SymfonyCompatibilityTrait.php index cdb5295e..bb9f50f9 100644 --- a/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator/SymfonyCompatibilityTrait.php +++ b/src/Knp/DictionaryBundle/Validator/Constraints/DictionaryValidator/SymfonyCompatibilityTrait.php @@ -14,6 +14,7 @@ default: throw new Exception('knplabs/dictionary-bundle is not compatible with the current version of symfony/validator: '.$version); + case '6.2': case '6.1': case '6.0': trait SymfonyCompatibilityTrait diff --git a/src/Knp/DictionaryBundle/ValueTransformer/Constant.php b/src/Knp/DictionaryBundle/ValueTransformer/Constant.php index 784d87ca..029631f9 100644 --- a/src/Knp/DictionaryBundle/ValueTransformer/Constant.php +++ b/src/Knp/DictionaryBundle/ValueTransformer/Constant.php @@ -4,12 +4,13 @@ namespace Knp\DictionaryBundle\ValueTransformer; +use Exception; use Knp\DictionaryBundle\ValueTransformer; use ReflectionClass; final class Constant implements ValueTransformer { - private string $pattern = '/^(?P.*)::(?P.*)$/'; + private const PATTERN = '/^(?P.*)::(?P.*)$/'; public function supports($value): bool { @@ -19,7 +20,7 @@ public function supports($value): bool $matches = []; - if (0 === preg_match($this->pattern, $value, $matches)) { + if (null === $matches = $this->extract($value)) { return false; } @@ -36,12 +37,27 @@ public function supports($value): bool public function transform($value) { - $matches = []; - - preg_match($this->pattern, $value, $matches); + if (null === $matches = $this->extract($value)) { + throw new Exception("Unable to resolve constant {$value}."); + } return (new ReflectionClass($matches['class'])) ->getConstant($matches['constant']) ; } + + /** + * @return ?array{class: class-string, constant: string} + */ + private function extract(string $value): ?array + { + if (preg_match(self::PATTERN, $value, $matches)) { + /** + * @var array{class: class-string, constant: string} $matches + */ + return $matches; + } + + return null; + } }