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

Smarty_Internal_Compile_Break::checkLevels()   D

Complexity

Conditions 12
Paths 120

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 29
nc 120
nop 3
dl 0
loc 43
rs 4.8484
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
 * Smarty Internal Plugin Compile Break
4
 * Compiles the {break} tag
5
 *
6
 * @package    Smarty
7
 * @subpackage Compiler
8
 * @author     Uwe Tews
9
 */
10
11
/**
12
 * Smarty Internal Plugin Compile Break Class
13
 *
14
 * @package    Smarty
15
 * @subpackage Compiler
16
 */
17
class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase
18
{
19
    /**
20
     * Attribute definition: Overwrites base class.
21
     *
22
     * @var array
23
     * @see Smarty_Internal_CompileBase
24
     */
25
    public $optional_attributes = array('levels');
26
27
    /**
28
     * Attribute definition: Overwrites base class.
29
     *
30
     * @var array
31
     * @see Smarty_Internal_CompileBase
32
     */
33
    public $shorttag_order = array('levels');
34
35
    /**
36
     * Compiles code for the {break} tag
37
     *
38
     * @param  array                                $args      array with attributes from parser
39
     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
40
     * @param  array                                $parameter array with compilation parameter
41
     *
42
     * @return string compiled code
43
     */
44
    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
0 ignored issues
show
Unused Code introduced by
The parameter $parameter is not used and could be removed.

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

Loading history...
45
    {
46
        list($levels, $foreachLevels) = $this->checkLevels($args, $compiler);
47
        $output = "<?php\n";
48
        if ($foreachLevels) {
49
            /* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
50
            $foreachCompiler = $compiler->getTagCompiler('foreach');
51
            $output .= $foreachCompiler->compileRestore($foreachLevels);
52
        }
53
        $output .= "break {$levels};?>";
54
        return $output;
55
    }
56
57
    /**
58
     * check attributes and return array of break and foreach levels
59
     *
60
     * @param  array                                $args     array with attributes from parser
61
     * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
62
     * @param  string                               $tag      tag name
63
     *
64
     * @return array
65
     * @throws \SmartyCompilerException
66
     */
67
    public function checkLevels($args, Smarty_Internal_TemplateCompilerBase $compiler, $tag = 'break')
68
    {
69
        static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true);
70
        // check and get attributes
71
        $_attr = $this->getAttributes($compiler, $args);
72
73
        if ($_attr[ 'nocache' ] === true) {
74
            $compiler->trigger_template_error('nocache option not allowed', null, true);
75
        }
76
77
        if (isset($_attr[ 'levels' ])) {
78
            if (!is_numeric($_attr[ 'levels' ])) {
79
                $compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
80
            }
81
            $levels = $_attr[ 'levels' ];
82
        } else {
83
            $levels = 1;
84
        }
85
        $level_count = $levels;
86
        $stack_count = count($compiler->_tag_stack) - 1;
87
        $foreachLevels = 0;
88
        $lastTag = '';
89
        while ($level_count >= 0 && $stack_count >= 0) {
90
            if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) {
91
                $lastTag = $compiler->_tag_stack[ $stack_count ][ 0 ];
92
                if ($level_count === 0) {
93
                    break;
94
                }
95
                $level_count --;
96
                if ($compiler->_tag_stack[ $stack_count ][ 0 ] === 'foreach') {
97
                    $foreachLevels ++;
98
                }
99
            }
100
            $stack_count --;
101
        }
102
        if ($level_count != 0) {
103
            $compiler->trigger_template_error("cannot {$tag} {$levels} level(s)", null, true);
104
        }
105
        if ($lastTag === 'foreach' && $tag === 'break') {
106
            $foreachLevels --;
107
        }
108
        return array($levels, $foreachLevels);
109
    }
110
}
111