FormErrorHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 72
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D getRecursiveReadableErrors() 0 45 10
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