Completed
Pull Request — master (#86)
by Gino
03:42
created

TDMCreateSmartyCode::getSmartyIncludeFile()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 7.756
cc 9
eloc 10
nc 5
nop 4
1
<?php
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
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: TDMCreateSmartyCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TDMCreateSmartyCode.
27
 */
28
class TDMCreateSmartyCode
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
    }
40
41
    /*
42
    *  @static function &getInstance
43
    *  @param null
44
    */
45
    /**
46
     * @return TDMCreateSmartySmartyCodes
47
     */
48
    public static function &getInstance()
49
    {
50
        static $instance = false;
51
        if (!$instance) {
52
            $instance = new self();
53
        }
54
55
        return $instance;
56
    }
57
58
    /**
59
     * @public function getSmartyTag
60
     *
61
     * @param $tag
62
     * @param $attributes
63
     * @param $content
64
     *
65
     * @return string
66
     */
67
    public function getSmartyTag($tag = '', $attributes = array(), $content = '')
68
    {
69
        if (empty($attributes)) {
70
            $attributes = array();
71
        }
72
        $attr = $this->getAttributes($attributes);
73
        $ret = "<{{$tag}{$attr}}>{$content}<{/{$tag}}>\n";
74
75
        return $ret;
76
    }
77
78
     /*
79
    *  @private function setAttributes
80
    *  @param array $attributes
81
    */
82
    /**
83
     * @param  $attributes
84
     *
85
     * @return string
86
     */
87
    private function getAttributes($attributes)
88
    {
89
        $str = '';
90
        foreach ($attributes as $name => $value) {
91
            if ($name != '_') {
92
                $str .= ' '.$name.'="'.$value.'"';
93
            }
94
        }
95
96
        return $str;
97
    }
98
99
    /*
100
    *  @public function getSmartyEmpty
101
    *  @param string $empty
102
    */
103
    /**
104
     * @param $empty
105
     *
106
     * @return string
107
     */
108
    public function getSmartyEmpty($empty = '')
109
    {
110
        return "{$empty}";
111
    }
112
113
    /*
114
    *  @public function getSmartyComment
115
    *  @param string $htmlComment
116
    */
117
    /**
118
     * @param $htmlComment
119
     *
120
     * @return string
121
     */
122
    public function getSmartyComment($smartyComment = '')
123
    {
124
        return "<{* {$smartyComment} *}>";
125
    }
126
127
    /*
128
    *  @public function getSmartyNoSimbol
129
    *  @param string $content
130
    */
131
    /**
132
     * @param $content
133
     *
134
     * @return string
135
     */
136
    public function getSmartyNoSimbol($noSimbol = '')
137
    {
138
        return "<{{$noSimbol}}>";
139
    }
140
141
    /*
142
    *  @public function getSmartyConst
143
    *  @param string $language
144
    *  @param mixed $const
145
    */
146
    /**
147
     * @param $language
148
     * @param $const
149
     *
150
     * @return string
151
     */
152
    public function getSmartyConst($language, $const)
153
    {
154
        return "<{\$smarty.const.{$language}{$const}}>";
155
    }
156
157
    /*
158
    *  @public function getSmartySingleVar
159
    *  @param string $var
160
    */
161
    /**
162
     * @param string $var
163
     *
164
     * @return string
165
     */
166
    public function getSmartySingleVar($var)
167
    {
168
        return "<{\${$var}}>";
169
    }
170
171
    /*
172
    *  @public function getSmartyDoubleVar
173
    *  @param string $leftVar
174
    *  @param string $rightVar
175
    */
176
    /**
177
     * @param string $leftVar
178
     * @param string $rightVar
179
     *
180
     * @return string
181
     */
182
    public function getSmartyDoubleVar($leftVar, $rightVar)
183
    {
184
        return "<{\${$leftVar}.{$rightVar}}>";
185
    }
186
187
    /*
188
    *  @public function getSmartyIncludeFile
189
    *  @param string $name
190
    */
191
    /**
192
     * @param $moduleDirname
193
     * @param $fileName
194
     * @param $admin
195
     *
196
     * @return string
197
     */
198
    public function getSmartyIncludeFile($moduleDirname, $fileName = 'header', $admin = false, $q = false)
199
    {
200
        if (!$admin && !$q) {
201
            $ret = "<{include file='db:{$moduleDirname}_{$fileName}.tpl'}>\n";
202
        } elseif ($admin && !$q) {
203
            $ret = "<{include file='db:{$moduleDirname}_admin_{$fileName}.tpl'}>\n";
204
        } elseif (!$admin && $q) {
205
            $ret = "<{includeq file='db:{$moduleDirname}_{$fileName}.tpl'}>\n";
206
        } elseif ($admin && $q) {
207
            $ret = "<{includeq file='db:{$moduleDirname}_admin_{$fileName}.tpl'}>\n";
208
        }
209
210
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
211
    }
212
213
    /*
214
    *  @public function getSmartyIncludeFileListSection
215
    *  @param string $name
216
    */
