diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ed199e9..92169b8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -12,12 +12,6 @@ parameters: count: 1 path: src/Qossmic/DataTransformer/ValueObjectTransformer.php - - - message: '#^Method Qossmic\\RichModelForms\\DataTransformer\\ValueObjectTransformer\:\:getPropertyValue\(\) should return bool\|int\|string\|null but returns mixed\.$#' - identifier: return.type - count: 1 - path: src/Qossmic/DataTransformer/ValueObjectTransformer.php - - message: '#^Parameter \#1 \$strategy of method Qossmic\\RichModelForms\\ExceptionHandling\\ExceptionHandlerRegistry\:\:has\(\) expects string, mixed given\.$#' identifier: argument.type diff --git a/src/Qossmic/DataTransformer/ValueObjectTransformer.php b/src/Qossmic/DataTransformer/ValueObjectTransformer.php index 7f0c6f0..ba8b2d8 100644 --- a/src/Qossmic/DataTransformer/ValueObjectTransformer.php +++ b/src/Qossmic/DataTransformer/ValueObjectTransformer.php @@ -43,10 +43,8 @@ public function __construct(ExceptionHandlerRegistry $exceptionHandlerRegistry, /** * @param object|null $value - * - * @return array|bool|int|string|null */ - public function transform(mixed $value): array|bool|int|string|null + public function transform(mixed $value): mixed { if (null === $value) { return null; @@ -90,10 +88,9 @@ public function reverseTransform(mixed $value): ?object } } - private function getPropertyValue(FormBuilderInterface $form, object $object): bool|int|string|null + private function getPropertyValue(FormBuilderInterface $form, object $object): mixed { if (null !== $form->getPropertyPath()) { - /* @phpstan-ignore-next-line */ return $this->propertyAccessor->getValue($object, $form->getPropertyPath()); } diff --git a/tests/Fixtures/Form/NonScalarType.php b/tests/Fixtures/Form/NonScalarType.php new file mode 100644 index 0000000..0525e8d --- /dev/null +++ b/tests/Fixtures/Form/NonScalarType.php @@ -0,0 +1,40 @@ + + * (c) Christopher Hertel + * (c) QOSSMIC GmbH + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace Qossmic\RichModelForms\Tests\Fixtures\Form; + +use Qossmic\RichModelForms\Tests\Fixtures\Model\ForNonScalarType; +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\DateType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class NonScalarType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder->add('dateFrom', DateType::class, [ + 'widget' => 'single_text', + 'required' => true, + 'input' => 'datetime_immutable', + ]); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefault('factory', [ForNonScalarType::class, 'create']); + $resolver->setDefault('immutable', true); + } +} diff --git a/tests/Fixtures/Model/ForNonScalarType.php b/tests/Fixtures/Model/ForNonScalarType.php new file mode 100644 index 0000000..e866b69 --- /dev/null +++ b/tests/Fixtures/Model/ForNonScalarType.php @@ -0,0 +1,34 @@ + + * (c) Christopher Hertel + * (c) QOSSMIC GmbH + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types = 1); + +namespace Qossmic\RichModelForms\Tests\Fixtures\Model; + +class ForNonScalarType +{ + private function __construct( + private \DateTimeImmutable $dateFrom, + ) { + } + + public static function create(\DateTimeImmutable $dateFrom): self + { + return new self($dateFrom); + } + + public function getDateFrom(): \DateTimeImmutable + { + return $this->dateFrom; + } +} diff --git a/tests/Integration/ValueObjectsTest.php b/tests/Integration/ValueObjectsTest.php index dbdfc0b..5701a0d 100644 --- a/tests/Integration/ValueObjectsTest.php +++ b/tests/Integration/ValueObjectsTest.php @@ -20,7 +20,9 @@ use Qossmic\RichModelForms\Extension\RichModelFormsTypeExtension; use Qossmic\RichModelForms\Tests\ExceptionHandlerRegistryTrait; use Qossmic\RichModelForms\Tests\Fixtures\Form\GrossPriceType; +use Qossmic\RichModelForms\Tests\Fixtures\Form\NonScalarType; use Qossmic\RichModelForms\Tests\Fixtures\Form\PriceType; +use Qossmic\RichModelForms\Tests\Fixtures\Model\ForNonScalarType; use Qossmic\RichModelForms\Tests\Fixtures\Model\GrossPrice; use Qossmic\RichModelForms\Tests\Fixtures\Model\Price; use Symfony\Component\Form\Exception\TransformationFailedException; @@ -34,6 +36,19 @@ class ValueObjectsTest extends TestCase { use ExceptionHandlerRegistryTrait; + public function testValueObjectTransformerSupportsNonScalarValue(): void + { + $form = $this->createNamedForm('amount', NonScalarType::class); + $form->submit([ + 'dateFrom' => '2025-01-01', + ]); + + $data = $form->getData(); + + $this->assertInstanceOf(ForNonScalarType::class, $data); + $this->assertEquals(new \DateTimeImmutable('2025-01-01'), $data->getDateFrom()); + } + public function testNonCompoundRootFormDoesNotRequirePropertyPathToBeSetIfPropertyPathCanBeDerivedFromFormName(): void { $form = $this->createNamedForm('amount', PriceType::class, new Price(500), [