CreateSmartyCode   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 297
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 297
rs 9.2
c 0
b 0
f 0
wmc 40

17 Methods

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

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 namespace XoopsModules\Tdmcreate\Files;
2
3
use  XoopsModules\Tdmcreate;
4
use  XoopsModules\Tdmcreate\Files;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 */
15
/**
16
 * tdmcreate module.
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
20
 *
21
 * @since           2.5.0
22
 *
23
 * @author          Txmod Xoops http://www.txmodxoops.org
24
 *
25
 * @version         $Id: CreateSmartyCode.php 12258 2014-01-02 09:33:29Z timgno $
26
 */
27
28
/**
29
 * Class CreateSmartyCode.
30
 */
31
class CreateSmartyCode
32
{
33
    /**
34
    *  @public function constructor
35
    *  @param null
36
    */
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 $smartyComment
110
     * @param string $t
111
     * @return string
112
     */
113
    public function getSmartyComment($smartyComment = '', $t = '')
114
    {
115
        return "{$t}<{* {$smartyComment} *}>";
116
    }
117
118
    /**
119
     * @public function getSmartyNoSimbol
120
     * @param string $noSimbol
121
     *
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
     *
135
     * @param string $t
136
     * @return string
137
     */
138
    public function getSmartyConst($language, $const, $t = '')
139
    {
140
        return "{$t}<{\$smarty.const.{$language}{$const}}>";
141
    }
142
143
    /**
144
     * @public function getSmartySingleVar
145
     * @param string $var
146
     *
147
     * @param string $t
148
     * @return string
149
     */
150
    public function getSmartySingleVar($var, $t = '')
151
    {
152
        return "{$t}<{\${$var}}>";
153
    }
154
155
    /**
156
     * @public function getSmartyDoubleVar
157
     * @param string $leftVar
158
     * @param string $rightVar
159
     *
160
     * @param string $t
161
     * @return string
162
     */
163
    public function getSmartyDoubleVar($leftVar, $rightVar, $t = '')
164
    {
165
        return "{$t}<{\${$leftVar}.{$rightVar}}>";
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
     * @return string
177
     */
178
    public function getSmartyIncludeFile($moduleDirname, $fileName = 'header', $admin = false, $q = false, $t = '')
179
    {
180
        if (!$admin && !$q) {
181
            $ret = "{$t}<{include file='db:{$moduleDirname}_{$fileName}.tpl'}>\n";
182
        } elseif ($admin && !$q) {
183
            $ret = "{$t}<{include file='db:{$moduleDirname}_admin_{$fileName}.tpl'}>\n";
184
        } elseif (!$admin && $q) {
185
            $ret = "{$t}<{includeq file='db:{$moduleDirname}_{$fileName}.tpl'}>\n";
186
        } elseif ($admin && $q) {
187
            $ret = "{$t}<{includeq file='db:{$moduleDirname}_admin_{$fileName}.tpl'}>\n";
188
        }
189
190
        return $ret;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
191
    }
192
193
    /**
194
     * @public function getSmartyIncludeFileListSection
195
     * @param        $moduleDirname
196
     * @param        $fileName
197
     * @param        $tableFieldName
198
     *
199
     * @param string $t
200
     * @return string
201
     */
202
    public function getSmartyIncludeFileListSection($moduleDirname, $fileName, $tableFieldName, $t = '')
203
    {
204
        return "{$t}<{include file='db:{$moduleDirname}_{$fileName}_list.tpl' {$tableFieldName}=\${$tableFieldName}[i]}>\n";
205
    }
206
207
    /**
208
     * @public function getSmartyIncludeFileListForeach
209
     * @param        $moduleDirname
210
     * @param        $fileName
211
     * @param        $tableFieldName
212
     *
213
     * @param string $t
214
     * @return string
215
     */
216
    public function getSmartyIncludeFileListForeach($moduleDirname, $fileName, $tableFieldName, $t = '')
217
    {
218
        return "{$t}<{include file='db:{$moduleDirname}_{$fileName}_list.tpl' {$tableFieldName}=\${$tableFieldName}}>\n";
219
    }
220
221
    /**
222
     * @public function getSmartyConditions
223
     * @param string $condition
224
     * @param string $operator
225
     * @param string $type
226
     * @param string $contentIf
227
     * @param mixed  $contentElse
228
     * @param bool   $count
229
     *
230
     * @param bool   $noSimbol
231
     * @param string $t
232
     * @return string
233
     */
234
    public function getSmartyConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $count = false, $noSimbol = false, $t = '')
235
    {
236
        $ret = '';
237
        if (!$contentElse) {
238
            if (!$count) {
239
                $ret = "{$t}<{if \${$condition}{$operator}{$type}}>\n";
240
            } elseif (!$noSimbol) {
241
                $ret = "{$t}<{if {$condition}{$operator}{$type}}>\n";
242
            } else {
243
                $ret = "{$t}<{if count(\${$condition}){$operator}{$type}}>\n";
244
            }
245
            $ret .= "{$t}{$contentIf}";
246
            $ret .= "{$t}<{/if}>\n";
247
        } else {
248
            if (!$count) {
249
                $ret = "{$t}<{if \${$condition}{$operator}{$type}}>\n";
250
            } elseif (!$noSimbol) {
251
                $ret = "{$t}<{if {$condition}{$operator}{$type}}>\n";
252
            } else {
253
                $ret = "{$t}<{if count(\${$condition}){$operator}{$type}}>\n";
254
            }
255
            $ret .= "{$t}{$contentIf}";
256
            $ret .= "{$t}<{else}>\n";
257
            $ret .= "{$t}{$contentElse}";
258
            $ret .= "{$t}<{/if}>\n";
259
        }
260
261
        return $ret;
262
    }
263
264
    /**
265
     * @public function getSmartyForeach
266
     * @param string $item
267
     * @param string $from
268
     * @param string $content
269
     *
270
     * @param string $name
271
     * @param string $key
272
     * @param string $t
273
     * @return string
274
     */
275
    public function getSmartyForeach($item = 'item', $from = 'from', $content = 'content', $name = '', $key = '', $t = '')
276
    {
277
        $name = '' != $name ? " name={$name}" : '';
278
        $key = '' != $key ? " key={$key}" : '';
279
        $ret = "{$t}<{foreach item={$item} from=\${$from}{$key}{$name}}>\n";
280
        $ret .= "{$t}{$content}";
281
        $ret .= "{$t}<{/foreach}>\n";
282
283
        return $ret;
284
    }
285
286
    /**
287
     * @public function getSmartyForeachQuery
288
     * @param string $item
289
     * @param string $from
290
     * @param string $content
291
     *
292
     * @param string $loop
293
     * @param string $key
294
     * @param string $t
295
     * @return string
296
     */
297
    public function getSmartyForeachQuery($item = 'item', $from = 'from', $content = 'content', $loop = 'loop', $key = '', $t = '')
298
    {
299
        $loop = '' != $loop ? " loop={$loop}" : '';
300
        $key = '' != $key ? " key={$key}" : '';
301
        $ret = "{$t}<{foreachq item={$item} from=\${$from}{$key}{$loop}}>\n";
302
        $ret .= "{$t}{$content}";
303
        $ret .= "{$t}<{/foreachq}>\n";
304
305
        return $ret;
306
    }
307
308
    /**
309
     * @public function getSmartySection
310
     * @param string $name
311
     * @param string $loop
312
     * @param string $content
313
     *
314
     * @param int    $start
315
     * @param int    $step
316
     * @param string $t
317
     * @return string
318
     */
319
    public function getSmartySection($name = 'name', $loop = 'loop', $content = 'content', $start = 0, $step = 0, $t = '')
320
    {
321
        $start = 0 != $start ? " start={$start}" : '';
322
        $step = 0 != $step ? " step={$step}" : '';
323
        $ret = "{$t}<{section name={$name} loop=\${$loop}{$start}{$step}}>\n";
324
        $ret .= "{$t}{$content}";
325
        $ret .= "{$t}<{/section}>\n";
326
327
        return $ret;
328
    }
329
}
330