FormErrorNormalizer::convertFormToArray()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 24
nop 1
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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