Completed
Push — master ( 88111b...b3fce7 )
by Michael
05:58
created

Customtag   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 224
Duplicated Lines 43.75 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 98
loc 224
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A getVar() 0 9 3
A render() 0 9 2
A renderWithPhp() 0 20 4
A getXoopsCode() 0 6 1
A getCloneLink() 0 6 1
A emptyString() 0 4 1
B generateTag() 98 98 1
A getCustomtagName() 0 6 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 namespace XoopsModules\Smartobject;
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright    XOOPS Project https://xoops.org/
15
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @package
17
 * @since
18
 * @author     XOOPS Development Team
19
 */
20
21
use XoopsModules\Smartobject;
22
23
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
25
//require_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobject.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
27
/**
28
 * Class SmartobjectCustomtag
29
 */
30
class Customtag extends Smartobject\BaseSmartObject
31
{
32
    public $content = false;
33
34
    /**
35
     * SmartobjectCustomtag constructor.
36
     */
37
    public function __construct()
38
    {
39
        $this->quickInitVar('customtagid', XOBJ_DTYPE_INT, true);
40
        $this->quickInitVar('name', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_CUSTOMTAG_NAME, _CO_SOBJECT_CUSTOMTAG_NAME_DSC);
41
        $this->quickInitVar('description', XOBJ_DTYPE_TXTAREA, false, _CO_SOBJECT_CUSTOMTAG_DESCRIPTION, _CO_SOBJECT_CUSTOMTAG_DESCRIPTION_DSC);
42
        $this->quickInitVar('content', XOBJ_DTYPE_TXTAREA, true, _CO_SOBJECT_CUSTOMTAG_CONTENT, _CO_SOBJECT_CUSTOMTAG_CONTENT_DSC);
43
        $this->quickInitVar('language', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_CUSTOMTAG_LANGUAGE, _CO_SOBJECT_CUSTOMTAG_LANGUAGE_DSC);
44
45
        $this->initNonPersistableVar('dohtml', XOBJ_DTYPE_INT, 'class', 'dohtml', '', true);
46
        $this->initNonPersistableVar('doimage', XOBJ_DTYPE_INT, 'class', 'doimage', '', true);
47
        $this->initNonPersistableVar('doxcode', XOBJ_DTYPE_INT, 'class', 'doxcode', '', true);
48
        $this->initNonPersistableVar('dosmiley', XOBJ_DTYPE_INT, 'class', 'dosmiley', '', true);
49
50
        $this->setControl('content', [
51
            'name'        => 'textarea',
52
            'form_editor' => 'textarea',
53
            'form_rows'   => 25
54
        ]);
55
        $this->setControl('language', [
56
            'name' => 'language',
57
            'all'  => true
58
        ]);
59
    }
60
61
    /**
62
     * @param  string $key
63
     * @param  string $format
64
     * @return mixed
65
     */
66
    public function getVar($key, $format = 's')
67
    {
68
        if ('s' === $format && in_array($key, [])) {
69
            //            return call_user_func(array($this, $key));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
            return $this->{$key}();
71
        }
72
73
        return parent::getVar($key, $format);
74
    }
75
76
    /**
77
     * @return bool|mixed
78
     */
79
    public function render()
80
    {
81
        if (!$this->content) {
82
            $ret           = $this->getVar('content');
83
            $this->content = $ret;
84
        }
85
86
        return $this->content;
87
    }
88
89
    /**
90
     * @return bool|mixed|string
91
     */
92
    public function renderWithPhp()
93
    {
94
        if (!$this->content) {
95
            $ret           = $this->getVar('content');
96
            $this->content = $ret;
97
        } else {
98
            $ret = $this->content;
99
        }
100
101
        // check for PHP if we are not on admin side
102
        if (!defined('XOOPS_CPFUNC_LOADED') && !(false === strpos($ret, '[php]'))) {
103
            $ret = str_replace('[php]', '', $ret);
104
            // we have PHP code, let's evaluate
105
            eval($ret);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
106
107
            return '';
108
        }
109
110
        return $this->content;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getXoopsCode()
117
    {
118
        $ret = '[customtag]' . $this->getVar('tag', 'n') . '[/customtag]';
119
120
        return $ret;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getCloneLink()
127
    {
128
        $ret = '<a href="' . SMARTOBJECT_URL . 'admin/customtag.php?op=clone&customtagid=' . $this->id() . '"><img src="' . SMARTOBJECT_IMAGES_ACTIONS_URL . 'editcopy.png" style="vertical-align: middle;" alt="' . _CO_SOBJECT_CUSTOMTAG_CLONE . '" title="' . _CO_SOBJECT_CUSTOMTAG_CLONE . '"></a>';
129
130
        return $ret;
131
    }
132
133
    /**
134
     * @param $var
135
     * @return bool
136
     */
137
    public function emptyString($var)
138
    {
139
        return (strlen($var) > 0);
140
    }
141
142
    /**
143
     * @return mixed|string
144
     */
145 View Code Duplication
    public function generateTag()
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...
146
    {
147
        $title = rawurlencode(strtolower($this->getVar('description', 'e')));
148
        $title = xoops_substr($title, 0, 10, '');
149
        // Transformation des ponctuations
150
        $pattern = [
151
            '/%09/', // Tab
152
            '/%20/', // Space
153
            '/%21/', // !
154
            '/%22/', // "
155
            '/%23/', // #
156
            '/%25/', // %
157
            '/%26/', // &
158
            '/%27/', // '
159
            '/%28/', // (
160
            '/%29/', // )
161
            '/%2C/', // ,
162
            '/%2F/', // /
163
            '/%3A/', // :
164
            '/%3B/', // ;
165
            '/%3C/', // <
166
            '/%3D/', // =
167
            '/%3E/', // >
168
            '/%3F/', // ?
169
            '/%40/', // @
170
            '/%5B/', // [
171
            '/%5C/', // \
172
            '/%5D/', // ]
173
            '/%5E/', // ^
174
            '/%7B/', // {
175
            '/%7C/', // |
176
            '/%7D/', // }
177
            '/%7E/', // ~
178
            "/\./" // .
179
        ];
180
        $rep_pat = [
181
            '-',
182
            '-',
183
            '-',
184
            '-',
185
            '-',
186
            '-100',
187
            '-',
188
            '-',
189
            '-',
190
            '-',
191
            '-',
192
            '-',
193
            '-',
194
            '-',
195
            '-',
196
            '-',
197
            '-',
198
            '-',
199
            '-at-',
200
            '-',
201
            '-',
202
            '-',
203
            '-',
204
            '-',
205
            '-',
206
            '-',
207
            '-',
208
            '-'
209
        ];
210
        $title   = preg_replace($pattern, $rep_pat, $title);
211
212
        // Transformation des caractères accentués
213
        $pattern = [
214
            '/%B0/', // °
215
            '/%E8/', // è
216
            '/%E9/', // é
217
            '/%EA/', // ê
218
            '/%EB/', // ë
219
            '/%E7/', // ç
220
            '/%E0/', // à
221
            '/%E2/', // â
222
            '/%E4/', // ä
223
            '/%EE/', // î
224
            '/%EF/', // ï
225
            '/%F9/', // ù
226
            '/%FC/', // ü
227
            '/%FB/', // û
228
            '/%F4/', // ô
229
            '/%F6/', // ö
230
        ];
231
        $rep_pat = ['-', 'e', 'e', 'e', 'e', 'c', 'a', 'a', 'a', 'i', 'i', 'u', 'u', 'u', 'o', 'o'];
232
        $title   = preg_replace($pattern, $rep_pat, $title);
233
234
        $tableau = explode('-', $title); // Transforme la chaine de caract�res en tableau
235
        $tableau = array_filter($tableau, [$this, 'emptyString']); // Supprime les chaines vides du tableau
236
        $title   = implode('-', $tableau); // Transforme un tableau en chaine de caract�res s�par� par un tiret
237
238
        $title .= time();
239
        $title = md5($title);
240
241
        return $title;
242
    }
243
244
    /**
245
     * @return mixed
246
     */
247
    public function getCustomtagName()
248
    {
249
        $ret = $this->getVar('name');
250
251
        return $ret;
252
    }
253
}
254