Passed
Push — master ( c69a4e...3c6756 )
by Michael
21:44 queued 13:20
created

Smarty_Internal_TemplateBase::_execute()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 101
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 67
nc 39490
nop 5
dl 0
loc 101
rs 0
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Smarty Internal Plugin Smarty Template  Base
4
 * This file contains the basic shared methods for template handling
5
 *
6
 * @package    Smarty
7
 * @subpackage Template
8
 * @author     Uwe Tews
9
 */
10
11
/**
12
 * Class with shared smarty/template methods
13
 *
14
 * @package    Smarty
15
 * @subpackage Template
16
 *
17
 * @property int $_objType
18
 *
19
 * The following methods will be dynamically loaded by the extension handler when they are called.
20
 * They are located in a corresponding Smarty_Internal_Method_xxxx class
21
 *
22
 * @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
23
 * @method Smarty_Internal_TemplateBase addDefaultModifiers(mixed $modifiers)
24
 * @method Smarty_Internal_TemplateBase addLiterals(mixed $literals)
25
 * @method Smarty_Internal_TemplateBase createData(Smarty_Internal_Data $parent = null, string $name = null)
26
 * @method array getAutoloadFilters(string $type = null)
27
 * @method string getDebugTemplate()
28
 * @method array getDefaultModifier()
29
 * @method array getLiterals()
30
 * @method array getTags(mixed $template = null)
31
 * @method object getRegisteredObject(string $object_name)
32
 * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
33
 * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
34
 * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
35
 * @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
36
 * @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
37
 * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
38
 * @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
39
 * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
40
 * @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers)
41
 * @method Smarty_Internal_TemplateBase setLiterals(mixed $literals)
42
 * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
43
 * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
44
 * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
45
 * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
46
 * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
47
 * @method Smarty_Internal_TemplateBase unregisterResource(string $name)
48
 */
