MapRenderer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 5
dl 6
loc 74
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 1
A renderArray() 0 17 3
A __construct() 0 4 1
A render() 0 15 2
A headElements() 6 10 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
namespace rtens\domin\delivery\web\renderers;
3
4
use rtens\domin\delivery\RendererRegistry;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\renderers\link\LinkPrinter;
7
use rtens\domin\delivery\web\WebRenderer;
8
9
class MapRenderer implements WebRenderer {
10
11
    /** @var RendererRegistry */
12
    protected $renderers;
13
14
    /** @var LinkPrinter */
15
    private $links;
16
17
    public function __construct(RendererRegistry $renderers, LinkPrinter $links) {
18
        $this->renderers = $renderers;
19
        $this->links = $links;
20
    }
21
22
    /**
23
     * @param mixed $value
24
     * @return bool
25
     */
26
    public function handles($value) {
27
        return is_array($value);
28
    }
29
30
    /**
31
     * @param array $array
32
     * @return mixed
33
     */
34
    public function render($array) {
35
        $content = [
36
            new Element('div', ['class' => 'panel-body'], [
37
                $this->renderArray($array)
38
            ])
39
        ];
40
41
        $links = $this->links->createLinkElements($array);
42
        if ($links) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $links of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            array_unshift($content, new Element('div', ['class' => 'panel-heading clearfix'], [
44
                new Element('small', ['class' => 'pull-right'], $links)
45
            ]));
46
        }
47
        return new Element('div', ['class' => 'panel panel-default'], $content);
48
    }
49
50
    public function renderArray(array $array) {
51
        $descriptions = [];
52
53
        foreach ($array as $key => $value) {
54
            $caption = htmlentities(ucfirst($key));
55
            $descriptions[] = new Element('dt', [], [
56
                is_null($value)
57
                    ? new Element('s', [], [$caption])
58
                    : $caption
59
            ]);
60
            $descriptions[] = new Element('dd', [], [
61
                $this->renderers->getRenderer($value)->render($value)
62
            ]);
63
        }
64
65
        return new Element('dl', ['class' => 'dl-horizontal', 'style' => 'margin-bottom: 0;'], $descriptions);
66
    }
67
68
    /**
69
     * @param array $array
70
     * @return array|Element[]
71
     */
72
    public function headElements($array) {
73
        $elements = [];
74 View Code Duplication
        foreach ($array as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
            $renderer = $this->renderers->getRenderer($item);
76
            if ($renderer instanceof WebRenderer) {
77
                $elements = array_merge($elements, $renderer->headElements($item));
78
            }
79
        }
80
        return $elements;
81
    }
82
}