From 7f2b90d3938374f3aec5fd9a14e47f7b45473f4b Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Fri, 5 Jan 2024 03:51:44 +0100 Subject: [PATCH 01/11] Added FormErrorNormalizer return statements --- .../Liform/Serializer/Normalizer/FormErrorNormalizer.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php index 66b3cd6..52babd4 100644 --- a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php +++ b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php @@ -40,7 +40,7 @@ public function __construct(TranslatorInterface $translator) /** * {@inheritdoc} */ - public function normalize($object, $format = null, array $context = []) + public function normalize($object, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { return [ 'code' => isset($context['status_code']) ? $context['status_code'] : null, @@ -52,11 +52,16 @@ public function normalize($object, $format = null, array $context = []) /** * {@inheritdoc} */ - public function supportsNormalization($data, $format = null) + public function supportsNormalization($data, $format = null): bool { return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid(); } + public function getSupportedTypes(?string $format): array + { + return [Form::class]; + } + /** * This code has been taken from JMSSerializer. * From 3440796100f7a303b298fa43029b92c4d8be63e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20=C3=85rhof?= Date: Fri, 5 Jan 2024 04:02:16 +0100 Subject: [PATCH 02/11] Update FormErrorNormalizer.php --- .../Liform/Serializer/Normalizer/FormErrorNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php index 52babd4..f3ae7ac 100644 --- a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php +++ b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php @@ -52,7 +52,7 @@ public function normalize($object, $format = null, array $context = []): array|s /** * {@inheritdoc} */ - public function supportsNormalization($data, $format = null): bool + public function supportsNormalization($data, $format = null, array $context = []): bool { return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid(); } From 6f0625734c4438babc14d193f5a70cb833078c3a Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Thu, 23 May 2024 18:21:10 -0400 Subject: [PATCH 03/11] use rector for 8.2 rules, fixes #73 --- composer.json | 13 +++++----- src/Limenius/Liform/FormUtil.php | 12 ++++----- .../Liform/Guesser/ValidatorGuesser.php | 14 +++++----- src/Limenius/Liform/Liform.php | 8 +----- src/Limenius/Liform/Resolver.php | 2 +- .../Normalizer/FormErrorNormalizer.php | 10 ++----- .../Normalizer/InitialValuesNormalizer.php | 12 +++------ .../Transformer/AbstractTransformer.php | 11 ++------ .../Liform/Transformer/ArrayTransformer.php | 2 +- .../Transformer/CompoundTransformer.php | 4 +-- tests/Limenius/Liform/Tests/ResolverTest.php | 6 ++--- .../InitialValuesNormalizerTest.php | 8 +++--- .../Transformer/ChoiceTransformerTest.php | 16 +++++------- .../Transformer/CommonTransformerTest.php | 26 +++++++------------ .../Transformer/CompoundTransformerTest.php | 2 +- .../Transformer/NumberTransformerTest.php | 2 +- .../Transformer/StringTransformerTest.php | 2 +- 17 files changed, 56 insertions(+), 94 deletions(-) diff --git a/composer.json b/composer.json index 9f20cb1..c6f6e0b 100644 --- a/composer.json +++ b/composer.json @@ -20,11 +20,11 @@ } }, "require": { - "php": "^8.0", - "symfony/form": "^5.4|^6.3|^7.0", - "symfony/translation": "^5.4|^6.3|^7.0", - "symfony/serializer": "^5.4|^6.3|^7.0", - "symfony/validator": "^5.4|^6.3|^7.0", + "php": "^8.2", + "symfony/form": "^5.4|^6.4|^7.0", + "symfony/translation": "^5.4|^6.4|^7.0", + "symfony/serializer": "^5.4|^6.4|^7.0", + "symfony/validator": "^5.4|^6.4|^7.0", "symfony/translation-contracts": "^1.0|^2.1|^3.0" }, "require-dev": { @@ -32,7 +32,8 @@ "escapestudios/symfony2-coding-standard": "^3.0", "phpunit/phpunit": "^8.0", "phpcompatibility/php-compatibility": "^9.3", - "phpstan/phpstan": "^1.8" + "phpstan/phpstan": "^1.8", + "rector/rector": "^1.1" }, "scripts": { "test": "phpunit", diff --git a/src/Limenius/Liform/FormUtil.php b/src/Limenius/Liform/FormUtil.php index 955bf84..9ebf7d3 100644 --- a/src/Limenius/Liform/FormUtil.php +++ b/src/Limenius/Liform/FormUtil.php @@ -27,7 +27,7 @@ class FormUtil public static function typeAncestry(FormInterface $form) { $types = []; - self::typeAncestryForType($form->getConfig()->getType(), $types); + self::typeAncestryForType($types, $form->getConfig()->getType()); return $types; } @@ -38,7 +38,7 @@ public static function typeAncestry(FormInterface $form) * * @return void */ - public static function typeAncestryForType(ResolvedFormTypeInterface $formType = null, array &$types) + public static function typeAncestryForType(array &$types, ResolvedFormTypeInterface $formType = null): void { if (!($formType instanceof ResolvedFormTypeInterface)) { return; @@ -46,17 +46,16 @@ public static function typeAncestryForType(ResolvedFormTypeInterface $formType = $types[] = $formType->getBlockPrefix(); - self::typeAncestryForType($formType->getParent(), $types); + self::typeAncestryForType($types, $formType->getParent()); } /** * Returns the dataClass of the form or its parents, if any * - * @param mixed $formType * * @return string|null the dataClass */ - public static function findDataClass($formType) + public static function findDataClass(mixed $formType) { if ($dataClass = $formType->getConfig()->getDataClass()) { return $dataClass; @@ -71,11 +70,10 @@ public static function findDataClass($formType) /** * @param FormInterface $form - * @param mixed $type * * @return boolean */ - public static function isTypeInAncestry(FormInterface $form, $type) + public static function isTypeInAncestry(FormInterface $form, mixed $type) { return in_array($type, self::typeAncestry($form)); } diff --git a/src/Limenius/Liform/Guesser/ValidatorGuesser.php b/src/Limenius/Liform/Guesser/ValidatorGuesser.php index c8b5794..2fbd9a5 100644 --- a/src/Limenius/Liform/Guesser/ValidatorGuesser.php +++ b/src/Limenius/Liform/Guesser/ValidatorGuesser.php @@ -26,9 +26,7 @@ class ValidatorGuesser extends ValidatorTypeGuesser */ public function guessMinLength(string $class, string $property) { - return $this->guess($class, $property, function (Constraint $constraint) { - return $this->guessMinLengthForConstraint($constraint); - }); + return $this->guess($class, $property, fn(Constraint $constraint) => $this->guessMinLengthForConstraint($constraint)); } /** @@ -40,18 +38,18 @@ public function guessMinLength(string $class, string $property) */ public function guessMinLengthForConstraint(Constraint $constraint) { - switch (get_class($constraint)) { - case 'Symfony\Component\Validator\Constraints\Length': + switch ($constraint::class) { + case \Symfony\Component\Validator\Constraints\Length::class: if (is_numeric($constraint->min)) { return new ValueGuess($constraint->min, Guess::HIGH_CONFIDENCE); } break; - case 'Symfony\Component\Validator\Constraints\Type': - if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) { + case \Symfony\Component\Validator\Constraints\Type::class: + if (in_array($constraint->type, ['double', 'float', 'numeric', 'real'])) { return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); } break; - case 'Symfony\Component\Validator\Constraints\Range': + case \Symfony\Component\Validator\Constraints\Range::class: if (is_numeric($constraint->min)) { return new ValueGuess(strlen((string) $constraint->min), Guess::LOW_CONFIDENCE); } diff --git a/src/Limenius/Liform/Liform.php b/src/Limenius/Liform/Liform.php index e7dd7ab..1149899 100644 --- a/src/Limenius/Liform/Liform.php +++ b/src/Limenius/Liform/Liform.php @@ -19,11 +19,6 @@ */ class Liform implements LiformInterface { - /** - * @var ResolverInterface - */ - private $resolver; - /** * @var ExtensionInterface[] */ @@ -32,9 +27,8 @@ class Liform implements LiformInterface /** * @param ResolverInterface $resolver */ - public function __construct(ResolverInterface $resolver) + public function __construct(private readonly ResolverInterface $resolver) { - $this->resolver = $resolver; } /** diff --git a/src/Limenius/Liform/Resolver.php b/src/Limenius/Liform/Resolver.php index 2dafe1d..b7fca9e 100644 --- a/src/Limenius/Liform/Resolver.php +++ b/src/Limenius/Liform/Resolver.php @@ -30,7 +30,7 @@ class Resolver implements ResolverInterface * @param TransformerInterface $transformer * @param string|null $widget */ - public function setTransformer($formType, TransformerInterface $transformer, $widget = null) + public function setTransformer($formType, TransformerInterface $transformer, $widget = null): void { $this->transformers[$formType] = [ 'transformer' => $transformer, diff --git a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php index f1b4d97..3315763 100644 --- a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php +++ b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php @@ -25,17 +25,11 @@ */ class FormErrorNormalizer implements NormalizerInterface { - /** - * @var TranslatorInterface - */ - private $translator; - /** * @param TranslatorInterface $translator */ - public function __construct(TranslatorInterface $translator) + public function __construct(private readonly TranslatorInterface $translator) { - $this->translator = $translator; } /** @@ -44,7 +38,7 @@ public function __construct(TranslatorInterface $translator) public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null { return [ - 'code' => isset($context['status_code']) ? $context['status_code'] : null, + 'code' => $context['status_code'] ?? null, 'message' => 'Validation Failed', 'errors' => $this->convertFormToArray($object), ]; diff --git a/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php b/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php index bda9c78..64c548c 100644 --- a/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php +++ b/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php @@ -64,7 +64,7 @@ private function getValues(Form $form, FormView $formView): mixed } } // Force serialization as {} instead of [] - $data = (object) array(); + $data = (object) []; foreach ($formView->children as $name => $child) { // Avoid unknown field error when csrf_protection is true // CSRF token should be extracted another way @@ -75,13 +75,7 @@ private function getValues(Form $form, FormView $formView): mixed return (array) $data; } else { - // handle separatedly the case with checkboxes, so the result is - // true/false instead of 1/0 - if (isset($formView->vars['checked'])) { - return $formView->vars['checked']; - } - - return $formView->vars['value']; + return $formView->vars['checked'] ?? $formView->vars['value']; } } @@ -93,7 +87,7 @@ private function getValues(Form $form, FormView $formView): mixed */ private function normalizeMultipleExpandedChoice(FormView $formView): array { - $data = array(); + $data = []; foreach ($formView->children as $name => $child) { if ($child->vars['checked']) { $data[] = $child->vars['value']; diff --git a/src/Limenius/Liform/Transformer/AbstractTransformer.php b/src/Limenius/Liform/Transformer/AbstractTransformer.php index 302487c..cc61596 100644 --- a/src/Limenius/Liform/Transformer/AbstractTransformer.php +++ b/src/Limenius/Liform/Transformer/AbstractTransformer.php @@ -20,14 +20,8 @@ */ abstract class AbstractTransformer implements TransformerInterface { - protected TranslatorInterface $translator; - - protected ?FormTypeGuesserInterface $validatorGuesser = null; - - public function __construct(TranslatorInterface $translator, FormTypeGuesserInterface $validatorGuesser = null) + public function __construct(protected TranslatorInterface $translator, protected ?FormTypeGuesserInterface $validatorGuesser = null) { - $this->translator = $translator; - $this->validatorGuesser = $validatorGuesser; } public function isRequired(FormInterface $form): bool @@ -133,8 +127,7 @@ protected function addDescription(FormInterface $form, array $schema): array return $schema; } - /** @param mixed $configWidget */ - protected function addWidget(FormInterface $form, array $schema, $configWidget): array + protected function addWidget(FormInterface $form, array $schema, mixed $configWidget): array { if ($liform = $form->getConfig()->getOption('liform')) { if (isset($liform['widget']) && $widget = $liform['widget']) { diff --git a/src/Limenius/Liform/Transformer/ArrayTransformer.php b/src/Limenius/Liform/Transformer/ArrayTransformer.php index fa19742..00aa771 100644 --- a/src/Limenius/Liform/Transformer/ArrayTransformer.php +++ b/src/Limenius/Liform/Transformer/ArrayTransformer.php @@ -32,7 +32,7 @@ class ArrayTransformer extends AbstractTransformer * @param FormTypeGuesserInterface|null $validatorGuesser * @param ResolverInterface $resolver */ - public function __construct(TranslatorInterface $translator, FormTypeGuesserInterface $validatorGuesser = null, ResolverInterface $resolver) + public function __construct(TranslatorInterface $translator, ResolverInterface $resolver, FormTypeGuesserInterface $validatorGuesser = null) { parent::__construct($translator, $validatorGuesser); $this->resolver = $resolver; diff --git a/src/Limenius/Liform/Transformer/CompoundTransformer.php b/src/Limenius/Liform/Transformer/CompoundTransformer.php index 8167b9b..adabbbe 100644 --- a/src/Limenius/Liform/Transformer/CompoundTransformer.php +++ b/src/Limenius/Liform/Transformer/CompoundTransformer.php @@ -28,10 +28,10 @@ class CompoundTransformer extends AbstractTransformer /** * @param TranslatorInterface $translator - * @param FormTypeGuesserInterface|null $validatorGuesser * @param ResolverInterface $resolver + * @param FormTypeGuesserInterface|null $validatorGuesser */ - public function __construct(TranslatorInterface $translator, FormTypeGuesserInterface $validatorGuesser = null, ResolverInterface $resolver) + public function __construct(TranslatorInterface $translator, ResolverInterface $resolver, FormTypeGuesserInterface $validatorGuesser = null) { parent::__construct($translator, $validatorGuesser); $this->resolver = $resolver; diff --git a/tests/Limenius/Liform/Tests/ResolverTest.php b/tests/Limenius/Liform/Tests/ResolverTest.php index fa1b368..1b05914 100644 --- a/tests/Limenius/Liform/Tests/ResolverTest.php +++ b/tests/Limenius/Liform/Tests/ResolverTest.php @@ -24,13 +24,13 @@ */ class ResolverTest extends TypeTestCase { - public function testConstruct() + public function testConstruct(): void { $resolver = new Resolver(); $this->assertInstanceOf(Resolver::class, $resolver); } - public function testCannotResolve() + public function testCannotResolve(): void { $this->expectException(TransformerException::class); @@ -39,7 +39,7 @@ public function testCannotResolve() $this->assertArrayHasKey('transformer', $resolver->resolve($form)); } - public function testResolve() + public function testResolve(): void { $resolver = new Resolver(); $stub = $this->createMock(StringTransformer::class); diff --git a/tests/Limenius/Liform/Tests/Serializer/InitialValuesNormalizerTest.php b/tests/Limenius/Liform/Tests/Serializer/InitialValuesNormalizerTest.php index 2200d91..1701735 100644 --- a/tests/Limenius/Liform/Tests/Serializer/InitialValuesNormalizerTest.php +++ b/tests/Limenius/Liform/Tests/Serializer/InitialValuesNormalizerTest.php @@ -24,13 +24,13 @@ */ class InitialValuesNormalizerTest extends LiformTestCase { - public function testConstruct() + public function testConstruct(): void { $normalizer = new InitialValuesNormalizer(); $this->assertInstanceOf(InitialValuesNormalizer::class, $normalizer); } - public function testSimpleCase() + public function testSimpleCase(): void { $form = $this->factory->create(FormType::class , ['firstName' => 'Joe']) ->add('firstName', TextType::class) @@ -40,7 +40,7 @@ public function testSimpleCase() $this->assertEquals('Joe', $data['firstName']); } - public function testChoiceExpandedMultiple() + public function testChoiceExpandedMultiple(): void { $form = $this->factory->create(FormType::class, ['firstName' => ['A']]) ->add( @@ -59,7 +59,7 @@ public function testChoiceExpandedMultiple() } - public function testChoiceExpanded() + public function testChoiceExpanded(): void { $form = $this->factory->create(FormType::class, ['firstName' => 'A']) ->add( diff --git a/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php index cd7908f..7a243c1 100644 --- a/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php @@ -25,7 +25,7 @@ */ class ChoiceTransformerTest extends LiformTestCase { - public function testChoice() + public function testChoice(): void { $form = $this->factory->create(FormType::class) ->add( @@ -39,9 +39,7 @@ public function testChoice() // 4 times: firstName, form, and the two choices $this->translator->expects($this->exactly(4)) ->method('trans') - ->will($this->returnCallback(function ($str) { - return $str.'-translated'; - })); + ->will($this->returnCallback(fn($str) => $str.'-translated')); $resolver = new Resolver(); $resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null)); @@ -56,7 +54,7 @@ public function testChoice() $this->assertEquals(['A', 'B'], $transformed['properties']['firstName']['enum']); } - public function testChoiceExpanded() + public function testChoiceExpanded(): void { $form = $this->factory->create(FormType::class) ->add( @@ -71,9 +69,7 @@ public function testChoiceExpanded() // 4 times: firstName, form, and the two choices $this->translator->expects($this->exactly(4)) ->method('trans') - ->will($this->returnCallback(function ($str) { - return $str.'-translated'; - })); + ->will($this->returnCallback(fn($str) => $str.'-translated')); $resolver = new Resolver(); $resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null)); @@ -90,7 +86,7 @@ public function testChoiceExpanded() $this->assertEquals('choice-expanded', $transformed['properties']['firstName']['widget']); } - public function testChoiceMultiple() + public function testChoiceMultiple(): void { $form = $this->factory->create(FormType::class) ->add( @@ -112,7 +108,7 @@ public function testChoiceMultiple() $this->assertArrayNotHasKey('widget', $transformed['properties']['firstName']); } - public function testChoiceMultipleExpanded() + public function testChoiceMultipleExpanded(): void { $form = $this->factory->create(FormType::class) ->add( diff --git a/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php index 15c5852..0a9c734 100644 --- a/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php @@ -25,7 +25,7 @@ */ class CommonTransformerTest extends LiformTestCase { - public function testRequired() + public function testRequired(): void { $form = $this->factory->create(FormType::class) ->add( @@ -44,7 +44,7 @@ public function testRequired() $this->assertContains('firstName', $transformed['required']); } - public function testDescription() + public function testDescription(): void { $form = $this->factory->create(FormType::class) ->add( @@ -56,9 +56,7 @@ public function testDescription() $resolver->setTransformer('text', new StringTransformer($this->translator)); $this->translator->method('trans') - ->will($this->returnCallback(function ($description) { - return $description; - })); + ->will($this->returnCallback(fn($description) => $description)); $transformer = new CompoundTransformer($this->translator, null, $resolver); $transformed = $transformer->transform($form); @@ -67,7 +65,7 @@ public function testDescription() $this->assertSame($description, $transformed['properties']['firstName']['description']); } - public function testDescriptionFromFormHelp() + public function testDescriptionFromFormHelp(): void { $form = $this->factory->create(FormType::class) ->add( @@ -81,9 +79,7 @@ public function testDescriptionFromFormHelp() $resolver = new Resolver(); $resolver->setTransformer('text', new StringTransformer($this->translator)); $this->translator->method('trans') - ->will($this->returnCallback(function ($description) { - return $description; - })); + ->will($this->returnCallback(fn($description) => $description)); $transformer = new CompoundTransformer($this->translator, null, $resolver); $transformed = $transformer->transform($form); @@ -92,7 +88,7 @@ public function testDescriptionFromFormHelp() $this->assertSame($description, $transformed['properties']['firstName']['description']); } - public function testDescriptionFromFormHelpOverriddenByLiformDescription() + public function testDescriptionFromFormHelpOverriddenByLiformDescription(): void { $form = $this->factory->create(FormType::class) ->add( @@ -110,9 +106,7 @@ public function testDescriptionFromFormHelpOverriddenByLiformDescription() $resolver->setTransformer('text', new StringTransformer($this->translator)); $transformer = new CompoundTransformer($this->translator, null, $resolver); $this->translator->method('trans') - ->will($this->returnCallback(function ($description) { - return $description; - })); + ->will($this->returnCallback(fn($description) => $description)); $transformed = $transformer->transform($form); $this->assertTrue(is_array($transformed)); @@ -120,7 +114,7 @@ public function testDescriptionFromFormHelpOverriddenByLiformDescription() $this->assertSame($description, $transformed['properties']['firstName']['description']); } - public function testLabel() + public function testLabel(): void { $form = $this->factory->create(FormType::class) ->add( @@ -142,7 +136,7 @@ public function testLabel() $this->assertEquals('a label', $transformed['properties']['firstName']['title']); } - public function testWidget() + public function testWidget(): void { $form = $this->factory->create(FormType::class) ->add( @@ -159,7 +153,7 @@ public function testWidget() $this->assertArrayHasKey('widget', $transformed['properties']['firstName']); } - public function testWidgetViaTransformerDefinition() + public function testWidgetViaTransformerDefinition(): void { $form = $this->factory->create(FormType::class) ->add( diff --git a/tests/Limenius/Liform/Tests/Transformer/CompoundTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/CompoundTransformerTest.php index da41067..0861759 100644 --- a/tests/Limenius/Liform/Tests/Transformer/CompoundTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/CompoundTransformerTest.php @@ -30,7 +30,7 @@ */ class CompoundTransformerTest extends LiformTestCase { - public function testOrder() + public function testOrder(): void { $form = $this->factory->create(FormType::class) ->add('firstName', TextType::class) diff --git a/tests/Limenius/Liform/Tests/Transformer/NumberTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/NumberTransformerTest.php index 7e1926e..11937d1 100644 --- a/tests/Limenius/Liform/Tests/Transformer/NumberTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/NumberTransformerTest.php @@ -25,7 +25,7 @@ */ class NumberTransformerTest extends LiformTestCase { - public function testPattern() + public function testPattern(): void { $form = $this->factory->create(FormType::class) ->add( diff --git a/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php index cc8c12b..ad1a01d 100644 --- a/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php @@ -25,7 +25,7 @@ */ class StringTransformerTest extends LiformTestCase { - public function testPattern() + public function testPattern(): void { $form = $this->factory->create(FormType::class) ->add( From ea9582cdca28ef2caf3b21fb713e399a8867718b Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Thu, 23 May 2024 18:24:43 -0400 Subject: [PATCH 04/11] add rector --- rector.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 rector.php diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..258e0fa --- /dev/null +++ b/rector.php @@ -0,0 +1,17 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + // uncomment to reach your current PHP version + ->withPhpSets(php82: true) + ->withRules([ + AddVoidReturnTypeWhereNoReturnRector::class, + ]); From c1008051fe6854ed6e1ad9e2862c5ca9b41917cd Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 15 Oct 2024 08:33:28 -0400 Subject: [PATCH 05/11] drop old PHP, add newer versions --- .github/workflows/static-analysis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 7f7fb9d..4cf1c16 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -6,14 +6,14 @@ on: jobs: phpstan: name: "PHPStan" - runs-on: "ubuntu-20.04" + runs-on: "ubuntu-24.04" strategy: matrix: php-version: - - "7.4" - - "8.0" - "8.1" + - "8.2" + - "8.3" steps: - name: "Checkout" uses: "actions/checkout@v2" From ddb740f715d28ece88b142bac39f7a7531321a3f Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 15 Oct 2024 08:39:01 -0400 Subject: [PATCH 06/11] bump minimum to 8.2 --- .github/workflows/phpunit.yml | 9 ++++----- .github/workflows/static-analysis.yml | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 35acac9..b4ab93f 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -6,19 +6,18 @@ on: jobs: phpunit: name: "PHPUnit" - runs-on: "ubuntu-20.04" + runs-on: "ubuntu-24.04" strategy: matrix: php-version: - - "7.4" - - "8.0" - - "8.1" + - "8.2" + - "8.3" deps: - "normal" include: - deps: "low" - php-version: "7.4" + php-version: "8.2" steps: - name: "Checkout" diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 4cf1c16..4db6e36 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -11,7 +11,6 @@ jobs: strategy: matrix: php-version: - - "8.1" - "8.2" - "8.3" steps: From 27a91358fedad13a5c3de19656b36af4d7599973 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 15 Oct 2024 08:52:47 -0400 Subject: [PATCH 07/11] tweaks for phpstan --- composer.json | 2 +- .../Serializer/Normalizer/FormErrorNormalizer.php | 11 +++++------ .../Tests/Transformer/ChoiceTransformerTest.php | 1 - 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index f575e5e..ede6d09 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "symfony/translation-contracts": "^1.0|^2.1|^3.0" }, "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^v0.7.2", + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", "escapestudios/symfony2-coding-standard": "^3.0", "phpunit/phpunit": "^8.0", "phpcompatibility/php-compatibility": "^9.3", diff --git a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php index 4ad4e80..4add0df 100644 --- a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php +++ b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php @@ -35,7 +35,7 @@ public function __construct(private readonly TranslatorInterface $translator) /** * {@inheritdoc} */ - public function normalize($object, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null + public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { return [ 'code' => $context['status_code'] ?? null, @@ -50,11 +50,14 @@ public function normalize($object, $format = null, array $context = []): array|s * @param null $format * @param array $context */ - public function supportsNormalization($data, $format = null, array $context = []): bool + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid(); } + /** + * {@inheritdoc} + */ public function getSupportedTypes(?string $format): array { return [Form::class]; @@ -120,8 +123,4 @@ private function getErrorMessage(FormError $error) return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); } - public function getSupportedTypes(?string $format): array - { - return [Form::class]; - } } diff --git a/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php index d684e6f..575956f 100644 --- a/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php @@ -125,7 +125,6 @@ public function testChoiceMultipleExpanded(): void $resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null)); $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('items', $transformed['properties']['firstName']); $this->assertEquals('array', $transformed['properties']['firstName']['type']); $this->assertArrayHasKey('widget', $transformed['properties']['firstName']); From 261c909952cd19dcdb7aca67aad22f4fab419f54 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 15 Oct 2024 08:53:31 -0400 Subject: [PATCH 08/11] bump to phpunit 9 --- composer.json | 2 +- phpunit.xml.dist | 32 +++++++++++++++----------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index ede6d09..b48a4a1 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^1.0", "escapestudios/symfony2-coding-standard": "^3.0", - "phpunit/phpunit": "^8.0", + "phpunit/phpunit": "^9.0", "phpcompatibility/php-compatibility": "^9.3", "phpstan/phpstan": "^1.8" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3414d22..b42eaa1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,17 @@ - - - - - ./tests/ - - - - - - ./ - - ./Tests - ./vendor - - - + + + + ./ + + + ./Tests + ./vendor + + + + + ./tests/ + + From d9c5b0a30e6e54e1c617c5557d66d6e124f90d78 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 15 Oct 2024 08:55:55 -0400 Subject: [PATCH 09/11] bump to phpunit 10.5 --- composer.json | 2 +- phpunit.xml.dist | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index b48a4a1..32517c7 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^1.0", "escapestudios/symfony2-coding-standard": "^3.0", - "phpunit/phpunit": "^9.0", + "phpunit/phpunit": "^10.5", "phpcompatibility/php-compatibility": "^9.3", "phpstan/phpstan": "^1.8" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b42eaa1..fe80140 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,11 @@ - - + + + + ./tests/ + + + ./ @@ -8,10 +13,5 @@ ./Tests ./vendor - - - - ./tests/ - - + From 99684665922f67f6708eb34f6fa87276b7584337 Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 15 Oct 2024 09:04:48 -0400 Subject: [PATCH 10/11] bump to phpunit 11.5, remove deprecations --- composer.json | 5 +++-- rector.php | 6 +++++- .../Liform/Tests/Transformer/ChoiceTransformerTest.php | 7 ++----- .../Liform/Tests/Transformer/CommonTransformerTest.php | 10 +++------- .../Liform/Tests/Transformer/StringTransformerTest.php | 1 - 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 32517c7..0a898bd 100644 --- a/composer.json +++ b/composer.json @@ -30,9 +30,10 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^1.0", "escapestudios/symfony2-coding-standard": "^3.0", - "phpunit/phpunit": "^10.5", + "phpunit/phpunit": "^11.4", "phpcompatibility/php-compatibility": "^9.3", - "phpstan/phpstan": "^1.8" + "phpstan/phpstan": "^1.8", + "rector/rector": "^1.2" }, "scripts": { "test": "phpunit", diff --git a/rector.php b/rector.php index 258e0fa..5b1e92f 100644 --- a/rector.php +++ b/rector.php @@ -4,6 +4,7 @@ use Rector\Config\RectorConfig; use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; +use Rector\PHPUnit\Set\PHPUnitSetList; return RectorConfig::configure() ->withPaths([ @@ -11,7 +12,10 @@ __DIR__ . '/tests', ]) // uncomment to reach your current PHP version - ->withPhpSets(php82: true) + ->withPhpSets(php83: true) + ->withSets([ + PHPUnitSetList::PHPUNIT_110 + ]) ->withRules([ AddVoidReturnTypeWhereNoReturnRector::class, ]); diff --git a/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php index 575956f..cb2327e 100644 --- a/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php @@ -39,13 +39,12 @@ public function testChoice(): void // 4 times: firstName, form, and the two choices $this->translator->expects($this->exactly(4)) ->method('trans') - ->will($this->returnCallback(fn($str) => $str.'-translated')); + ->willReturnCallback(fn($str) => $str.'-translated'); $resolver = new Resolver(); $resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null)); $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('enum_titles', $transformed['properties']['firstName']); $this->assertArrayHasKey('enum_titles', $transformed['properties']['firstName']['options']); $this->assertEquals(['a-translated', 'b-translated'], $transformed['properties']['firstName']['enum_titles']); @@ -69,13 +68,12 @@ public function testChoiceExpanded(): void // 4 times: firstName, form, and the two choices $this->translator->expects($this->exactly(4)) ->method('trans') - ->will($this->returnCallback(fn($str) => $str.'-translated')); + ->willReturnCallback(fn($str) => $str.'-translated'); $resolver = new Resolver(); $resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null)); $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('enum_titles', $transformed['properties']['firstName']); $this->assertArrayHasKey('enum_titles', $transformed['properties']['firstName']['options']); $this->assertEquals(['a-translated', 'b-translated'], $transformed['properties']['firstName']['enum_titles']); @@ -102,7 +100,6 @@ public function testChoiceMultiple(): void $resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null)); $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('items', $transformed['properties']['firstName']); $this->assertEquals('array', $transformed['properties']['firstName']['type']); $this->assertArrayNotHasKey('widget', $transformed['properties']['firstName']); diff --git a/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php index e33713d..4504702 100644 --- a/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/CommonTransformerTest.php @@ -38,7 +38,6 @@ public function testRequired(): void $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('required', $transformed); $this->assertTrue(is_array($transformed['required'])); $this->assertContains('firstName', $transformed['required']); @@ -56,11 +55,10 @@ public function testDescription(): void $resolver->setTransformer('text', new StringTransformer($this->translator)); $this->translator->method('trans') - ->will($this->returnCallback(fn($description) => $description)); + ->willReturnCallback(fn($description) => $description); $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('description', $transformed['properties']['firstName']); $this->assertSame($description, $transformed['properties']['firstName']['description']); } @@ -79,11 +77,10 @@ public function testDescriptionFromFormHelp(): void $resolver = new Resolver(); $resolver->setTransformer('text', new StringTransformer($this->translator)); $this->translator->method('trans') - ->will($this->returnCallback(fn($description) => $description)); + ->willReturnCallback(fn($description) => $description); $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('description', $transformed['properties']['firstName']); $this->assertSame($description, $transformed['properties']['firstName']['description']); } @@ -106,10 +103,9 @@ public function testDescriptionFromFormHelpOverriddenByLiformDescription(): void $resolver->setTransformer('text', new StringTransformer($this->translator)); $transformer = new CompoundTransformer($this->translator, $resolver); $this->translator->method('trans') - ->will($this->returnCallback(fn($description) => $description)); + ->willReturnCallback(fn($description) => $description); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertArrayHasKey('description', $transformed['properties']['firstName']); $this->assertSame($description, $transformed['properties']['firstName']['description']); } diff --git a/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php b/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php index e2a4da0..0b6d265 100644 --- a/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php +++ b/tests/Limenius/Liform/Tests/Transformer/StringTransformerTest.php @@ -37,7 +37,6 @@ public function testPattern(): void $resolver->setTransformer('text', new StringTransformer($this->translator)); $transformer = new CompoundTransformer($this->translator, $resolver); $transformed = $transformer->transform($form); - $this->assertTrue(is_array($transformed)); $this->assertEquals('.{5,}', $transformed['properties']['firstName']['pattern']); } } From 8086bc6fe8db18e98c9e1488fb77c131918298eb Mon Sep 17 00:00:00 2001 From: Tac Tacelosky Date: Tue, 15 Oct 2024 09:08:39 -0400 Subject: [PATCH 11/11] fix return for getSupportedTypes --- .../Liform/Serializer/Normalizer/FormErrorNormalizer.php | 3 +-- .../Liform/Serializer/Normalizer/InitialValuesNormalizer.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php index 4add0df..b4a5db9 100644 --- a/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php +++ b/src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php @@ -60,7 +60,7 @@ public function supportsNormalization(mixed $data, ?string $format = null, array */ public function getSupportedTypes(?string $format): array { - return [Form::class]; + return [Form::class => true]; } /** @@ -122,5 +122,4 @@ private function getErrorMessage(FormError $error) return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); } - } diff --git a/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php b/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php index cd4bef8..d6dd82d 100644 --- a/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php +++ b/src/Limenius/Liform/Serializer/Normalizer/InitialValuesNormalizer.php @@ -118,6 +118,6 @@ private function normalizeExpandedChoice(FormView $formView): mixed public function getSupportedTypes(?string $format): array { - return [Form::class]; + return [Form::class => true]; } }