49
abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
50
{
51
    /**
52
     * Set this if you want different sets of cache files for the same
53
     * templates.
54
     *
55
     * @var string
56
     */
57
    public $cache_id = null;
58
59
    /**
60
     * Set this if you want different sets of compiled files for the same
61
     * templates.
62
     *
63
     * @var string
64
     */
65
    public $compile_id = null;
66
67
    /**
68
     * caching enabled
69
     *
70
     * @var int
71
     */
72
    public $caching = Smarty::CACHING_OFF;
73
74
    /**
75
     * check template for modifications?
76
     *
77
     * @var int
78
     */
79
    public $compile_check = Smarty::COMPILECHECK_ON;
80
81
    /**
82
     * cache lifetime in seconds
83
     *
84
     * @var integer
85
     */
86
    public $cache_lifetime = 3600;
87
88
    /**
89
     * Array of source information for known template functions
90
     *
91
     * @var array
92
     */
93
    public $tplFunctions = array();
94
95
    /**
96
     * universal cache
97
     *
98
     * @var array()
99
     */
100
    public $_cache = array();
101
102
    /**
103
     * fetches a rendered Smarty template
104
     *
105
     * @param string $template   the resource handle of the template file or template object
106
     * @param mixed  $cache_id   cache id to be used with this template
107
     * @param mixed  $compile_id compile id to be used with this template
108
     * @param object $parent     next higher level of Smarty variables
109
     *
110
     * @throws Exception
111
     * @throws SmartyException
112
     * @return string rendered template output
113
     */
114
    public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
115
    {
116
        $result = $this->_execute($template, $cache_id, $compile_id, $parent, 0);
117
        return $result === null ? ob_get_clean() : $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result === null ? ob_get_clean() : $result could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
118
    }
119
120
    /**
121
     * displays a Smarty template
122
     *
123
     * @param string $template   the resource handle of the template file or template object
124
     * @param mixed  $cache_id   cache id to be used with this template
125
     * @param mixed  $compile_id compile id to be used with this template
126
     * @param object $parent     next higher level of Smarty variables
127
     *
128
     * @throws \Exception
129
     * @throws \SmartyException
130
     */
131
    public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
132
    {
133
        // display template
134
        $this->_execute($template, $cache_id, $compile_id, $parent, 1);
135
    }
136
137
    /**
138
     * test if cache is valid
139
     *
140
     * @api  Smarty::isCached()
141
     * @link https://www.smarty.net/docs/en/api.is.cached.tpl
142
     *
143
     * @param null|string|\Smarty_Internal_Template $template   the resource handle of the template file or template
144
     *                                                          object
145
     * @param mixed                                 $cache_id   cache id to be used with this template
146
     * @param mixed                                 $compile_id compile id to be used with this template
147
     * @param object                                $parent     next higher level of Smarty variables
148
     *
149
     * @return bool cache status
150
     * @throws \Exception
151
     * @throws \SmartyException
152
     */
153
    public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
154
    {
155
        return $this->_execute($template, $cache_id, $compile_id, $parent, 2);
0 ignored issues
show
Bug introduced by
It seems like $template can also be of type Smarty_Internal_Template; however, parameter $template of Smarty_Internal_TemplateBase::_execute() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

155
        return $this->_execute(/** @scrutinizer ignore-type */ $template, $cache_id, $compile_id, $parent, 2);
Loading history...
156
    }
157
158
    /**
159
     * fetches a rendered Smarty template
160
     *
161
     * @param string $template   the resource handle of the template file or template object
162
     * @param mixed  $cache_id   cache id to be used with this template
163
     * @param mixed  $compile_id compile id to be used with this template
164
     * @param object $parent     next higher level of Smarty variables
165
     * @param string $function   function type 0 = fetch,  1 = display, 2 = isCache
166
     *
167
     * @return mixed
168
     * @throws \Exception
169
     * @throws \SmartyException
170
     */
171
    private function _execute($template, $cache_id, $compile_id, $parent, $function)
172
    {
173
        $smarty = $this->_getSmartyObj();
174
        $saveVars = true;
175
        if ($template === null) {
0 ignored issues
show
introduced by
The condition $template === null is always false.
Loading history...
176
            if (!$this->_isTplObj()) {
177
                throw new SmartyException($function . '():Missing \'$template\' parameter');
178
            } else {
179
                $template = $this;
180
            }
181
        } elseif (is_object($template)) {
0 ignored issues
show
introduced by
The condition is_object($template) is always false.
Loading history...
182
            /* @var Smarty_Internal_Template $template */
183
            if (!isset($template->_objType) || !$template->_isTplObj()) {
184
                throw new SmartyException($function . '():Template object expected');
185
            }
186
        } else {
187
            // get template object
188
            $saveVars = false;
189
            $template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent ? $parent : $this, false);
0 ignored issues
show
introduced by
$parent is of type object, thus it always evaluated to true.
Loading history...
190
            if ($this->_objType === 1) {
191
                // set caching in template object
192
                $template->caching = $this->caching;
193
            }
194
        }
195
        // make sure we have integer values
196
        $template->caching = (int)$template->caching;
197
        // fetch template content
198
        $level = ob_get_level();
199
        try {
200
            $_smarty_old_error_level =
201
                isset($smarty->error_reporting) ? error_reporting($smarty->error_reporting) : null;
202
203
            if ($smarty->isMutingUndefinedOrNullWarnings()) {
204
                $errorHandler = new Smarty_Internal_ErrorHandler();
205
                $errorHandler->activate();
206
            }
207
208
            if ($this->_objType === 2) {
209
                /* @var Smarty_Internal_Template $this */
210
                $template->tplFunctions = $this->tplFunctions;
211
                $template->inheritance = $this->inheritance;
212
            }
213
            /* @var Smarty_Internal_Template $parent */
214
            if (isset($parent->_objType) && ($parent->_objType === 2) && !empty($parent->tplFunctions)) {
215
                $template->tplFunctions = array_merge($parent->tplFunctions, $template->tplFunctions);
216
            }
217
            if ($function === 2) {
0 ignored issues
show
introduced by
The condition $function === 2 is always false.
Loading history...
218
                if ($template->caching) {
219
                    // return cache status of template
220
                    if (!isset($template->cached)) {
221
                        $template->loadCached();
222
                    }
223
                    $result = $template->cached->isCached($template);
224
                    Smarty_Internal_Template::$isCacheTplObj[ $template->_getTemplateId() ] = $template;
225
                } else {
226
                    return false;
227
                }
228
            } else {
229
                if ($saveVars) {
0 ignored issues
show
introduced by
The condition $saveVars is always false.
Loading history...
230
                    $savedTplVars = $template->tpl_vars;
231
                    $savedConfigVars = $template->config_vars;
232
                }
233
                ob_start();
234
                $template->_mergeVars();
235
                if (!empty(Smarty::$global_tpl_vars)) {
236
                    $template->tpl_vars = array_merge(Smarty::$global_tpl_vars, $template->tpl_vars);
237
                }
238
                $result = $template->render(false, $function);
0 ignored issues
show
Bug introduced by
$function of type string is incompatible with the type boolean|null expected by parameter $display of Smarty_Internal_Template::render(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

238
                $result = $template->render(false, /** @scrutinizer ignore-type */ $function);
Loading history...
239
                $template->_cleanUp();
240
                if ($saveVars) {
0 ignored issues
show
introduced by
The condition $saveVars is always false.
Loading history...
241
                    $template->tpl_vars = $savedTplVars;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $savedTplVars does not seem to be defined for all execution paths leading up to this point.
Loading history...
242
                    $template->config_vars = $savedConfigVars;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $savedConfigVars does not seem to be defined for all execution paths leading up to this point.
Loading history...
243
                } else {
244
                    if (!$function && !isset(Smarty_Internal_Template::$tplObjCache[ $template->templateId ])) {
245
                        $template->parent = null;
246
                        $template->tpl_vars = $template->config_vars = array();
247
                        Smarty_Internal_Template::$tplObjCache[ $template->templateId ] = $template;
248
                    }
249
                }
250
            }
251
252
            if (isset($errorHandler)) {
253
                $errorHandler->deactivate();
254
            }
255
256
            if (isset($_smarty_old_error_level)) {
257
                error_reporting($_smarty_old_error_level);
258
            }
259
            return $result;
260
        } catch (Throwable $e) {
261
            while (ob_get_level() > $level) {
262
                ob_end_clean();
263
            }
264
            if (isset($errorHandler)) {
265
                $errorHandler->deactivate();
266
            }
267
268
            if (isset($_smarty_old_error_level)) {
269
                error_reporting($_smarty_old_error_level);
270
            }
271
            throw $e;
272
        }
273
    }
274
275
    /**
276
     * Registers plugin to be used in templates
277
     *
278
     * @api  Smarty::registerPlugin()
279
     * @link https://www.smarty.net/docs/en/api.register.plugin.tpl
280
     *
281
     * @param string   $type       plugin type
282
     * @param string   $name       name of template tag
283
     * @param callable $callback   PHP callback to register
284
     * @param bool     $cacheable  if true (default) this function is cache able
285
     * @param mixed    $cache_attr caching attributes if any
286
     *
287
     * @return \Smarty|\Smarty_Internal_Template
288
     * @throws \SmartyException
289
     */
290
    public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null)
