Passed
Push — master ( 975da5...45a1ec )
by Richard
04:42 queued 11s
created

smarty_resource_db_tplinfo()   C

Complexity

Conditions 15
Paths 103

Size

Total Lines 64
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 43
c 1
b 0
f 0
nc 103
nop 1
dl 0
loc 64
rs 5.8916

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 plugin
4
 *
5
 * Fetches templates from a database
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
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright       (c) 2000-2021 XOOPS Project (https://xoops.org)
15
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
16
 * @param $tpl_name
17
 * @param $tpl_source
18
 * @param $smarty
19
 * @return bool
20
 */
21
function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty)
0 ignored issues
show
Unused Code introduced by
The parameter $smarty is not used and could be removed. ( Ignorable by Annotation )

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

21
function smarty_resource_db_source($tpl_name, &$tpl_source, /** @scrutinizer ignore-unused */ &$smarty)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
{
23
    if (!$tpl = smarty_resource_db_tplinfo($tpl_name)) {
24
        return false;
25
    }
26
    if (is_object($tpl)) {
0 ignored issues
show
introduced by
The condition is_object($tpl) is always false.
Loading history...
27
        $tpl_source = $tpl->getVar('tpl_source', 'n');
28
    } else {
29
        $fp         = fopen($tpl, 'r');
0 ignored issues
show
Bug introduced by
It seems like $tpl can also be of type true; however, parameter $filename of fopen() 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

29
        $fp         = fopen(/** @scrutinizer ignore-type */ $tpl, 'r');
Loading history...
30
        $filesize   = filesize($tpl);
0 ignored issues
show
Bug introduced by
It seems like $tpl can also be of type true; however, parameter $filename of filesize() 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

30
        $filesize   = filesize(/** @scrutinizer ignore-type */ $tpl);
Loading history...
31
        $tpl_source = ($filesize > 0) ? fread($fp, $filesize) : '';
32
        fclose($fp);
33
    }
34
35
    return true;
36
}
37
38
/**
39
 * @param $tpl_name
40
 * @param $tpl_timestamp
41
 * @param $smarty
42
 *
43
 * @return bool
44
 */
45
function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
0 ignored issues
show
Unused Code introduced by
The parameter $smarty is not used and could be removed. ( Ignorable by Annotation )

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

45
function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, /** @scrutinizer ignore-unused */ &$smarty)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
{
47
    if (!$tpl = smarty_resource_db_tplinfo($tpl_name)) {
48
        return false;
49
    }
50
    if (is_object($tpl)) {
0 ignored issues
show
introduced by
The condition is_object($tpl) is always false.
Loading history...
51
        $tpl_timestamp = $tpl->getVar('tpl_lastmodified', 'n');
52
    } else {
53
        $tpl_timestamp = filemtime($tpl);
0 ignored issues
show
Bug introduced by
It seems like $tpl can also be of type true; however, parameter $filename of filemtime() 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

53
        $tpl_timestamp = filemtime(/** @scrutinizer ignore-type */ $tpl);
Loading history...
54
    }
55
56
    return true;
57
}
58
59
/**
60
 * @param $tpl_name
61
 * @param $smarty
62
 *
63
 * @return bool
64
 */
65
function smarty_resource_db_secure($tpl_name, &$smarty)
0 ignored issues
show
Unused Code introduced by
The parameter $smarty is not used and could be removed. ( Ignorable by Annotation )

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

65
function smarty_resource_db_secure($tpl_name, /** @scrutinizer ignore-unused */ &$smarty)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tpl_name is not used and could be removed. ( Ignorable by Annotation )

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

65
function smarty_resource_db_secure(/** @scrutinizer ignore-unused */ $tpl_name, &$smarty)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
{
67
    // assume all templates are secure
68
    return true;
69
}
70
71
/**
72
 * @param $tpl_name
73
 * @param $smarty
74
 */
75
function smarty_resource_db_trusted($tpl_name, &$smarty)
0 ignored issues
show
Unused Code introduced by
The parameter $smarty is not used and could be removed. ( Ignorable by Annotation )

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

75
function smarty_resource_db_trusted($tpl_name, /** @scrutinizer ignore-unused */ &$smarty)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tpl_name is not used and could be removed. ( Ignorable by Annotation )

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

75
function smarty_resource_db_trusted(/** @scrutinizer ignore-unused */ $tpl_name, &$smarty)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
{
77
    // not used for templates
78
}
79
80
/**
81
 * @param $tpl_name
82
 *
83
 * @return bool|string
84
 */
85
function smarty_resource_db_tplinfo($tpl_name)
86
{
87
    static $cache = array();
88
    global $xoopsConfig;
89
90
    if (isset($cache[$tpl_name])) {
91
        return $cache[$tpl_name];
92
    }
93
    $tplset          = $xoopsConfig['template_set'];
94
    $theme           = isset($xoopsConfig['theme_set']) ? $xoopsConfig['theme_set'] : 'default';
95
    /** @var \XoopsTplfileHandler $tplfile_handler */
96
    $tplfile_handler = xoops_getHandler('tplfile');
97
    // If we're not using the "default" template set, then get the templates from the DB
98
    if ($tplset !== 'default') {
99
        $tplobj = $tplfile_handler->find($tplset, null, null, null, $tpl_name, true);
100
        if (count($tplobj)) {
101
            return $cache[$tpl_name] = $tplobj[0];
102
        }
103
    }
104
    // If we're using the default tplset, get the template from the filesystem
105
    $tplobj = $tplfile_handler->find('default', null, null, null, $tpl_name, true);
106
107
    if (!count($tplobj)) {
108
        return $cache[$tpl_name] = false;
109
    }
110
    $tplobj = $tplobj[0];
111
    $module = $tplobj->getVar('tpl_module', 'n');
112
    $type   = $tplobj->getVar('tpl_type', 'n');
113
    // Construct template path
114
    switch ($type) {
115
        case 'block':
116
            $directory = XOOPS_THEME_PATH;
117
            $path      = 'blocks/';
118
            if (class_exists('XoopsSystemCpanel', false)) {
119
                $directory = XOOPS_ADMINTHEME_PATH;
120
                $theme     = isset($xoopsConfig['cpanel']) ? $xoopsConfig['cpanel'] : 'default';
121
            }
122
            break;
123
        case 'admin':
124
            $theme     = isset($xoopsConfig['cpanel']) ? $xoopsConfig['cpanel'] : 'default';
125
            $directory = XOOPS_ADMINTHEME_PATH;
126
            $path      = 'admin/';
127
            break;
128
        default:
129
            // at time of this comment, this only includes type 'module'
130
            $directory = XOOPS_THEME_PATH;
131
            $path      = '';
132
            if (class_exists('XoopsSystemCpanel', false)) {
133
                $directory = XOOPS_ADMINTHEME_PATH;
134
                $theme     = isset($xoopsConfig['cpanel']) ? $xoopsConfig['cpanel'] : 'default';
135
            }
136
            break;
137
    }
138
    // First, check for an overloaded version within the theme folder
139
    $filepath = $directory . "/{$theme}/modules/{$module}/{$path}{$tpl_name}";
140
    if (!file_exists($filepath)) {
141
        // If no custom version exists, get the tpl from its default location
142
        $filepath = XOOPS_ROOT_PATH . "/modules/{$module}/templates/{$path}{$tpl_name}";
143
        if (!file_exists($filepath)) {
144
            return $cache[$tpl_name] = $tplobj;
145
        }
146
    }
147
148
    return $cache[$tpl_name] = $filepath;
149
}
150