217
    /**
218
     * @param $moduleDirname
219
     * @param $fileName
220
     * @param $tableFieldName
221
     *
222
     * @return string
223
     */
224
    public function getSmartyIncludeFileListSection($moduleDirname, $fileName, $tableFieldName)
225
    {
226
        return "<{include file='db:{$moduleDirname}_{$fileName}_list.tpl' {$tableFieldName}=\${$tableFieldName}[i]}>";
227
    }
228
229
    /*
230
    *  @public function getSmartyIncludeFileListForeach
231
    *  @param string $name
232
    */
233
    /**
234
     * @param $moduleDirname
235
     * @param $fileName
236
     * @param $tableFieldName
237
     *
238
     * @return string
239
     */
240
    public function getSmartyIncludeFileListForeach($moduleDirname, $fileName, $tableFieldName)
241
    {
242
        return "<{include file='db:{$moduleDirname}_{$fileName}_list.tpl' {$tableFieldName}=\${$tableFieldName}}>";
243
    }
244
245
    /*
246
    *  @public function getSmartyConditions
247
    *  @param string $condition
248
    *  @param string $operator
249
    *  @param string $type
250
    *  @param string $contentIf
251
    *  @param mixed  $contentElse
252
    *  @param bool   $count
253
    */
254
    /**
255
     * @param string $condition
256
     * @param string $operator
257
     * @param string $type
258
     * @param string $contentIf
259
     * @param mixed  $contentElse
260
     * @param bool   $count
261
     *
262
     * @return string
263
     */
264
    public function getSmartyConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $count = false, $noSimbol = false)
265
    {
266
        if (!$contentElse) {
267
            if (!$count) {
268
                $ret = "<{if \${$condition}{$operator}{$type}}>\n";
269
            } elseif (!$noSimbol) {
270
                $ret = "<{if {$condition}{$operator}{$type}}>\n";
271
            } else {
272
                $ret = "<{if count(\${$condition}){$operator}{$type}}>\n";
273
            }
274
            $ret .= "\t{$contentIf}\n";
275
            $ret .= "<{/if}>\n";
276
        } else {
277
            if (!$count) {
278
                $ret = "<{if \${$condition}{$operator}{$type}}>\n";
279
            } elseif (!$noSimbol) {
280
                $ret = "<{if {$condition}{$operator}{$type}}>\n";
281
            } else {
282
                $ret = "<{if count(\${$condition}){$operator}{$type}}>\n";
283
            }
284
            $ret .= "\t{$contentIf}\n";
285
            $ret .= "<{else}>\n";
286
            $ret .= "\t{$contentElse}\n";
287
            $ret .= "<{/if}>\n";
288
        }
289
290
        return $ret;
291
    }
292
293
    /*
294
    *  @public function getSmartyForeach
295
    *  @param string $item
296
    *  @param string $from
297
    *  @param string $content
298
    */
299
    /**
300
     * @param string $item
301
     * @param string $from
302
     * @param string $content
303
     *
304
     * @return string
305
     */
306
    public function getSmartyForeach($item = 'item', $from = 'from', $content = 'content', $name = '', $key = '')
307
    {
308
        $name = $name != '' ? " name={$name}" : '';
309
        $key = $key != '' ? " key={$key}" : '';
310
        $ret = "<{foreach item={$item} from=\${$from}{$key}{$name}}>\n";
311
        $ret .= "\t{$content}\n";
312
        $ret .= "<{/foreach}>\n";
313
314
        return $ret;
315
    }
316
317
    /*
318
    *  @public function getSmartyForeachQuery
319
    *  @param string $item
320
    *  @param string $from
321
    *  @param string $content
322
    */
323
    /**
324
     * @param string $item
325
     * @param string $from
326
     * @param string $content
327
     *
328
     * @return string
329
     */
330
    public function getSmartyForeachQuery($item = 'item', $from = 'from', $content = 'content', $loop = 'loop', $key = '')
331
    {
332
        $loop = $loop != '' ? " loop={$loop}" : '';
333
        $key = $key != '' ? " key={$key}" : '';
334
        $ret = "<{foreachq item={$item} from=\${$from}{$key}{$loop}}>\n";
335
        $ret .= "\t{$content}\n";
336
        $ret .= "<{/foreachq}>\n";
337
338
        return $ret;
339
    }
340
341
    /*
342
    *  @public function getSmartySection
343
    *  @param string $name
344
    *  @param string $loop
345
    *  @param string $content
346
    */
347
    /**
348
     * @param string $name
349
     * @param string $loop
350
     * @param string $content
351
     *
352
     * @return string
353
     */
354
    public function getSmartySection($name = 'name', $loop = 'loop', $content = 'content', $start = 0, $step = 0)
355
    {
356
        $start = $start != 0 ? " start={$start}" : '';
357
        $step = $step != 0 ? " step={$step}" : '';
358
        $ret = "<{section name={$name} loop=\${$loop}{$start}{$step}}>\n";
359
        $ret .= "\t{$content}\n";
360
        $ret .= "<{/section}>\n";
361
362
        return $ret;
363
    }
364
}
365