Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

AdminHelper   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 200
Duplicated Lines 6.5 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 13
loc 200
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A help() 0 4 1
A _cacheBadge() 0 19 5
A badge() 0 16 3
A addBreadcrumb() 0 13 2
B formatCakeLog() 0 46 6
A jqueryTable() 0 22 1
A accessionToRoles() 13 13 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 Admin\View\Helper;
4
5
use Admin\Lib\CakeLogEntry;
6
use App\View\Helper\AppHelper;
7
use App\View\Helper\TimeHHelper;
8
use Cake\View\Helper\BreadcrumbsHelper;
9
use Cake\View\Helper\HtmlHelper;
10
use SaitoHelp\View\Helper\SaitoHelpHelper;
11
12
/**
13
 * @property BreadcrumbsHelper $Breadcrumbs
14
 * @property HtmlHelper $Html
15
 * @property SaitoHelpHelper $SaitoHelp
16
 * @property TimeHHelper $TimeH
17
 */
18
class AdminHelper extends AppHelper
19
{
20
    public $helpers = [
21
        'Breadcrumbs',
22
        'SaitoHelp',
23
        'Html',
24
        'TimeH'
25
    ];
26
27
    /**
28
     * help
29
     *
30
     * @param string $id id
31
     * @return mixed
32
     */
33
    public function help($id)
34
    {
35
        return $this->SaitoHelp->icon($id, ['style' => 'float: right;']);
36
    }
37
38
    /**
39
     * cache badge
40
     *
41
     * @param string $engine engine
42
     * @return string
43
     */
44
    protected function _cacheBadge($engine)
45
    {
46
        switch ($engine) {
47
            case 'File':
48
                $badge = 'warning';
49
                break;
50
            case 'Apc':
51
            case 'Apcu':
52
                $badge = 'success';
53
                break;
54
            case 'Debug':
55
                $badge = 'important';
56
                break;
57
            default:
58
                $badge = 'info';
59
        }
60
61
        return $badge;
62
    }
63
64
    /**
65
     * badge
66
     *
67
     * @param string $text text
68
     * @param null $type type
69
     * @return mixed
70
     */
71
    public function badge($text, $type = null)
72
    {
73
        if (is_callable([$this, $type])) {
74
            $badge = $this->$type($text);
75
        } elseif (is_string(($type))) {
76
            $badge = $type;
77
        } else {
78
            $badge = 'info';
79
        }
80
81
        return $this->Html->tag(
82
            'span',
83
            $text,
84
            ['class' => "badge badge-$badge"]
85
        );
86
    }
87
88
    /**
89
     * Adds Breadcrumb item
90
     *
91
     * @see BreadcrumbsHelper::add()
92
     *
93
     * @param string $title Title
94
     * @param string $url URL
95
     * @param array $options Options
96
     * @return BreadcrumbsHelper
97
     */
98
    public function addBreadcrumb($title, $url = null, array $options = [])
99
    {
100
        $options += ['class' => ''];
101
        // set breadcrumb item class for Bootstrap
102
        $options['class'] = $options['class'] . ' breadcrumb-item';
103
        // last item in breadcrump is current (active) page and not linked
104
        if ($url === false) {
105
            // set breadcrumb active item class for Bootstrap
106
            $options['class'] .= ' active';
107
        }
108
109
        return $this->Breadcrumbs->add($title, $url, $options);
110
    }
111
112
    /**
113
     * format cake log
114
     *
115
     * @bogus the ability to see logs isn't in Saito 5 anymore
116
     *
117
     * @param string $log log
118
     * @return string
119
     */
120
    public function formatCakeLog($log)
121
    {
122
        $_nErrorsToShow = 20;
123
        $errors = preg_split(
124
            '/(?=^\d{4}-\d{2}-\d{2})/m',
125
            $log,
126
            -1,
127
            PREG_SPLIT_NO_EMPTY
128
        );
129
        if (empty($errors)) {
130
            return '<p>' . __('No log file found.') . '</p>';
131
        }
132
133
        $out = '';
134
        $k = 0;
135
        $errors = array_reverse($errors);
136
        foreach ($errors as $error) {
137
            $e = new CakeLogEntry($error);
138
            $_i = self::tagId();
139
            $_details = $e->details();
140
            if (!empty($_details)) {
141
                $out .= '<button class="btn btn-mini" style="float:right;" onclick="$(\'#' . $_i . '\').toggle(); return false;">' . __(
142
                    'Details'
143
                ) . '</button>' . "\n";
144
            }
145
            $out .= '<pre style="font-size: 10px;">' . "\n";
146
            $out .= '<div class="row"><div class="span2" style="text-align: right">';
147
            $out .= $this->TimeH->formatTime($e->time(), 'eng');
0 ignored issues
show
Documentation introduced by
$e->time() is of type string, but the function expects a object<DateTimeInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
149
            $out .= '</div>';
150
            $out .= '<div class="span7">';
151
            $out .= $e->message();
152
            if (!empty($_details)) {
153
                $out .= '<span id="' . $_i . '" style="display: none;">' . "\n";
154
                $out .= $_details;
155
                $out .= '</span>';
156
            }
157
            $out .= '</div></div>';
158
            $out .= '</pre>' . "\n";
159
            if ($k++ > $_nErrorsToShow) {
160
                break;
161
            }
162
        }
163
164
        return $out;
165
    }
166
167
    /**
168
     * jquery table
169
     *
170
     * @param string $selector selector
171
     * @param string $sort sort
172
     *
173
     * @return void
174
     */
175
    public function jqueryTable($selector, $sort)
0 ignored issues
show
Unused Code introduced by
The parameter $sort is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
    {
177
        $this->Html->css(
178
            '../js/node_modules/datatables.net-bs4/css/dataTables.bootstrap4.css',
179
            ['block' => 'script']
180
        );
181
        $this->Html->script(
182
            [
183
                '../js/node_modules/datatables.net/js/jquery.dataTables.js',
184
                '../js/node_modules/datatables.net-bs4/js/dataTables.bootstrap4.js'
185
            ],
186
            ['block' => 'script']
187
        );
188
189
        $script = <<<EOF
190
$(function() {
191
    var userTable = $('{$selector}').DataTable();
192
});
193
EOF;
194
195
        $this->Html->scriptBlock($script, ['block' => 'script']);
196
    }
197
198
    /**
199
     * accession to roles
200
     *
201
     * @param int $accession accession
202
     * @return string
203
     */
204 View Code Duplication
    public function accessionToRoles($accession)
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...
205
    {
206
        switch ($accession) {
207
            case (0):
208
                return __('user.type.anon');
209
            case (1):
210
                return __('user.type.user');
211
            case (2):
212
                return __('user.type.mod');
213
            case (3):
214
                return __('user.type.admin');
215
        }
216
    }
217
}
218