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

Smarty_Internal_Runtime_UpdateScope::updateScope()   D

Complexity

Conditions 25
Paths 520

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 37
nc 520
nop 3
dl 0
loc 54
rs 4.3093
c 0
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
/**
4
 * Runtime Extension updateScope
5
 *
6
 * @package    Smarty
7
 * @subpackage PluginsInternal
8
 * @author     Uwe Tews
9
 *
10
 **/
11
class Smarty_Internal_Runtime_UpdateScope
12
{
13
14
    /**
15
     * Update new assigned template or config variable in other effected scopes
16
     *
17
     * @param Smarty_Internal_Template $tpl     data object
18
     * @param string|null              $varName variable name
19
     * @param int                      $tagScope   tag scope to which bubble up variable value
20
     *
21
     */
22
    public function _updateScope(Smarty_Internal_Template $tpl, $varName, $tagScope = 0)
23
    {
24
        if ($tagScope) {
25
            $this->_updateVarStack($tpl, $varName);
26
            $tagScope = $tagScope & ~Smarty::SCOPE_LOCAL;
27
            if (!$tpl->scope && !$tagScope) return;
28
        }
29
        $mergedScope = $tagScope | $tpl->scope;
30
        if ($mergedScope) {
31
            if ($mergedScope & Smarty::SCOPE_GLOBAL && $varName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $varName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
32
                Smarty::$global_tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
33
            }
34
            // update scopes
35
            foreach ($this->_getAffectedScopes($tpl, $mergedScope) as $ptr) {
36
                $this->_updateVariableInOtherScope($ptr->tpl_vars, $tpl, $varName);
37
                if($tagScope && $ptr->_isTplObj() && isset($tpl->_cache[ 'varStack' ])) {
38
                    $this->_updateVarStack($ptr, $varName);              }
39
            }
40
        }
41
    }
42
43
    /**
44
     * Get array of objects which needs to be updated  by given scope value
45
     *
46
     * @param Smarty_Internal_Template $tpl
47
     * @param int                      $mergedScope merged tag and template scope to which bubble up variable value
48
     *
49
     * @return array
50
     */
51
    public function _getAffectedScopes(Smarty_Internal_Template $tpl, $mergedScope)
52
    {
53
        $_stack = array();
54
        $ptr = $tpl->parent;
55
        if ($mergedScope && isset($ptr) && $ptr->_isTplObj()) {
56
            $_stack[] = $ptr;
57
            $mergedScope = $mergedScope & ~Smarty::SCOPE_PARENT;
58
            if (!$mergedScope) {
59
                // only parent was set, we are done
60
                return $_stack;
61
            }
62
            $ptr = $ptr->parent;
63
        }
64
        while (isset($ptr) && $ptr->_isTplObj()) {
65
                $_stack[] = $ptr;
66
             $ptr = $ptr->parent;
67
        }
68
        if ($mergedScope & Smarty::SCOPE_SMARTY) {
69
            if (isset($tpl->smarty)) {
70
                $_stack[] = $tpl->smarty;
71
            }
72
        } elseif ($mergedScope & Smarty::SCOPE_ROOT) {
73
            while (isset($ptr)) {
74
                if (!$ptr->_isTplObj()) {
75
                    $_stack[] = $ptr;
76
                    break;
77
                }
78
                $ptr = $ptr->parent;
79
            }
80
        }
81
        return $_stack;
82
    }
83
84
    /**
85
     * Update varibale in other scope
86
     *
87
     * @param array     $tpl_vars template variable array
88
     * @param \Smarty_Internal_Template $from
89
     * @param string               $varName variable name
90
     */
91
    public function _updateVariableInOtherScope(&$tpl_vars, Smarty_Internal_Template $from, $varName)
92
    {
93
        if (!isset($tpl_vars[ $varName ])) {
94
            $tpl_vars[ $varName ] = clone $from->tpl_vars[ $varName ];
95
        } else {
96
            $tpl_vars[ $varName ] = clone $tpl_vars[ $varName ];
97
            $tpl_vars[ $varName ]->value = $from->tpl_vars[ $varName ]->value;
98
        }
99
    }
100
101
    /**
102
     * Update variable in template local variable stack
103
     *
104
     * @param \Smarty_Internal_Template $tpl
105
     * @param string|null               $varName variable name or null for config variables
106
     */
107
    public function _updateVarStack(Smarty_Internal_Template $tpl, $varName)
108
    {
109
        $i = 0;
110
        while (isset($tpl->_cache[ 'varStack' ][ $i ])) {
111
            $this->_updateVariableInOtherScope($tpl->_cache[ 'varStack' ][ $i ][ 'tpl' ], $tpl, $varName);
112
            $i ++;
113
        }
114
    }
115
}
116