DefaultErrorRender   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 40
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addCustomErrorMessage() 0 6 1
A appendMessage() 0 7 1
A addErrorMessages() 0 8 2
A render() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Form\Renderer\Error;
6
7
8
use Del\Form\Field\FieldInterface;
9
use DOMElement;
10
11
class DefaultErrorRender extends AbstractErrorRender
12
{
13 3
    public function render(FieldInterface $field): DOMElement
14
    {
15 3
        $helpBlock = $this->getDom()->createElement('span');
16 3
        $helpBlock->setAttribute('class', 'text-danger');
17
18 3
        if ($this->shouldRender($field)) {
19 3
            $helpBlock = $field->hasCustomErrorMessage()
20 1
                ? $this->addCustomErrorMessage($helpBlock, $field)
21 2
                : $this->addErrorMessages($helpBlock, $field);
22
        }
23 3
        return $helpBlock;
24
    }
25
26 1
    private function addCustomErrorMessage(DOMElement $helpBlock, FieldInterface $field): DOMElement
27
    {
28 1
        $message = $field->getCustomErrorMessage();
29 1
        $text = $this->createText($message);
30 1
        $helpBlock->appendChild($text);
31 1
        return $helpBlock;
32
    }
33
34 2
    private function addErrorMessages(DOMElement $helpBlock, FieldInterface $field): DOMElement
35
    {
36 2
        $messages = $field->getMessages();
37
38 2
        foreach ($messages as $message) {
39 2
            $helpBlock = $this->appendMessage($helpBlock, $message);
40
        }
41 2
        return $helpBlock;
42
    }
43
44 2
    private function appendMessage(DOMElement $helpBlock, $message): DOMElement
45
    {
46 2
        $text = $this->createText($message);
47 2
        $br = $this->createLineBreak();
48 2
        $helpBlock->appendChild($text);
49 2
        $helpBlock->appendChild($br);
50 2
        return $helpBlock;
51
    }
52
}
53