Passed
Push — master ( 5fbe19...8394af )
by Goffy
04:16
created

CreateSmartyCode   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 311
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 311
rs 8.96
c 0
b 0
f 0
wmc 43

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getSmartySingleVar() 0 3 1
A getSmartyNoSimbol() 0 3 1
A getSmartyConst() 0 3 1
A getSmartyDoubleVar() 0 3 1
A getSmartyComment() 0 3 1
A __construct() 0 2 1
A getInstance() 0 8 2
A getAttributes() 0 10 3
A getSmartyTag() 0 9 2
A getSmartyEmpty() 0 3 1
A getSmartyIncludeFileListForeach() 0 3 1
B getSmartyIncludeFile() 0 14 9
A getSmartyIncludeFileListSection() 0 3 1
B getSmartyConditions() 0 36 9
A getSmartyForeach() 0 9 3
A getSmartySection() 0 9 3
A getSmartyForeachQuery() 0 9 3

How to fix   Complexity   

Complex Class

Complex classes like CreateSmartyCode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CreateSmartyCode, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files;
4
5
use  XoopsModules\Modulebuilder;
6
use  XoopsModules\Modulebuilder\Files;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
/**
18
 * modulebuilder module.
19
 *
20
 * @copyright       XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.0
24
 *
25
 * @author          Txmod Xoops http://www.txmodxoops.org
26
 *
27
 */
28
29
/**
30
 * Class CreateSmartyCode.
31
 */
