FormErrorNormalizer::normalize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 2
b 0
f 1
nc 2
nop 3
dl 0
loc 17
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Serializer\Normalizer;
6
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
10
/**
11
 * @see \FOS\RestBundle\Serializer\Normalizer\FormErrorNormalizer
12
 */
13
final class FormErrorNormalizer implements NormalizerInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 6
    public function normalize($object, string $format = null, array $context = []): array
19
    {
20
        $data = [
21 6
            'code' => $context['status_code'] ?? null,
22
            'message' => 'Validation Failed',
23 6
            'errors' => $this->convertFormToArray($object),
24
        ];
25
26 6
        if (!\is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
27
            throw new \RuntimeException('Normalized form data should be of type array.');
28
        }
29
30
        /** @var array $data */
31 6
        $data = $data['errors']['children'];
32 6
        $data = array_filter($data, fn (array $child) => isset($child['errors']) && \count($child['errors']) > 0);
33
34 6
        return array_map(fn (array $child) => $child['errors'] ?? [], $data);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 34
    public function supportsNormalization($data, string $format = null): bool
41
    {
42 34
        return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
43
    }
44
45 6
    private function convertFormToArray(FormInterface $data): array
46
    {
47 6
        $errors = [];
48 6
        $form = [];
49
50 6
        foreach ($data->getErrors() as $error) {
51 6
            $errors[] = $error->getMessage();
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            /** @scrutinizer ignore-call */ 
52
            $errors[] = $error->getMessage();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        }
53
54 6
        if ($errors !== []) {
55 6
            $form['errors'] = $errors;
56
        }
57
58 6
        $children = [];
59 6
        foreach ($data->all() as $child) {
60 6
            if ($child instanceof FormInterface) {
61 6
                $children[$child->getName()] = $this->convertFormToArray($child);
62
            }
63
        }
64
65 6
        if ($children !== []) {
66 6
            $form['children'] = $children;
67
        }
68
69 6
        return $form;
70
    }
71
}
72