Skip to content

Commit

Permalink
Fix phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Dec 2, 2021
1 parent 2c6322a commit 77af965
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
7 changes: 7 additions & 0 deletions phpstan-baseline.neon
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: "#^Property JMS\\\\Serializer\\\\Metadata\\\\PropertyMetadata\\:\\:\\$groups \\(array\\<string\\>\\) in isset\\(\\) is not nullable\\.$#"
count: 1
path: src/Type/BaseDoctrineORMSerializationType.php

3 changes: 3 additions & 0 deletions phpstan.neon.dist
@@ -1,3 +1,6 @@
includes:
- phpstan-baseline.neon

parameters:
level: 8
paths:
Expand Down
3 changes: 3 additions & 0 deletions src/DataTransformer/BooleanTypeToBooleanTransformer.php
Expand Up @@ -16,6 +16,9 @@
use Sonata\Form\Type\BooleanType;
use Symfony\Component\Form\DataTransformerInterface;

/**
* @phpstan-implements DataTransformerInterface<boolean, int>
*/
final class BooleanTypeToBooleanTransformer implements DataTransformerInterface
{
/**
Expand Down
11 changes: 7 additions & 4 deletions src/Type/BaseDoctrineORMSerializationType.php
Expand Up @@ -134,7 +134,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

if (isset($doctrineMetadata->fieldMappings[$name])) {
$fieldMetadata = $doctrineMetadata->fieldMappings[$name];
$type = $fieldMetadata['type'] ?? null;
$type = $fieldMetadata['type'];
$nullable = $fieldMetadata['nullable'] ?? false;
} elseif (isset($doctrineMetadata->associationMappings[$name])) {
$associationMetadata = $doctrineMetadata->associationMappings[$name];
Expand All @@ -145,25 +145,28 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$nullable = $associationMetadata['inverseJoinColumns']['nullable'];
}
}

$required = true !== $nullable;

switch ($type) {
case 'datetime':
$builder->add(
$name,
DateTimeType::class,
['required' => !$nullable, 'widget' => 'single_text']
['required' => $required, 'widget' => 'single_text']
);

break;

case 'boolean':
$childBuilder = $builder->create($name, null, ['required' => !$nullable]);
$childBuilder = $builder->create($name, null, ['required' => $required]);
$childBuilder->addEventSubscriber(new FixCheckboxDataListener());
$builder->add($childBuilder);

break;

default:
$builder->add($name, null, ['required' => !$nullable]);
$builder->add($name, null, ['required' => $required]);

break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/BasePickerType.php
Expand Up @@ -213,7 +213,7 @@ private function formatObject(\DateTimeInterface $dateTime, string $format): str
$formatter->setPattern($format);

$formatted = $formatter->format($dateTime);
if (false === $formatted) {
if (!\is_string($formatted)) {
throw new \RuntimeException(sprintf('The format "%s" is invalid.', $format));
}

Expand Down
20 changes: 9 additions & 11 deletions src/Validator/ErrorElement.php
Expand Up @@ -91,11 +91,6 @@ final class ErrorElement
*/
private $group;

/**
* @var ConstraintValidatorFactoryInterface
*/
private $constraintValidatorFactory;

/**
* @var string[]
*/
Expand Down Expand Up @@ -127,7 +122,11 @@ final class ErrorElement
private $errors = [];

/**
* NEXT_MAJOR: Remove `$constraintValidatorFactory` from the signature.
*
* @param mixed $subject
*
* @phpstan-ignore-next-line
*/
public function __construct(
$subject,
Expand All @@ -138,7 +137,6 @@ public function __construct(
$this->subject = $subject;
$this->context = $context;
$this->group = $group;
$this->constraintValidatorFactory = $constraintValidatorFactory;
$this->basePropertyPath = $this->context->getPropertyPath();
}

Expand Down Expand Up @@ -224,11 +222,11 @@ public function addViolation($message, array $parameters = [], $value = null, st
$subPath = (string) $this->getCurrentPropertyPath();

$this->context->buildViolation($message)
->atPath($subPath)
->setParameters($parameters)
->setTranslationDomain($translationDomain)
->setInvalidValue($value)
->addViolation();
->atPath($subPath)
->setParameters($parameters)
->setTranslationDomain($translationDomain)
->setInvalidValue($value)
->addViolation();

$this->errors[] = [$message, $parameters, $value];

Expand Down
10 changes: 3 additions & 7 deletions tests/EventListener/FixCheckboxDataListenerTest.php
Expand Up @@ -17,7 +17,6 @@
use Sonata\Form\EventListener\FixCheckboxDataListener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Forms;
Expand All @@ -34,7 +33,7 @@ public function testFixCheckbox(
$data,
$expected,
?EventSubscriberInterface $subscriber,
?DataTransformerInterface $transformer
BooleanToStringTransformer $transformer
): void {
$dispatcher = new EventDispatcher();

Expand All @@ -47,10 +46,7 @@ public function testFixCheckbox(
->getFormFactory();

$formBuilder = new FormBuilder('checkbox', 'stdClass', $dispatcher, $formFactory);

if (null !== $transformer) {
$formBuilder->addViewTransformer($transformer);
}
$formBuilder->addViewTransformer($transformer);

$form = $formBuilder->getForm();
$form->submit($data);
Expand All @@ -59,7 +55,7 @@ public function testFixCheckbox(
}

/**
* @return iterable<array{mixed, mixed, EventSubscriberInterface|null, DataTransformerInterface|null}>
* @return iterable<array{mixed, mixed, EventSubscriberInterface|null, BooleanToStringTransformer}>
*/
public function valuesProvider(): iterable
{
Expand Down

0 comments on commit 77af965

Please sign in to comment.