AbstractRenderer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 12.26 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 13
loc 106
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getElement() 0 4 1
A setElement() 0 5 1
A getEngine() 0 7 2
A setEngine() 0 5 1
A getAttributes() 13 13 3
A render() 0 5 1

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
/**
4
 * This file is part of slick/form package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Form\Renderer;
11
12
use Slick\Form\ElementInterface;
13
use Slick\Template\EngineInterface;
14
15
/**
16
 * AbstractRenderer: base renderer
17
 *
18
 * @package Slick\Form\Renderer
19
 * @author  Filipe Silva <[email protected]>
20
 */
21
abstract class AbstractRenderer
22
{
23
24
    /**
25
     * @var ElementInterface
26
     */
27
    protected $element;
28
29
    /**
30
     * @var EngineInterface
31
     */
32
    protected $engine;
33
34
    /**
35
     * Creates an abstract render with element dependency.
36
     *
37
     * @param ElementInterface|null $element
38
     */
39 20
    public function __construct(ElementInterface $element = null)
40
    {
41 20
        $this->element = $element;
42 20
    }
43
44
    /**
45
     * Returns current element
46
     *
47
     * @return ElementInterface
48
     */
49 20
    public function getElement()
50
    {
51 20
        return $this->element;
52
    }
53
54
    /**
55
     * Sets HTML element to render
56
     *
57
     * @param ElementInterface $element
58
     *
59
     * @return $this|self|AbstractRenderer
60
     */
61 20
    public function setElement(ElementInterface $element)
62
    {
63 20
        $this->element = $element;
64 20
        return $this;
65
    }
66
67
    /**
68
     * Returns the current template engine
69
     *
70
     * If no template is provided a new one is created with default settings.
71
     *
72
     * @return EngineInterface
73
     */
74 20
    public function getEngine()
75
    {
76 20
        if (null === $this->engine) {
77 20
            $this->setEngine(TemplateFactory::getEngine());
78 20
        }
79 20
        return $this->engine;
80
    }
81
82
    /**
83
     * Sets current template engine
84
     *
85
     * @param EngineInterface $engine
86
     *
87
     * @return $this|self|AbstractRenderer
88
     */
89 20
    public function setEngine($engine)
90
    {
91 20
        $this->engine = $engine;
92 20
        return $this;
93
    }
94
95
    /**
96
     * Returns the elements's attributes as a string
97
     *
98
     * @return string
99
     */
100 18 View Code Duplication
    public function getAttributes()
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...
101
    {
102 18
        $result = [];
103 18
        foreach ($this->element->getAttributes() as $attribute => $value) {
104 18
            if (null === $value) {
105 6
                $result[] = $attribute;
106 6
                continue;
107
            }
108
109 18
            $result[] = "{$attribute}=\"{$value}\"";
110 18
        }
111 18
        return implode(' ', $result);
112
    }
113
114
    /**
115
     * Render the HTML element in the provided context
116
     *
117
     * @param array $context
118
     *
119
     * @return string The HTML string output
120
     */
121 20
    public function render($context = [])
122
    {
123 20
        $this->getElement()->setRendering(true);
124 20
        return '';
125
    }
126
}