Completed
Branch dev-master (5a9550)
by Derek Stephen
02:00
created

HorizontalFormRenderer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 25
loc 85
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A render() 11 11 1
A setFormAttributes() 14 14 2
A getMethod() 0 4 2
A getId() 0 4 2
A processFields() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 29/11/2016
5
 * Time: 19:44
6
 */
7
8
namespace Del\Form\Renderer;
9
10
use Del\Form\Collection\FieldCollection;
11
use Del\Form\AbstractForm;
12
use Del\Form\FormInterface;
13
use DOMDocument;
14
15
class HorizontalFormRenderer implements FormRendererInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: renderFieldBlock, renderFieldLabel
Loading history...
16
{
17
    /** @var DOMDocument $dom */
18
    private $dom;
19
20
    /** @var \DomElement $form */
21
    private $form;
22
23
    /** @var bool $displayErrors */
24
    private $displayErrors;
25
26
    public function __construct($name)
27
    {
28
        $this->dom = new DOMDocument();
29
        $form = $this->dom->createElement('form');
30
        $form->setAttribute('name', $name);
31
        $this->form = $form;
32
    }
33
34
    /**
35
     * @param FormInterface $form
36
     * @param bool $displayErrors
37
     * @return string
38
     */
39 View Code Duplication
    public function render(FormInterface $form, $displayErrors = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $this->displayErrors = $displayErrors;
42
        $this->setFormAttributes($form);
43
44
        $fields = $form->getFields();
45
        $this->processFields($fields);
46
47
        $this->dom->appendChild($this->form);
48
        return $this->dom->saveHTML();
49
    }
50
51
    /**
52
     * @param FormInterface $form
53
     */
54 View Code Duplication
    private function setFormAttributes(FormInterface $form)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $attributes = $form->getAttributes();
57
        foreach ($attributes as $key => $value) {
58
            $this->form->setAttribute($key, $value);
59
        }
60
61
        // set Id as name or method as post if not set
62
        $method = $this->getMethod($form);
63
        $id = $this->getId($form);
64
65
        $this->form->setAttribute('id', $id);
66
        $this->form->setAttribute('method', $method);
67
    }
68
69
    /**
70
     * @param FormInterface $form
71
     * @return string
72
     */
73
    private function getMethod(FormInterface $form)
74
    {
75
        return $form->getMethod() ?: AbstractForm::METHOD_POST;
76
    }
77
78
    /**
79
     * @param FormInterface $form
80
     * @return string
81
     */
82
    private function getId(FormInterface $form)
83
    {
84
        return $form->getId() ?: $this->form->getAttribute('name');
85
    }
86
87
    private function processFields(FieldCollection $fields)
88
    {
89
        $fields->rewind();
90
        while ($fields->valid()) {
91
            $current = $fields->current();
92
            $child = $current->getRenderer()->render($this->dom, $current, $this->displayErrors);
0 ignored issues
show
Unused Code introduced by
The call to FieldRendererInterface::render() has too many arguments starting with $this->displayErrors.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
            $this->form->appendChild($child);
94
            $fields->next();
95
        }
96
        $fields->rewind();
97
    }
98
99
}