Completed
Push — master ( 312098...b95ba9 )
by Nikolas
40:06
created

LinkPrinter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 13 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 13
c 7
b 0
f 2
lcom 1
cbo 6
dl 13
loc 100
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createLinkElements() 0 3 1
B createDropDown() 0 32 4
B createLinks() 0 22 4
A makeUrl() 13 13 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\link;
3
4
use rtens\domin\ActionRegistry;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\resources\ExecutionResource;
7
use rtens\domin\delivery\web\WebCommentParser;
8
9
class LinkPrinter {
10
11
    /** @var LinkRegistry */
12
    private $links;
13
14
    /** @var ActionRegistry */
15
    private $actions;
16
17
    /** @var WebCommentParser */
18
    private $parser;
19
20
    public function __construct(LinkRegistry $links, ActionRegistry $actions, WebCommentParser $parser) {
21
        $this->links = $links;
22
        $this->actions = $actions;
23
        $this->parser = $parser;
24
    }
25
26
    /**
27
     * @param mixed $object
28
     * @return array
29
     */
30
    public function createLinkElements($object) {
31
        return $this->createLinks($object, 'btn btn-xs btn-primary');
32
    }
33
34
    /**
35
     * @param mixed $object
36
     * @param string|null $caption
37
     * @return array|\rtens\domin\delivery\web\Element[]
38
     */
39
    public function createDropDown($object, $caption = null) {
40
        $links = $this->createLinks($object);
41
        if (empty($links)) {
42
            return [];
43
        }
44
45
        if (!$caption) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $caption of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
46
            if (is_object($object)) {
47
                $caption = (new \ReflectionClass($object))->getShortName();
48
            } else {
49
                $caption = 'Actions';
50
            }
51
        }
52
53
        return [
54
            new Element('div', ['class' => 'dropdown'], [
55
                new Element('button', [
56
                    'class' => 'btn btn-xs btn-primary dropdown-toggle',
57
                    'type' => 'button',
58
                    'data-toggle' => 'dropdown',
59
                    'aria-haspopup' => 'true',
60
                    'aria-expanded' => 'false'
61
                ], [
62
                    $caption,
63
                    new Element('span', ['class' => 'caret'])
64
                ]),
65
                new Element('ul', ['class' => 'dropdown-menu'], array_map(function (Element $element) {
66
                    return new Element('li', [], [$element]);
67
                }, $links))
68
            ])
69
        ];
70
    }
71
72
    private function createLinks($object, $classes = '') {
73
        return array_map(function (Link $link) use ($object, $classes) {
74
            $action = $this->actions->getAction($link->actionId());
75
76
            $parameters = $link->parameters($object);
77
            if ($link->force()) {
78
                $parameters[ExecutionResource::FORCE_ARG] = true;
79
            }
80
81
            $attributes = ['class' => $classes, 'href' => $this->makeUrl($link->actionId(), $parameters)];
82
            if ($link->confirm() !== null) {
83
                $attributes['onclick'] = "return confirm('{$link->confirm()}');";
84
            }
85
            $description = $action->description();
86
            if (!is_null($description)) {
87
                $attributes['title'] = str_replace('"', "'", strip_tags($this->parser->shorten($description)));
88
            }
89
            return new Element('a', $attributes, [
90
                $action->caption()
91
            ]);
92
        }, $this->links->getLinks($object));
93
    }
94
95 View Code Duplication
    private function makeUrl($actionId, array $parameters) {
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...
96
        $url = $actionId;
97
98
        if ($parameters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parameters 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...
99
            $keyValues = [];
100
            foreach ($parameters as $key => $value) {
101
                $keyValues[] = urlencode($key) . '=' . urlencode($value);
102
            }
103
            $url .= '?' . implode('&', $keyValues);
104
        }
105
106
        return $url;
107
    }
108
}