Completed
Push — master ( 6744a4...ab6d26 )
by Guilh
7s
created

FormErrorNormalizer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 75
ccs 37
cts 38
cp 0.9737
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalize() 0 12 3
A supportsNormalization() 0 8 4
B convertFormToArray() 0 25 6
A getErrorMessage() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Serializer\Normalizer;
13
14
use FOS\RestBundle\Util\FormWrapper;
15
use Symfony\Component\Form\FormError;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18
use Symfony\Component\Translation\TranslatorInterface;
19
20
/**
21
 * Normalizes invalid Form instances.
22
 *
23
 * @author Ener-Getick <[email protected]>
24
 */
25
class FormErrorNormalizer implements NormalizerInterface
26
{
27
    private $translator;
28
29 13
    public function __construct(TranslatorInterface $translator)
30
    {
31 13
        $this->translator = $translator;
32 13
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function normalize($object, $format = null, array $context = [])
38
    {
39 2
        if ($object instanceof FormWrapper) {
40 1
            $object = $object->form;
41 1
        }
42
43
        return [
44 2
            'code' => isset($context['status_code']) ? $context['status_code'] : null,
45 2
            'message' => 'Validation Failed',
46 2
            'errors' => $this->convertFormToArray($object),
47 2
        ];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 7
    public function supportsNormalization($data, $format = null)
54
    {
55 7
        if ($data instanceof FormWrapper) {
56 1
            $data = $data->form;
57 1
        }
58
59 7
        return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
60
    }
61
62
    /**
63
     * This code has been taken from JMSSerializer.
64
     */
65 2
    private function convertFormToArray(FormInterface $data)
66
    {
67 2
        $form = $errors = [];
68
69 2
        foreach ($data->getErrors() as $error) {
70 2
            $errors[] = $this->getErrorMessage($error);
0 ignored issues
show
Documentation introduced by
$error is of type object<Symfony\Component...ormErrorIterator>|false, but the function expects a object<Symfony\Component\Form\FormError>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71 2
        }
72
73 2
        if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
74 2
            $form['errors'] = $errors;
75 2
        }
76
77 2
        $children = [];
78 2
        foreach ($data->all() as $child) {
79 2
            if ($child instanceof FormInterface) {
80 2
                $children[$child->getName()] = $this->convertFormToArray($child);
81 2
            }
82 2
        }
83
84 2
        if ($children) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $children of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85 2
            $form['children'] = $children;
86 2
        }
87
88 2
        return $form;
89
    }
90
91 2
    private function getErrorMessage(FormError $error)
92
    {
93 2
        if (null !== $error->getMessagePluralization()) {
94
            return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators');
95
        }
96
97 2
        return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators');
98
    }
99
}
100