Completed
Pull Request — master (#87)
by Sebastian
18:02 queued 13:56
created

HtmlBuilder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 102
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 24
loc 102
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A flashMessage() 0 10 2
A error() 0 14 4
A message() 0 8 2
A info() 12 12 2
A warning() 12 12 2
A avatar() 0 7 1
A deleteButton() 0 12 1
A onlineIndicator() 0 12 4
A backToIndex() 0 4 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
namespace App\Services\Html;
4
5
use App\Services\Auth\User;
6
use Collective\Html\HtmlBuilder as BaseHtmlBuilder;
7
use Form;
8
use Session;
9
10
class HtmlBuilder extends BaseHtmlBuilder
11
{
12
    public function flashMessage(): string
13
    {
14
        if (! Session::has('flash_notification.message')) {
15
            return '';
16
        }
17
18
        $level = Session::get('flash_notification.level');
19
20
        return el("div.alert.-{$level}", Session::get('flash_notification.message'));
21
    }
22
23
    public function error($message, $name = ''): string
24
    {
25
        if (empty($message) && empty($name)) {
26
            return '';
27
        }
28
29
        $attributes = empty($name) ? [] : ['data-validation-error' => $name];
30
31
        return el(
32
            'div.alert.-danger',
33
            $attributes,
34
            $message
35
        );
36
    }
37
38
    public function message($message, string $classes = ''): string
39
    {
40
        if (empty($message)) {
41
            return '';
42
        }
43
44
        return el('div.alert.-success', ['class' => $classes], $message);
45
    }
46
47 View Code Duplication
    public function info($message, string $classes = ''): string
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...
48
    {
49
        if (empty($message)) {
50
            return '';
51
        }
52
53
        return el(
54
            'div.alert.-info',
55
            ['class' => $classes],
56
            el('span.fa.fa-info-circle').' '.$message
57
        );
58
    }
59
60 View Code Duplication
    public function warning($message, string $classes = ''): string
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...
61
    {
62
        if (empty($message)) {
63
            return '';
64
        }
65
66
        return el(
67
            'div.alert.-warning',
68
            ['class' => $classes],
69
            el('span.fa.fa-exclamation-triangle').' '.$message
70
        );
71
    }
72
73
    public function avatar(User $user, string $classes = ''): string
74
    {
75
        return el('span.avatar', [
76
            'class' => $classes,
77
            'style' => "background-image: url('{$user->avatar}')",
78
        ], '');
79
    }
80
81
    public function deleteButton(string $url): string
82
    {
83
        return Form::openButton(
84
            [
85
                'url' => $url,
86
                'method' => 'delete',
87
            ],
88
            [
89
                'class' => 'button -danger -small',
90
            ]
91
        ).el('span.fa.fa-remove').Form::closeButton();
92
    }
93
94
    public function onlineIndicator(bool $online): string
95
    {
96
        $state = $online ? 'on' : 'off';
97
        $icon = $online ? 'circle' : 'circle-o';
98
        $title = $online ? 'Online' : 'Offline';
99
100
        return el(
101
            "span.status.-{$state}",
102
            ['title' => $title],
103
            el("i.fa.fa-{$icon}")
104
        );
105
    }
106
107
    public function backToIndex(string $action, array $parameters = []): string
108
    {
109
        return el('a.breadcrumb--back', ['href' => action($action, $parameters)], fragment('back.backToIndex'));
110
    }
111
}
112