Completed
Push — master ( 89c874...acb1c9 )
by Cheren
15:37
created

HtmlHelper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 178
Duplicated Lines 4.49 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 7
dl 8
loc 178
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A css() 0 5 1
A icon() 0 19 1
C less() 0 22 7
B link() 0 27 2
A script() 0 4 1
A _isEscapeTitle() 0 4 3
A _setTitleAttr() 0 8 2

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
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\View\Helper;
17
18
use Cake\View\View;
19
use Cake\Utility\Hash;
20
use Cake\Core\Configure;
21
use Core\View\Helper\Traits\HelperTrait;
22
use Core\View\Helper\Traits\IncludeTrait;
23
use Cake\View\Helper\HtmlHelper as CakeHtmlHelper;
24
25
/**
26
 * Class HtmlHelper
27
 *
28
 * @package Core\View\Helper
29
 * @property \Core\View\Helper\LessHelper $Less
30
 * @property \Core\View\Helper\UrlHelper $Url
31
 * @property \Core\View\Helper\DocumentHelper $Document
32
 */
33
class HtmlHelper extends CakeHtmlHelper
34
{
35
36
    use HelperTrait, IncludeTrait;
37
38
    /**
39
     * List of helpers used by this helper.
40
     *
41
     * @var array
42
     */
43
    public $helpers = [
44
        'Core.Less',
45
        'Core.Document',
46
        'Url' => ['className' => 'Core.Url'],
47
    ];
48
49
    /**
50
     * HtmlHelper constructor.
51
     *
52
     * @param View $View
53
     * @param array $config
54
     */
55 View Code Duplication
    public function __construct(View $View, array $config = [])
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...
56
    {
57
        parent::__construct($View, $config);
58
        $this->_configWrite('btnPref', Configure::read('Cms.btnPref'));
59
        $this->_configWrite('iconPref', Configure::read('Cms.iconPref'));
60
        $this->_configWrite('classPrefix', Configure::read('Cms.classPrefix'));
61
        $this->_configWrite('templates.icon', '<i class="{{class}}"{{attrs}}></i>');
62
    }
63
64
    /**
65
     * Creates a link element for CSS stylesheets.
66
     *
67
     * @param array|string $path
68
     * @param array $options
69
     * @return null|string
70
     */
71
    public function css($path, array $options = [])
72
    {
73
        $options += ['rel' => 'stylesheet'];
74
        return $this->_include($path, $options, 'css');
75
    }
76
77
    /**
78
     * Create icon element.
79
     *
80
     * @param string $icon
81
     * @param array $options
82
     * @return null|string
83
     */
84
    public function icon($icon = 'home', array $options = [])
85
    {
86
        $iconPref = $this->_configRead('iconPref');
87
        $_classes = [
88
            $this->_class(__FUNCTION__),
89
            $iconPref,
90
            $iconPref . '-' . $icon,
91
        ];
92
93
        $options = $this->_addClass($options, implode(' ', $_classes));
94
        $classes = $options['class'];
95
        unset($options['class']);
96
97
        $templater = $this->templater();
98
        return $templater->format(__FUNCTION__, [
99
            'class' => $classes,
100
            'attrs' => $templater->formatAttributes($options),
101
        ]);
102
    }
103
104
    /**
105
     * Creates a CSS stylesheets from less.
106
     *
107
     * @param string|array $path
108
     * @param array $options
109
     * @return null|string
110
     */
111
    public function less($path, array $options = [])
112
    {
113
        $cssPath = [];
114
115
        if (!isset($options['force'])) {
116
            $options['force'] = false;
117
        }
118
119
        if (is_array($path)) {
120
            foreach ($path as $i) {
121
                if ($result = $this->Less->process($i, $options['force'])) {
122
                    $cssPath[] = $result;
123
                }
124
            }
125
        }
126
127
        if (is_string($path) && $result = $this->Less->process($path, $options['force'])) {
128
            $cssPath[] = $result;
129
        }
130
131
        return $this->css($cssPath, $options);
132
    }
133
134
    /**
135
     * Create an html link.
136
     *
137
     * @param string $title
138
     * @param null|string|array $url
139
     * @param array $options
140
     * @return string
141
     */
142
    public function link($title, $url = null, array $options = [])
143
    {
144
        $options = $this->addClass($options, $this->_class(__FUNCTION__));
145
        $options = Hash::merge([
146
            'escapeTitle' => false,
147
            'clear'       => false,
148
            'label'       => $title,
149
        ], $options);
150
151
        $isClear = (bool) $options['clear'];
152
        unset($options['clear']);
153
154
        $options = $this->_setTitleAttr($title, $options);
155
156
        //  Set title in html tag.
157
        if ($this->_isEscapeTitle($title, $isClear, $options)) {
158
            $title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']);
159
        }
160
161
        $options = $this->_getBtnClass($options);
162
        $options = $this->_getToolTipAttr($options);
163
164
        list($title, $options) = $this->_createIcon($this, $title, $options);
165
        unset($options['label']);
166
167
        return parent::link($title, $url, $options);
168
    }
169
170
    /**
171
     * Returns one or many `<script>` tags depending on the number of scripts given.
172
     *
173
     * @param array|string $path
174
     * @param array $options
175
     * @return null|string
176
     */
177
    public function script($path, array $options = [])
178
    {
179
        return $this->_include($path, $options, 'script');
180
    }
181
182
    /**
183
     * Check if need escape link title.
184
     *
185
     * @param string $title
186
     * @param bool $isClear
187
     * @param array $options
188
     * @return bool
189
     */
190
    protected function _isEscapeTitle($title, $isClear, array $options = [])
191
    {
192
        return $options['escapeTitle'] === false && !empty($title) && !$isClear;
193
    }
194
195
    /**
196
     * Setup default title attr.
197
     *
198
     * @param string $title
199
     * @param array $options
200
     * @return array
201
     */
202
    protected function _setTitleAttr($title, array $options = [])
203
    {
204
        if (!isset($options['title'])) {
205
            $options['title'] = strip_tags($title);
206
        }
207
208
        return $options;
209
    }
210
}
211