From d1265fa57905148d4300ebfdee24b5392484361f Mon Sep 17 00:00:00 2001 From: zeljko Date: Fri, 5 Aug 2022 14:00:21 +0200 Subject: [PATCH] Improve static analysis --- src/Accessor/CollectionMapper.php | 14 +++++--------- src/Extension/FactoryExtension.php | 8 +++++--- src/Extension/MapperExtension.php | 11 +++++++++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Accessor/CollectionMapper.php b/src/Accessor/CollectionMapper.php index 83e372f..51b1328 100644 --- a/src/Accessor/CollectionMapper.php +++ b/src/Accessor/CollectionMapper.php @@ -4,6 +4,7 @@ namespace Strictify\FormMapper\Accessor; +use Closure; use ReflectionFunction; use Symfony\Component\Form\FormInterface; use function is_array; @@ -41,7 +42,7 @@ public function update(array $options, &$data, FormInterface $form): void $removeEntry = $options['entry_options']['remove_entry'] ?? null; $removalCallback = $this->extractRemoveEntryFromCollection($form); - foreach ($toRemove as $key => $item) { + foreach ($toRemove as $item) { if ($removeEntry) { $removeEntry($item); } @@ -52,11 +53,7 @@ public function update(array $options, &$data, FormInterface $form): void } } - /** - * @param mixed $data - * @param mixed $submittedData - */ - private function submit($data, $submittedData, ReflectionFunction $reflection): bool + private function submit(mixed $data, mixed $submittedData, ReflectionFunction $reflection): bool { $params = $reflection->getParameters(); @@ -116,7 +113,7 @@ private function getExtraValues(callable $compare, array $originalValues, array return $extraValues; } - private function search(callable $compare, $key, $value, $originalValues) + private function search(callable $compare, int|string $key, object|array $value, object|array $originalValues): false|int|string { foreach ($originalValues as $originalKey => $originalValue) { if ($value && $originalValue && $compare($value, $originalValue, $key, $originalKey) === true) { @@ -127,7 +124,7 @@ private function search(callable $compare, $key, $value, $originalValues) return false; } - private function extractRemoveEntryFromCollection(FormInterface $form): ?\Closure + private function extractRemoveEntryFromCollection(FormInterface $form): ?Closure { foreach ($form as $child) { $options = $child->getConfig()->getOptions(); @@ -136,6 +133,5 @@ private function extractRemoveEntryFromCollection(FormInterface $form): ?\Closur } return null; - } } diff --git a/src/Extension/FactoryExtension.php b/src/Extension/FactoryExtension.php index da8081a..a3050c2 100644 --- a/src/Extension/FactoryExtension.php +++ b/src/Extension/FactoryExtension.php @@ -39,14 +39,16 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->addAllowedTypes('factory', ['null', Closure::class]); $resolver->addAllowedTypes('show_factory_error', ['bool']); - $resolver->setNormalizer('empty_data', function (Options $options, $default) { - /** @var Closure|null $factory */ + $resolver->setNormalizer('empty_data', function (Options $options, mixed $default) { $factory = $options['factory']; - return $factory ? $this->createEmptyDataWrapper($factory) : $default; + return $factory instanceof Closure ? $this->createEmptyDataWrapper($factory) : $default; }); } + /** + * @return Closure(FormInterface) + */ private function createEmptyDataWrapper(Closure $factory): Closure { return function (FormInterface $form) use ($factory) { diff --git a/src/Extension/MapperExtension.php b/src/Extension/MapperExtension.php index 5714540..10384a9 100644 --- a/src/Extension/MapperExtension.php +++ b/src/Extension/MapperExtension.php @@ -6,6 +6,8 @@ use Closure; use ReflectionFunction; +use ReflectionUnionType; +use ReflectionNamedType; use Symfony\Component\Validator\Constraint; use Strictify\FormMapper\Service\Comparator; use Symfony\Component\OptionsResolver\Options; @@ -44,7 +46,7 @@ public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'get_value' => null, - 'update_value' => fn ($data) => throw new MissingOptionsException('You have to create "update_value" callback.'), + 'update_value' => fn(mixed $data) => throw new MissingOptionsException('You have to create "update_value" callback.'), 'add_value' => fn() => null, 'remove_value' => fn() => null, 'compare' => @@ -84,6 +86,7 @@ private function normalizeConstraints(Options $options, array $constraints): arr $firstParam = $params[0]; $type = $firstParam->getType(); + // first param is not typehinted, do not add extra constraints if (!$type) { return $constraints; @@ -100,7 +103,11 @@ private function normalizeConstraints(Options $options, array $constraints): arr } if (!in_array(Type::class, $constraintClasses, true)) { - $extraConstraints[] = new Type(['type' => $type->getName()]); + $typeName = match (true) { + $type instanceof ReflectionUnionType => array_map(static fn(ReflectionNamedType $reflectionNamedType) => $reflectionNamedType->getName(), $type->getTypes()), + $type instanceof ReflectionNamedType => $type->getName(), + }; + $extraConstraints[] = new Type(['type' => $typeName]); } // these extra constraints must be executed first