291
    {
292
        return $this->ext->registerPlugin->registerPlugin($this, $type, $name, $callback, $cacheable, $cache_attr);
293
    }
294
295
    /**
296
     * load a filter of specified type and name
297
     *
298
     * @api  Smarty::loadFilter()
299
     * @link https://www.smarty.net/docs/en/api.load.filter.tpl
300
     *
301
     * @param string $type filter type
302
     * @param string $name filter name
303
     *
304
     * @return bool
305
     * @throws \SmartyException
306
     */
307
    public function loadFilter($type, $name)
308
    {
309
        return $this->ext->loadFilter->loadFilter($this, $type, $name);
310
    }
311
312
    /**
313
     * Registers a filter function
314
     *
315
     * @api  Smarty::registerFilter()
316
     * @link https://www.smarty.net/docs/en/api.register.filter.tpl
317
     *
318
     * @param string      $type filter type
319
     * @param callable    $callback
320
     * @param string|null $name optional filter name
321
     *
322
     * @return \Smarty|\Smarty_Internal_Template
323
     * @throws \SmartyException
324
     */
325
    public function registerFilter($type, $callback, $name = null)
326
    {
327
        return $this->ext->registerFilter->registerFilter($this, $type, $callback, $name);
328
    }
329
330
    /**
331
     * Registers object to be used in templates
332
     *
333
     * @api  Smarty::registerObject()
334
     * @link https://www.smarty.net/docs/en/api.register.object.tpl
335
     *
336
     * @param string $object_name
337
     * @param object $object                     the referenced PHP object to register
338
     * @param array  $allowed_methods_properties list of allowed methods (empty = all)
339
     * @param bool   $format                     smarty argument format, else traditional
340
     * @param array  $block_methods              list of block-methods
341
     *
342
     * @return \Smarty|\Smarty_Internal_Template
343
     * @throws \SmartyException
344
     */
345
    public function registerObject(
346
        $object_name,
347
        $object,
348
        $allowed_methods_properties = array(),
349
        $format = true,
350
        $block_methods = array()
351
    ) {
352
        return $this->ext->registerObject->registerObject(
353
            $this,
354
            $object_name,
355
            $object,
356
            $allowed_methods_properties,
357
            $format,
358
            $block_methods
359
        );
360
    }
361
362
    /**
363
     * @param int $compile_check
364
     */
365
    public function setCompileCheck($compile_check)
366
    {
367
        $this->compile_check = (int)$compile_check;
368
    }
369
370
    /**
371
     * @param int $caching
372
     */
373
    public function setCaching($caching)
374
    {
375
        $this->caching = (int)$caching;
376
    }
377
378
    /**
379
     * @param int $cache_lifetime
380
     */
381
    public function setCacheLifetime($cache_lifetime)
382
    {
383
        $this->cache_lifetime = $cache_lifetime;
384
    }
385
386
    /**
387
     * @param string $compile_id
388
     */
389
    public function setCompileId($compile_id)
390
    {
391
        $this->compile_id = $compile_id;
392
    }
393
394
    /**
395
     * @param string $cache_id
396
     */
397
    public function setCacheId($cache_id)
398
    {
399
        $this->cache_id = $cache_id;
400
    }
401
}
402