32
class CreateSmartyCode
33
{
34
    /**
35
     * @public function constructor
36
     * @param null
37
     */
38
    public function __construct()
39
    {
40
    }
41
42
    /**
43
     * @static function getInstance
44
     * @param null
45
     * @return Files\CreateSmartyCode
46
     */
47
    public static function getInstance()
48
    {
49
        static $instance = false;
50
        if (!$instance) {
51
            $instance = new self();
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     * @public function getSmartyTag
59
     *
60
     * @param string $tag
61
     * @param array  $attributes
62
     * @param string $content
63
     *
64
     * @param string $t
65
     * @return string
66
     */
67
    public function getSmartyTag($tag = '', $attributes = [], $content = '', $t = '')
68
    {
69
        if (empty($attributes)) {
70
            $attributes = [];
71
        }
72
        $attr = $this->getAttributes($attributes);
73
        $ret  = "{$t}<{{$tag}{$attr}}>{$content}<{/{$tag}}>";
74
75
        return $ret;
76
    }
77
78
    /**
79
     * @private function setAttributes
80
     * @param array $attributes
81
     *
82
     * @return string
83
     */
84
    private function getAttributes($attributes)
85
    {
86
        $str = '';
87
        foreach ($attributes as $name => $value) {
88
            if ('_' !== $name) {
89
                $str .= ' ' . $name . '="' . $value . '"';
90
            }
91
        }
92
93
        return $str;
94
    }
95
96
    /**
97
     * @public function getSmartyEmpty
98
     * @param string $empty
99
     *
100
     * @return string
101
     */
102
    public function getSmartyEmpty($empty = '')
103
    {
104
        return (string)$empty;
105
    }
106
107
    /**
108
     * @public function getSmartyComment
109
     * @param string $comment
110
     * @param string $t
111
     * @param string $n
112
     * @return string
113
     */
114
    public function getSmartyComment($comment = '', $t = '', $n = "\n")
115
    {
116
        return "{$t}<{* {$comment} *}>{$n}";
117
    }
118
119
    /**
120
     * @public function getSmartyNoSimbol
121
     * @param string $noSimbol
122
     * @param string $t
123
     * @return string
124
     */
125
    public function getSmartyNoSimbol($noSimbol = '', $t = '')
126
    {
127
        return "{$t}<{{$noSimbol}}>";
128
    }
129
130
    /**
131
     * @public function getSmartyConst
132
     * @param string $language
133
     * @param mixed  $const
134
     * @param string $t
135
     * @param string $n
136
     * @return string
137
     */
138
    public function getSmartyConst($language, $const, $t = '', $n = '')
139
    {
140
        return "{$t}<{\$smarty.const.{$language}{$const}}>{$n}";
141
    }
142
143
    /**
144
     * @public function getSmartySingleVar
145
     * @param string $var
146
     * @param string $t
147
     * @param string $n
148
     * @return string
149
     */
150
    public function getSmartySingleVar($var, $t = '', $n = "")
151
    {
152
        return "{$t}<{\${$var}}>{$n}";
153
    }
154
155
    /**
156
     * @public function getSmartyDoubleVar
157
     * @param string $leftVar
158
     * @param string $rightVar
159
     * @param string $t
160
     * @param string $n
161
     * @return string
162
     */
163
    public function getSmartyDoubleVar($leftVar, $rightVar, $t = '', $n = "")
164
    {
165
        return "{$t}<{\${$leftVar}.{$rightVar}}>{$n}";
166
    }
167
168
    /**
169
     * @public function getSmartyIncludeFile
170
     * @param        $moduleDirname
171
     * @param string $fileName
172
     * @param bool $admin
173
     *
174
     * @param bool $q
175
     * @param string $t
176
     * @param string $n
177
     * @param string $attributes
178
     * @return string
179
     */
180
    public function getSmartyIncludeFile($moduleDirname, $fileName = 'header', $admin = false, $q = false, $t = '', $n = "\n", $attributes = '')
181
    {
182
        $ret = '';
183
        if (!$admin && !$q) {
184
            $ret = "{$t}<{include file='db:{$moduleDirname}_{$fileName}.tpl' {$attributes}}>{$n}";
185
        } elseif ($admin && !$q) {
186
            $ret = "{$t}<{include file='db:{$moduleDirname}_admin_{$fileName}.tpl' {$attributes}}>{$n}";
187
        } elseif (!$admin && $q) {
188
            $ret = "{$t}<{includeq file='db:{$moduleDirname}_{$fileName}.tpl' {$attributes}}>{$n}";
189
        } elseif ($admin && $q) {
190
            $ret = "{$t}<{includeq file='db:{$moduleDirname}_admin_{$fileName}.tpl' {$attributes}}>{$n}";
191
        }
192
193
        return $ret;
194
    }
195
196
    /**
197
     * @public function getSmartyIncludeFileListSection
198
     * @param $moduleDirname
199
     * @param $fileName
200
     * @param $itemName
201
     * @param $arrayName
202
     * @param string $t
203
     * @param string $n
204
     * @return string
205
     */
206
    public function getSmartyIncludeFileListSection($moduleDirname, $fileName, $itemName, $arrayName, $t = '', $n = '')
207
    {
208
        return "{$t}<{include file='db:{$moduleDirname}_{$fileName}_list.tpl' {$itemName}=\${$arrayName}[i]}>{$n}";
209
    }
210
211
    /**
212
     * @public function getSmartyIncludeFileListForeach
213
     * @param $moduleDirname
214
     * @param $fileName
215
     * @param $tableFieldName
216
     * @param string $t
217
     * @param string $n
218
     * @return string
219
     */
220
    public function getSmartyIncludeFileListForeach($moduleDirname, $fileName, $tableFieldName, $t = '', $n = '')
221
    {
222
        return "{$t}<{include file='db:{$moduleDirname}_{$fileName}_list.tpl' {$tableFieldName}=\${$tableFieldName}}>{$n}";
223
    }
224
225
    /**
226
     * @public function getSmartyConditions
227
     * @param string $condition
228
     * @param string $operator
229
     * @param string $type
230
     * @param string $contentIf
231
     * @param mixed $contentElse
232
     * @param bool $count
233
     * @param bool $noSimbol
234
     * @param string $t
235
     * @param string $n
236
     * @param bool $split
237
     * @param mixed $default
238
     * @return string
239
     */
240
    public function getSmartyConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $count = false, $noSimbol = false, $t = '', $n = "\n", $split = true, $default = 'string')
241
    {
242
        $ns = '';
243
        $ts = '';
244
        if ($split) {
245
            $ns = $n;
246
            $ts = $t;
247
        }
248
        if (!$count) {
249
            $ret = "{$t}<{if \${$condition}";
250
            if ('string' === $default) {
251
                $ret .= "|default:''";
252
            } elseif ('int' === $default) {
253
                $ret .= "|default:0";
254
            }
255
            $ret .= "{$operator}{$type}}>{$ns}";
256
        } elseif (!$noSimbol) {
257
            $ret = "{$t}<{if {$condition}";
258
            if ('string' === $default) {
259
                $ret .= "|default:''";
260
            } elseif ('int' === $default) {
261
                $ret .= "|default:0";
262
            }
263
            $ret .= "{$operator}{$type}}>{$ns}";
264
        } else {
265
            $ret = "{$t}<{if count(\${$condition}){$operator}{$type}}>{$ns}";
266
        }
267
        $ret .= "{$contentIf}";
268
        if ($contentElse) {
269
            $ret .= "{$ts}<{else}>{$ns}";
270
            $ret .= "{$contentElse}";
271
        }
272
        $ret .= "{$ts}<{/if}>{$n}";
273
274
275
        return $ret;
276
    }
277
278
    /**
279
     * @public function getSmartyForeach
280
     * @param string $item
281
     * @param string $from
282
     * @param string $content
283
     * @param string $name
284
     * @param string $key
285
     * @param string $t
286
     * @param string $n
287
     * @return string
288
     */
289
    public function getSmartyForeach($item = 'item', $from = 'from', $content = 'content', $name = '', $key = '', $t = '', $n = "\n")
290
    {
291
        $name = '' != $name ? " name={$name}" : '';
292
        $key  = '' != $key ? " key={$key}" : '';
293
        $ret  = "{$t}<{foreach item={$item} from=\${$from}{$key}{$name}}>{$n}";
294
        $ret  .= "{$content}";
295
        $ret  .= "{$t}<{/foreach}>{$n}";
296
297
        return $ret;
298
    }
299
300
    /**
301
     * @public function getSmartyForeachQuery
302
     * @param string $item
303
     * @param string $from
304
     * @param string $content
305
     *
306
     * @param string $loop
307
     * @param string $key
308
     * @param string $t
309
     * @param string $n
310
     * @return string
311
     */
312
    public function getSmartyForeachQuery($item = 'item', $from = 'from', $content = 'content', $loop = 'loop', $key = '', $t = '', $n = "\n")
313
    {
314
        $loop = '' != $loop ? " loop={$loop}" : '';
315
        $key  = '' != $key ? " key={$key}" : '';
316
        $ret  = "{$t}<{foreachq item={$item} from=\${$from}{$key}{$loop}}>{$n}";
317
        $ret  .= "{$content}";
318
        $ret  .= "{$t}<{/foreachq}>{$n}";
319
320
        return $ret;
321
    }
322
323
    /**
324
     * @public function getSmartySection
325
     * @param string $name
326
     * @param string $loop
327
     * @param string $content
328
     * @param int $start
329
     * @param int $step
330
     * @param string $t
331
     * @param string $n
332
     * @return string
333
     */
334
    public function getSmartySection($name = 'name', $loop = 'loop', $content = 'content', $start = 0, $step = 0, $t = '', $n = "\n")
335
    {
336
        $start = 0 != $start ? " start={$start}" : '';
337
        $step  = 0 != $step ? " step={$step}" : '';
338
        $ret   = "{$t}<{section name={$name} loop=\${$loop}{$start}{$step}}>{$n}";
339
        $ret   .= "{$content}";
340
        $ret   .= "{$t}<{/section}>{$n}";
341
342
        return $ret;
343
    }
344
}
345