HasErrorsTrait::addError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
/**
6
 * Trait HasErorsTrait
7
 * @package Nip\Form\Traits
8
 */
9
trait HasErrorsTrait
10
{
11
    protected $errorsByField = null;
12
13
    /**
14
     * @return array
15
     */
16
    public function getErrors()
17
    {
18
        $errors = array_merge((array)$this->getMessagesType('error'), $this->getElementsErrors());
0 ignored issues
show
Bug introduced by
It seems like getMessagesType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

18
        $errors = array_merge((array)$this->/** @scrutinizer ignore-call */ getMessagesType('error'), $this->getElementsErrors());
Loading history...
19
20
        return $errors;
21
    }
22
23
    /**
24
     * @return array
25
     */
26
    public function getElementsErrors()
27
    {
28
        $elementsErrors = $this->getErrorsByField();
29
        $errors = [];
30
        foreach ($elementsErrors as $name => $elementErrors) {
31
            $errors = array_merge($errors, $elementErrors);
32
        }
33
34
        return $errors;
35
    }
36
37
    /**
38
     * @param $message
39
     * @return $this
40
     */
41
    public function addError($message)
42
    {
43
        $this->addMessage($message, 'error');
0 ignored issues
show
Bug introduced by
It seems like addMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

43
        $this->/** @scrutinizer ignore-call */ 
44
               addMessage($message, 'error');
Loading history...
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function getErrorsByField(): array
52
    {
53
        if ($this->errorsByField === null) {
54
            $this->generateElementsErrors();
55
        }
56
57
        return $this->errorsByField;
58
    }
59
60
    protected function generateElementsErrors()
61
    {
62
        $elements = $this->getElements();
0 ignored issues
show
Bug introduced by
The method getElements() does not exist on Nip\Form\Traits\HasErrorsTrait. Did you maybe mean getElementsErrors()? ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-call */ 
63
        $elements = $this->getElements();

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...
63
        $this->errorsByField = [];
64
        if (is_array($elements)) {
65
            foreach ($elements as $name => $element) {
66
                $elementErrors = $element->getErrors();
67
                if (count($elementErrors)) {
68
                    $this->errorsByField[$name] = $elementErrors;
69
                }
70
            }
71
        }
72
    }
73
}
74