Completed
Pull Request — master (#1281)
by
unknown
02:32
created

Navbar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 14.1 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 11
loc 78
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A left() 0 6 1
A right() 0 6 1
A add() 0 4 1
B render() 11 18 5

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
namespace Encore\Admin\Widgets;
4
5
use Illuminate\Contracts\Support\Htmlable;
6
use Illuminate\Contracts\Support\Renderable;
7
8
class Navbar implements Renderable
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $elements = [];
14
15
    /**
16
     * Navbar constructor.
17
     */
18
    public function __construct()
19
    {
20
        $this->elements = [
21
            'left'   => collect(),
22
            'right'  => collect(),
23
        ];
24
    }
25
26
    /**
27
     * @param $element
28
     *
29
     * @return $this
30
     */
31
    public function left($element)
32
    {
33
        $this->elements['left']->push($element);
34
35
        return $this;
36
    }
37
38
    /**
39
     * @param $element
40
     *
41
     * @return $this
42
     */
43
    public function right($element)
44
    {
45
        $this->elements['right']->push($element);
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param $element
52
     *
53
     * @return Navbar
54
     *
55
     * @deprecated
56
     */
57
    public function add($element)
58
    {
59
        return $this->right($element);
60
    }
61
62
    /**
63
     * @param string $part
64
     *
65
     * @return mixed
66
     */
67
    public function render($part = 'right')
68
    {
69
        if (!isset($this->elements[$part]) || $this->elements[$part]->isEmpty()) {
70
            return '';
71
        }
72
73 View Code Duplication
        return $this->elements[$part]->map(function ($element) {
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...
74
            if ($element instanceof Htmlable) {
75
                return $element->toHtml();
76
            }
77
78
            if ($element instanceof Renderable) {
79
                return $element->render();
80
            }
81
82
            return (string) $element;
83
        })->implode('');
84
    }
85
}
86