Skip to content

Commit

Permalink
Merge pull request #75 from survos/tac
Browse files Browse the repository at this point in the history
8.2 support
  • Loading branch information
tacman authored Nov 29, 2024
2 parents c52533c + 8086bc6 commit c364314
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 150 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ 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"
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
"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",
"phpunit/phpunit": "^11.4",
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.8"
"phpstan/phpstan": "^1.8",
"rector/rector": "^1.2"
},
"scripts": {
"test": "phpunit",
Expand Down
32 changes: 15 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Liform Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">
<testsuites>
<testsuite name="Liform Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</source>
</phpunit>
21 changes: 21 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\PHPUnit\Set\PHPUnitSetList;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
// uncomment to reach your current PHP version
->withPhpSets(php83: true)
->withSets([
PHPUnitSetList::PHPUNIT_110
])
->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
27 changes: 12 additions & 15 deletions src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,20 @@
*/
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;
}

/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
return [
'code' => isset($context['status_code']) ? $context['status_code'] : null,
'code' => $context['status_code'] ?? null,
'message' => 'Validation Failed',
'errors' => $this->convertFormToArray($object),
];
Expand All @@ -56,11 +50,19 @@ public function normalize($object, $format = null, array $context = []): float|a
* @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 => true];
}

/**
* This code has been taken from JMSSerializer.
*
Expand Down Expand Up @@ -120,9 +122,4 @@ private function getErrorMessage(FormError $error)

return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators');
}

public function getSupportedTypes(?string $format): array
{
return [Form::class];
}
}
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 Expand Up @@ -124,6 +118,6 @@ private function normalizeExpandedChoice(FormView $formView): mixed

public function getSupportedTypes(?string $format): array
{
return [Form::class];
return [Form::class => true];
}
}
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
Loading

0 comments on commit c364314

Please sign in to comment.