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

AbstractFormRenderer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 122
Duplicated Lines 20.49 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A render() 11 11 1
A setFormAttributes() 14 14 2
A getMethod() 0 4 2
A getId() 0 4 2
A processFields() 0 15 3
A renderError() 0 9 3

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: 07/12/2016
5
 * Time: 01:54
6
 */
7
8
namespace Del\Form\Renderer;
9
10
use Del\Form\Collection\FieldCollection;
11
use Del\Form\AbstractForm;
12
use Del\Form\Field\FieldInterface;
13
use Del\Form\FormInterface;
14
use Del\Form\Renderer\Error\DefaultErrorRender;
15
use Del\Form\Renderer\Error\ErrorRendererInterface;
16
use DOMDocument;
17
use DomElement;
18
19
abstract class AbstractFormRenderer implements FormRendererInterface
20
{
21
    /** @var DOMDocument $dom */
22
    protected $dom;
23
24
    /** @var DomElement $form */
25
    protected $form;
26
27
    /** @var bool $displayErrors */
28
    protected $displayErrors;
29
30
    /** @var ErrorRendererInterface $errorRenderer */
31
    protected $errorRenderer;
32
33
    /** @var DomElement $label The label element*/
34
    protected $label;
35
36
    /** @var DomElement $element the field element */
37
    protected $element;
38
39
    /** @var DomElement $errors The error block html*/
40
    protected $errors;
41
42
    /** @var DomElement $block The containing html block */
43
    protected $block;
44
45
    /** @var FieldInterface $field The current field being processed */
46
    protected $field;
47
48 19
    public function __construct($name)
49
    {
50 19
        $this->dom = new DOMDocument();
51 19
        $this->errorRenderer = new DefaultErrorRender($this->dom);
52 19
        $form = $this->dom->createElement('form');
53 19
        $form->setAttribute('name', $name);
54 19
        $this->form = $form;
55 19
    }
56
57
    /**
58
     * @param FormInterface $form
59
     * @param bool $displayErrors
60
     * @return string
61
     */
62 6 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...
63
    {
64 6
        $this->displayErrors = $displayErrors;
65 6
        $this->setFormAttributes($form);
66
67 6
        $fields = $form->getFields();
68 6
        $this->processFields($fields);
69
70 6
        $this->dom->appendChild($this->form);
71 6
        return $this->dom->saveHTML();
72
    }
73
74
    /**
75
     * @param FormInterface $form
76
     */
77 6 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...
78
    {
79 6
        $attributes = $form->getAttributes();
80 6
        foreach ($attributes as $key => $value) {
81 6
            $this->form->setAttribute($key, $value);
82
        }
83
84
        // set Id as name or method as post if not set
85 6
        $method = $this->getMethod($form);
86 6
        $id = $this->getId($form);
87
88 6
        $this->form->setAttribute('id', $id);
89 6
        $this->form->setAttribute('method', $method);
90 6
    }
91
92
    /**
93
     * @param FormInterface $form
94
     * @return string
95
     */
96 6
    private function getMethod(FormInterface $form)
97
    {
98 6
        return $form->getMethod() ?: AbstractForm::METHOD_POST;
99
    }
100
101
    /**
102
     * @param FormInterface $form
103
     * @return string
104
     */
105 6
    private function getId(FormInterface $form)
106
    {
107 6
        return $form->getId() ?: $this->form->getAttribute('name');
108
    }
109
110 6
    private function processFields(FieldCollection $fields)
111
    {
112 6
        $fields->rewind();
113 6
        while ($fields->valid()) {
114 5
            $this->block = $this->dom->createElement('div');
115 5
            $this->field = $fields->current();
116 5
            $this->label = $this->renderFieldLabel();
117 5
            $this->element = $this->field->getRenderer()->render($this->dom, $this->field);
118 5
            $this->errors = $this->field->isValid() ? null : $this->renderError();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->field->isValid() ... : $this->renderError() can also be of type object<Del\Form\Renderer\DOMElement>. However, the property $errors is declared as type object<DOMElement>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
119 5
            $this->block = $this->renderFieldBlock();
120 5
            $this->form->appendChild($this->block);
121 5
            $fields->next();
122
        }
123 6
        $fields->rewind();
124 6
    }
125
126
127
128
    /**
129
     * @return DOMElement|null
130
     */
131 3
    public function renderError()
132
    {
133 3
        $errorBlock = null;
134 3
        if (!$this->field->isValid() && $this->displayErrors === true) {
135 2
            $this->block->setAttribute('class', 'has-error ');
136 2
            $errorBlock = $this->errorRenderer->render($this->field);
137
        }
138 3
        return $errorBlock;
139
    }
140
}