Skip to content

Commit

Permalink
Improve static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
zmitic committed Aug 5, 2022
1 parent d354b8d commit d1265fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/Accessor/CollectionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Strictify\FormMapper\Accessor;

use Closure;
use ReflectionFunction;
use Symfony\Component\Form\FormInterface;
use function is_array;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -136,6 +133,5 @@ private function extractRemoveEntryFromCollection(FormInterface $form): ?\Closur
}

return null;

}
}
8 changes: 5 additions & 3 deletions src/Extension/FactoryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions src/Extension/MapperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' =>
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit d1265fa

Please sign in to comment.