Passed
Push — master ( 79149c...dd37f9 )
by Richard
05:12 queued 11s
created

smarty_function_config_load()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 109
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 82
dl 0
loc 109
rs 0
c 0
b 0
f 0
cc 27
nc 126976
nop 2

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
 * @package Smarty
5
 * @subpackage plugins
6
 */
7
8
/**
9
 * Smarty {config_load} function plugin
10
 *
11
 * Type:     function<br>
12
 * Name:     config_load<br>
13
 * Purpose:  load config file vars
14
 * @link http://smarty.php.net/manual/en/language.function.config.load.php {config_load}
15
 *       (Smarty online manual)
16
 * @author Monte Ohrt <monte at ohrt dot com>
17
 * @author messju mohr <messju at lammfellpuschen dot de> (added use of resources)
18
 * @param array Format:
0 ignored issues
show
Bug introduced by
The type Format was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
 * <pre>
20
 * array('file' => required config file name,
21
 *       'section' => optional config file section to load
22
 *       'scope' => local/parent/global
23
 *       'global' => overrides scope, setting to parent if true)
24
 * </pre>
25
 * @param Smarty
26
 */
27
function smarty_function_config_load($params, &$smarty)
28
{
29
        if ($smarty->debugging) {
30
            $_params = array();
31
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
32
            $_debug_start_time = smarty_core_get_microtime($_params, $smarty);
33
        }
34
35
        $_file = isset($params['file']) ? $smarty->_dequote($params['file']) : null;
36
        $_section = isset($params['section']) ? $smarty->_dequote($params['section']) : null;
37
        $_scope = isset($params['scope']) ? $smarty->_dequote($params['scope']) : 'global';
38
        $_global = isset($params['global']) ? $smarty->_dequote($params['global']) : false;
39
40
        if (!isset($_file) || strlen($_file) == 0) {
41
            $smarty->trigger_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
42
        }
43
44
        if (isset($_scope)) {
45
            if ($_scope != 'local' &&
46
                $_scope != 'parent' &&
47
                $_scope != 'global') {
48
                $smarty->trigger_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
49
            }
50
        } else {
51
            if ($_global) {
52
                $_scope = 'parent';
53
            } else {
54
                $_scope = 'local';
55
            }
56
        }
57
58
        $_params = array('resource_name' => $_file,
59
                         'resource_base_path' => $smarty->config_dir,
60
                         'get_source' => false);
61
        $smarty->_parse_resource_name($_params);
62
        $_file_path = $_params['resource_type'] . ':' . $_params['resource_name'];
0 ignored issues
show
Bug introduced by
Are you sure $_params['resource_type'] of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

62
        $_file_path = /** @scrutinizer ignore-type */ $_params['resource_type'] . ':' . $_params['resource_name'];
Loading history...
63
        if (isset($_section))
64
            $_compile_file = $smarty->_get_compile_path($_file_path.'|'.$_section);
65
        else
66
            $_compile_file = $smarty->_get_compile_path($_file_path);
67
68
        if($smarty->force_compile || !file_exists($_compile_file)) {
69
            $_compile = true;
70
        } elseif ($smarty->compile_check) {
71
            $_params = array('resource_name' => $_file,
72
                             'resource_base_path' => $smarty->config_dir,
73
                             'get_source' => false);
74
            $_compile = $smarty->_fetch_resource_info($_params) &&
75
                $_params['resource_timestamp'] > filemtime($_compile_file);
76
        } else {
77
            $_compile = false;
78
        }
79
80
        if($_compile) {
81
            // compile config file
82
            if(!is_object($smarty->_conf_obj)) {
83
                require_once SMARTY_DIR . $smarty->config_class . '.class.php';
84
                $smarty->_conf_obj = new $smarty->config_class();
85
                $smarty->_conf_obj->overwrite = $smarty->config_overwrite;
86
                $smarty->_conf_obj->booleanize = $smarty->config_booleanize;
87
                $smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
88
                $smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
89
            }
90
91
            $_params = array('resource_name' => $_file,
92
                             'resource_base_path' => $smarty->config_dir,
93
                             $_params['get_source'] = true);
94
            if (!$smarty->_fetch_resource_info($_params)) {
95
                return;
96
            }
97
            $smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
98
            $_config_vars = array_merge($smarty->_conf_obj->get($_file),
99
                    $smarty->_conf_obj->get($_file, $_section));
100
            if(function_exists('var_export')) {
101
                $_output = '<?php $_config_vars = ' . var_export($_config_vars, true) . '; ?>';
102
            } else {
103
                $_output = '<?php $_config_vars = unserialize(\'' . strtr(serialize($_config_vars),array('\''=>'\\\'', '\\'=>'\\\\')) . '\'); ?>';
104
            }
105
            $_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output, 'resource_timestamp' => $_params['resource_timestamp']));
106
            require_once(SMARTY_CORE_DIR . 'core.write_compiled_resource.php');
107
            smarty_core_write_compiled_resource($_params, $smarty);
108
        } else {
109
            include($_compile_file);
110
        }
111
112
        if ($smarty->caching) {
113
            $smarty->_cache_info['config'][$_file] = true;
114
        }
115
116
        $smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_config_vars does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
        $smarty->_config[0]['files'][$_file] = true;
118
119
        if ($_scope == 'parent') {
120
                $smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
121
                $smarty->_config[1]['files'][$_file] = true;
122
        } else if ($_scope == 'global') {
123
            for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
124
                $smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
125
                $smarty->_config[$i]['files'][$_file] = true;
126
            }
127
        }
128
129
        if ($smarty->debugging) {
130
            $_params = array();
131
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
132
            $smarty->_smarty_debug_info[] = array('type'      => 'config',
133
                                                'filename'  => $_file.' ['.$_section.'] '.$_scope,
134
                                                'depth'     => $smarty->_inclusion_depth,
135
                                                'exec_time' => smarty_core_get_microtime($_params, $smarty) - $_debug_start_time);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_debug_start_time does not seem to be defined for all execution paths leading up to this point.
Loading history...
136
        }
137
138
}
139
140
/* vim: set expandtab: */
141
142
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
143