Completed
Branch master (e379bd)
by Pierre-Henry
33:06
created

_loadConfigVars()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 16
nc 12
nop 2
dl 0
loc 26
rs 5.1612
c 0
b 0
f 0

How to fix   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
/**
4
 * Smarty Method ConfigLoad
5
 *
6
 * Smarty::configLoad() method
7
 *
8
 * @package    Smarty
9
 * @subpackage PluginsInternal
10
 * @author     Uwe Tews
11
 */
12
class Smarty_Internal_Method_ConfigLoad
13
{
14
    /**
15
     * Valid for all objects
16
     *
17
     * @var int
18
     */
19
    public $objMap = 7;
20
21
    /**
22
     * load a config file, optionally load just selected sections
23
     *
24
     * @api  Smarty::configLoad()
25
     * @link http://www.smarty.net/docs/en/api.config.load.tpl
26
     *
27
     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
28
     * @param  string                                                 $config_file filename
29
     * @param  mixed                                                  $sections    array of section names, single
30
     *                                                                             section or null
31
     *
32
     * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
33
     * @throws \SmartyException
34
     */
35
    public function configLoad(Smarty_Internal_Data $data, $config_file, $sections = null)
36
    {
37
        $this->_loadConfigFile($data, $config_file, $sections, null);
38
        return $data;
39
    }
40
41
    /**
42
     * load a config file, optionally load just selected sections
43
     *
44
     * @api  Smarty::configLoad()
45
     * @link http://www.smarty.net/docs/en/api.config.load.tpl
46
     *
47
     * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
48
     * @param  string                                                 $config_file filename
49
     * @param  mixed                                                  $sections    array of section names, single
50
     *                                                                             section or null
51
     * @param int                                                     $scope       scope into which config variables
52
     *                                                                             shall be loaded
53
     *
54
     * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
55
     * @throws \SmartyException
56
     */
57
    public function _loadConfigFile(Smarty_Internal_Data $data, $config_file, $sections = null, $scope = 0)
58
    {
59
        /* @var \Smarty $smarty */
60
        $smarty = $data->_getSmartyObj();
61
        /* @var \Smarty_Internal_Template $confObj */
62
        $confObj = new Smarty_Internal_Template($config_file, $smarty, $data, null, null, null, null, true);
63
        $confObj->caching = Smarty::CACHING_OFF;
64
        $confObj->source->config_sections = $sections;
0 ignored issues
show
Bug introduced by
The property config_sections does not seem to exist in Smarty_Template_Source.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
        $confObj->source->scope = $scope;
0 ignored issues
show
Bug introduced by
The property scope does not seem to exist in Smarty_Template_Source.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
66
        $confObj->compiled = Smarty_Template_Compiled::load($confObj);
67
        $confObj->compiled->render($confObj);
68
        if ($data->_isTplObj()) {
69
            $data->compiled->file_dependency[ $confObj->source->uid ] =
0 ignored issues
show
Bug introduced by
The property compiled does not seem to exist in Smarty_Internal_Data.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
                array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
71
        }
72
    }
73
74
    /**
75
     * load config variables into template object
76
     *
77
     * @param \Smarty_Internal_Template $tpl
78
     * @param  array                    $new_config_vars
79
     *
80
     */
81
    public function _loadConfigVars(Smarty_Internal_Template $tpl, $new_config_vars)
82
    {
83
        $this->_assignConfigVars($tpl->parent->config_vars, $tpl, $new_config_vars);
84
        $tagScope = $tpl->source->scope;
0 ignored issues
show
Bug introduced by
The property scope does not seem to exist in Smarty_Template_Source.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
        if ($tagScope >= 0) {
86
            if ($tagScope == Smarty::SCOPE_LOCAL) {
87
                $this->_updateVarStack($tpl, $new_config_vars);
88
                $tagScope = 0;
89
                if (!$tpl->scope) {
90
                    return;
91
                }
92
            }
93
            if ($tpl->parent->_isTplObj() && ($tagScope || $tpl->parent->scope)) {
94
                $mergedScope = $tagScope | $tpl->scope;
95
                if ($mergedScope) {
96
                    // update scopes
97
                    foreach ($tpl->smarty->ext->_updateScope->_getAffectedScopes($tpl->parent, $mergedScope) as $ptr) {
0 ignored issues
show
Compatibility introduced by
$tpl->parent of type object<Smarty_Internal_Data> is not a sub-type of object<Smarty_Internal_Template>. It seems like you assume a child class of the class Smarty_Internal_Data to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
98
                        $this->_assignConfigVars($ptr->config_vars, $tpl, $new_config_vars);
99
                        if ($tagScope && $ptr->_isTplObj() && isset($tpl->_cache[ 'varStack' ])) {
100
                            $this->_updateVarStack($tpl, $new_config_vars);
101
                        }
102
                    }
103
                }
104
            }
105
        }
106
    }
107
108
    /**
109
     * Assign all config variables in given scope
110
     *
111
     * @param array                     $config_vars     config variables in scope
112
     * @param \Smarty_Internal_Template $tpl
113
     * @param  array                    $new_config_vars loaded config variables
114
     */
115
    public function _assignConfigVars(&$config_vars, Smarty_Internal_Template $tpl, $new_config_vars)
116
    {
117
        // copy global config vars
118
        foreach ($new_config_vars[ 'vars' ] as $variable => $value) {
119
            if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
120
                $config_vars[ $variable ] = $value;
121
            } else {
122
                $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
123
            }
124
        }
125
        // scan sections
126
        $sections = $tpl->source->config_sections;
0 ignored issues
show
Bug introduced by
The property config_sections does not seem to exist in Smarty_Template_Source.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
127
        if (!empty($sections)) {
128
            foreach ((array) $sections as $tpl_section) {
129
                if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) {
130
                    foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) {
131
                        if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
132
                            $config_vars[ $variable ] = $value;
133
                        } else {
134
                            $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
135
                        }
136
                    }
137
                }
138
            }
139
        }
140
    }
141
142
    /**
143
     * Update config variables in template local variable stack
144
     *
145
     * @param \Smarty_Internal_Template $tpl
146
     * @param array                     $config_vars
147
     */
148
    public function _updateVarStack(Smarty_Internal_Template $tpl, $config_vars)
149
    {
150
        $i = 0;
151
        while (isset($tpl->_cache[ 'varStack' ][ $i ])) {
152
            $this->_assignConfigVars($tpl->_cache[ 'varStack' ][ $i ][ 'config' ], $tpl, $config_vars);
153
            $i ++;
154
        }
155
    }
156
157
    /**
158
     * gets  a config variable value
159
     *
160
     * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
161
     * @param string                                                  $varName the name of the config variable
162
     * @param bool                                                    $errorEnable
163
     *
164
     * @return null|string  the value of the config variable
165
     */
166
    public function _getConfigVariable(Smarty_Internal_Data $data, $varName, $errorEnable = true)
167
    {
168
        $_ptr = $data;
169
        while ($_ptr !== null) {
170
            if (isset($_ptr->config_vars[ $varName ])) {
171
                // found it, return it
172
                return $_ptr->config_vars[ $varName ];
173
            }
174
            // not found, try at parent
175
            $_ptr = $_ptr->parent;
176
        }
177
        if ($data->smarty->error_unassigned && $errorEnable) {
178
            // force a notice
179
            $x = $$varName;
0 ignored issues
show
Unused Code introduced by
$x is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
180
        }
181
        return null;
182
    }
183
}
184