Skip to content

Commit

Permalink
Merge branch 'tac' of github.com:survos/Liform into tac
Browse files Browse the repository at this point in the history
  • Loading branch information
tacman committed Oct 15, 2024
2 parents c100805 + cd2648f commit 24c359d
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 104 deletions.
17 changes: 17 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
// uncomment to reach your current PHP version
->withPhpSets(php82: true)
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
]);
12 changes: 5 additions & 7 deletions src/Limenius/Liform/FormUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -38,25 +38,24 @@ 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;
}

$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;
Expand All @@ -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));
}
Expand Down
8 changes: 3 additions & 5 deletions src/Limenius/Liform/Guesser/ValidatorGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,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));
}

/**
Expand All @@ -43,14 +41,14 @@ public function guessMinLength(string $class, string $property)
*/
public function guessMinLengthForConstraint(Constraint $constraint)
{
switch (get_class($constraint)) {
switch ($constraint::class) {
case Length::class:
if (is_numeric($constraint->min)) {
return new ValueGuess($constraint->min, Guess::HIGH_CONFIDENCE);
}
break;
case Type::class:
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
if (in_array($constraint->type, ['double', 'float', 'numeric', 'real'])) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
break;
Expand Down
8 changes: 1 addition & 7 deletions src/Limenius/Liform/Liform.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
*/
class Liform implements LiformInterface
{
/**
* @var ResolverInterface
*/
private $resolver;

/**
* @var ExtensionInterface[]
*/
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Limenius/Liform/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function getValues(FormInterface $form, FormView $formView): mixed
return $this->normalizeExpandedChoice($formView);
}
// 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
Expand All @@ -78,13 +78,7 @@ private function getValues(FormInterface $form, FormView $formView): mixed
return (array) $data;
}

// 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'];
}

/**
Expand All @@ -95,7 +89,7 @@ private function getValues(FormInterface $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'];
Expand Down
11 changes: 2 additions & 9 deletions src/Limenius/Liform/Transformer/AbstractTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@
*/
abstract class AbstractTransformer implements TransformerInterface
{
protected TranslatorInterface $translator;

protected ?FormTypeGuesserInterface $validatorGuesser = null;

/**
* @param TranslatorInterface $translator
* @param FormTypeGuesserInterface|null $validatorGuesser
*/
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
Expand Down Expand Up @@ -147,8 +141,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']) {
Expand Down
2 changes: 1 addition & 1 deletion src/Limenius/Liform/Transformer/ArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/Limenius/Liform/Transformer/CompoundTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,9 +47,7 @@ public function transform(FormInterface $form, array $extensions = [], $widget =
$required = [];

$formItems = $form->all();
uasort($formItems, static function ($a, $b): int {
return $a->getConfig()->getOption('priority') <=> $b->getConfig()->getOption('priority');
});
uasort($formItems, static fn($a, $b): int => $a->getConfig()->getOption('priority') <=> $b->getConfig()->getOption('priority'));

foreach ($formItems as $name => $field) {
$transformerData = $this->resolver->resolve($field);
Expand Down
6 changes: 3 additions & 3 deletions tests/Limenius/Liform/Tests/ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -59,7 +59,7 @@ public function testChoiceExpandedMultiple()

}

public function testChoiceExpanded()
public function testChoiceExpanded(): void
{
$form = $this->factory->create(FormType::class, ['firstName' => 'A'])
->add(
Expand Down
24 changes: 10 additions & 14 deletions tests/Limenius/Liform/Tests/Transformer/ChoiceTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
class ChoiceTransformerTest extends LiformTestCase
{
public function testChoice()
public function testChoice(): void
{
$form = $this->factory->create(FormType::class)
->add(
Expand All @@ -39,13 +39,11 @@ 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));
$transformer = new CompoundTransformer($this->translator, null, $resolver);
$transformer = new CompoundTransformer($this->translator, $resolver);
$transformed = $transformer->transform($form);
$this->assertTrue(is_array($transformed));
$this->assertArrayHasKey('enum_titles', $transformed['properties']['firstName']);
Expand All @@ -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(
Expand All @@ -71,13 +69,11 @@ 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));
$transformer = new CompoundTransformer($this->translator, null, $resolver);
$transformer = new CompoundTransformer($this->translator, $resolver);
$transformed = $transformer->transform($form);
$this->assertTrue(is_array($transformed));
$this->assertArrayHasKey('enum_titles', $transformed['properties']['firstName']);
Expand All @@ -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(
Expand All @@ -104,15 +100,15 @@ public function testChoiceMultiple()

$resolver = new Resolver();
$resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null));
$transformer = new CompoundTransformer($this->translator, null, $resolver);
$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']);
}

public function testChoiceMultipleExpanded()
public function testChoiceMultipleExpanded(): void
{
$form = $this->factory->create(FormType::class)
->add(
Expand All @@ -127,7 +123,7 @@ public function testChoiceMultipleExpanded()

$resolver = new Resolver();
$resolver->setTransformer('choice', new Transformer\ChoiceTransformer($this->translator, null));
$transformer = new CompoundTransformer($this->translator, null, $resolver);
$transformer = new CompoundTransformer($this->translator, $resolver);
$transformed = $transformer->transform($form);
$this->assertTrue(is_array($transformed));
$this->assertArrayHasKey('items', $transformed['properties']['firstName']);
Expand Down
Loading

0 comments on commit 24c359d

Please sign in to comment.