FormErrorHelper::getRecursiveReadableErrors()   D
last analyzed

Complexity

Conditions 10
Paths 28

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 22
nc 28
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Victoire\Bundle\FormBundle\Helper;
4
5
use Symfony\Component\Form\Form;
6
7
/**
8
 * A service that give errors from a form as a string.
9
 *
10
 * service: victoire_form.error_helper
11
 */
12
class FormErrorHelper
13
{
14
    protected $translator = null;
15
16
    /**
17
     * The constructor.
18
     *
19
     * @param Translator $translator The translator service
20
     * @param booleand   $debug      Is debug mode enabled ? It will be verbose then.
21
     */
22
    public function __construct($translator, $debug)
23
    {
24
        $this->translator = $translator;
25
        $this->debug = $debug;
0 ignored issues
show
Bug introduced by
The property debug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$translationDomain" missing
Loading history...
introduced by
Doc comment for parameter "$level" missing
Loading history...
29
     * Returns a string representation of all form errors (including children errors).
30
     *
31
     * This method should only be used in ajax calls.
32
     *
33
     * @param Form $form         The form to parse
34
     * @param bool $withChildren Do we parse the embedded forms
35
     *
36
     * @return string A string representation of all errors
37
     */
38
    public function getRecursiveReadableErrors($form, $withChildren = true, $translationDomain = null, $level = 0)
39
    {
40
        $errors = '';
41
        $translationDomain = $translationDomain ? $translationDomain : $form->getConfig()->getOption('translation_domain');
42
43
        //the errors of the fields
44
        foreach ($form->getErrors() as $error) {
45
            //the view contains the label identifier
46
            $view = $form->createView();
47
            $labelId = $view->vars['label'];
48
49
            //get the translated label
50
            if ($labelId !== null) {
51
                $label = $this->translator->trans(/* @Ignore */$labelId, [], $translationDomain).' : ';
52
            } else {
53
                $label = '';
54
            }
55
56
            //in case of dev mode, we display the item that is a problem
57
            //getCause cames in Symfony 2.5 version, this is just a fallback to avoid BC with previous versions
58
            if ($this->debug && method_exists($error, 'getCause')) {
59
                $cause = $error->getCause();
60
                if ($cause !== null) {
61
                    $causePropertyPath = $cause->getPropertyPath();
62
                    $errors .= ' '.$causePropertyPath;
63
                }
64
            }
65
66
            //add the error
67
            $errors .= $label.$this->translator->trans(/* @Ignore */$error->getMessage(), [], $translationDomain)."\n";
68
        }
69
70
        //do we parse the children
71
        if ($withChildren) {
72
            //we parse the children
73
            foreach ($form->getIterator() as $child) {
74
                $level++;
75
                if ($err = $this->getRecursiveReadableErrors($child, $withChildren, $translationDomain, $level)) {
76
                    $errors .= $err;
77
                }
78
            }
79
        }
80
81
        return $errors;
82
    }
83
}
84