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

Smarty_Internal_Compile_Blockclose   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
F compile() 0 83 16
1
<?php
2
/*
3
 * This file is part of Smarty.
4
 *
5
 * (c) 2015 Uwe Tews
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * Smarty Internal Plugin Compile Block Class
13
 *
14
 * @author Uwe Tews <[email protected]>
15
 */
16
class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inheritance
17
{
18
    /**
19
     * Attribute definition: Overwrites base class.
20
     *
21
     * @var array
22
     * @see Smarty_Internal_CompileBase
23
     */
24
    public $required_attributes = array('name');
25
26
    /**
27
     * Attribute definition: Overwrites base class.
28
     *
29
     * @var array
30
     * @see Smarty_Internal_CompileBase
31
     */
32
    public $shorttag_order = array('name');
33
34
    /**
35
     * Attribute definition: Overwrites base class.
36
     *
37
     * @var array
38
     * @see Smarty_Internal_CompileBase
39
     */
40
    public $option_flags = array('hide', 'nocache');
41
42
    /**
43
     * Attribute definition: Overwrites base class.
44
     *
45
     * @var array
46
     * @see Smarty_Internal_CompileBase
47
     */
48
    public $optional_attributes = array('assign');
49
50
    /**
51
     * Saved compiler object
52
     *
53
     * @var Smarty_Internal_TemplateCompilerBase
54
     */
55
    public $compiler = null;
56
57
    /**
58
     * Compiles code for the {block} tag
59
     *
60
     * @param  array                                 $args      array with attributes from parser
61
     * @param  \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
62
     * @param  array                                 $parameter array with compilation parameter
63
     *
64
     * @return bool true
65
     */
66
    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...
67
    {
68
        if (!isset($compiler->_cache[ 'blockNesting' ])) {
69
            $compiler->_cache[ 'blockNesting' ] = 0;
70
        }
71
        if ($compiler->_cache[ 'blockNesting' ] == 0) {
72
            // make sure that inheritance gets initialized in template code
73
            $this->registerInit($compiler);
74
            $this->option_flags = array('hide', 'nocache', 'append', 'prepend');
75
        } else {
76
            $this->option_flags = array('hide', 'nocache');
77
        }
78
        // check and get attributes
79
        $_attr = $this->getAttributes($compiler, $args);
80
        $compiler->_cache[ 'blockNesting' ] ++;
81
        $_className = 'Block_' . preg_replace('![^\w]+!', '_', uniqid(rand(), true));
82
        $compiler->_cache[ 'blockName' ][ $compiler->_cache[ 'blockNesting' ] ] = $_attr[ 'name' ];
83
        $compiler->_cache[ 'blockClass' ][ $compiler->_cache[ 'blockNesting' ] ] = $_className;
84
        $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ] = array();
85
        $compiler->_cache[ 'blockParams' ][ 1 ][ 'subBlocks' ][ trim($_attr[ 'name' ], '"\'') ][] = $_className;
86
        $this->openTag($compiler, 'block', array($_attr, $compiler->nocache, $compiler->parser->current_buffer,
87
                                                 $compiler->template->compiled->has_nocache_code,
88
                                                 $compiler->template->caching));
89
        // must whole block be nocache ?
90
        if ($compiler->tag_nocache) {
91
            $i = 0;
0 ignored issues
show
Unused Code introduced by
$i 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...
92
        }
93
        $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
94
        // $compiler->suppressNocacheProcessing = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
        if ($_attr[ 'nocache' ] === true) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
96
            //$compiler->trigger_template_error('nocache option not allowed', $compiler->parser->lex->taglineno);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
        }
98
        $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
99
        $compiler->template->compiled->has_nocache_code = false;
100
        $compiler->suppressNocacheProcessing = true;
101
    }
102
}
103
104
/**
105
 * Smarty Internal Plugin Compile BlockClose Class
106
 *
107
 */
108
class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_Inheritance
109
{
110
    /**
111
     * Compiles code for the {/block} tag
112
     *
113
     * @param  array                                $args      array with attributes from parser
114
     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
115
     * @param  array                                $parameter array with compilation parameter
116
     *
117
     * @return bool true
118
     */
119
    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
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...
120
    {
121
        list($_attr, $_nocache, $_buffer, $_has_nocache_code, $_caching) = $this->closeTag($compiler, array('block'));
0 ignored issues
show
Unused Code introduced by
The assignment to $_caching is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
122
        // init block parameter
123
        $_block = $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ];
124
        unset($compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]);
125
        $_name = $_attr[ 'name' ];
126
        $_assign = isset($_attr[ 'assign' ]) ? $_attr[ 'assign' ] : null;
127
        unset($_attr[ 'assign' ], $_attr[ 'name' ]);
128
        foreach ($_attr as $name => $stat) {
129
            if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat != 'false')) {
130
                $_block[ $name ] = 'true';
131
            }
132
        }
133
        $_className = $compiler->_cache[ 'blockClass' ][ $compiler->_cache[ 'blockNesting' ] ];
134
        // get compiled block code
135
        $_functionCode = $compiler->parser->current_buffer;
136
        // setup buffer for template function code
137
        $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
138
139
        $output = "<?php\n";
140
        $output .= "/* {block {$_name}} */\n";
141
        $output .= "class {$_className} extends Smarty_Internal_Block\n";
142
        $output .= "{\n";
143
        foreach ($_block as $property => $value) {
144
            $output .= "public \${$property} = " . var_export($value,true) .";\n";
145
        }
146
        $output .= "public function callBlock(Smarty_Internal_Template \$_smarty_tpl) {\n";
147
        //$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
148
        if ($compiler->template->compiled->has_nocache_code) {
149
            $output .= "\$_smarty_tpl->cached->hashes['{$compiler->template->compiled->nocache_hash}'] = true;\n";
150
        }
151
        if (isset($_assign)) {
152
            $output .= "ob_start();\n";
153
        }
154
        $output .= "?>\n";
155
        $compiler->parser->current_buffer->append_subtree($compiler->parser,
156
                                                          new Smarty_Internal_ParseTree_Tag($compiler->parser,
157
                                                                                            $output));
158
        $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
159
        $output = "<?php\n";
160
        if (isset($_assign)) {
161
            $output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
162
        }
163
        $output .= "}\n";
164
        $output .= "}\n";
165
        $output .= "/* {/block {$_name}} */\n\n";
166
        $output .= "?>\n";
167
        $compiler->parser->current_buffer->append_subtree($compiler->parser,
168
                                                          new Smarty_Internal_ParseTree_Tag($compiler->parser,
169
                                                                                            $output));
170
        $compiler->blockOrFunctionCode .= $f = $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
0 ignored issues
show
Unused Code introduced by
$f 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...
171
        $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
172
        // nocache plugins must be copied
173
        if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) {
174
            foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) {
175
                foreach ($tmp as $type => $data) {
176
                    $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin ][ $type ] =
177
                        $data;
178
                }
179
            }
180
        }
181
182
        // restore old status
183
        $compiler->template->compiled->has_nocache_code = $_has_nocache_code;
184
        $compiler->tag_nocache = $compiler->nocache;
185
        $compiler->nocache = $_nocache;
186
        $compiler->parser->current_buffer = $_buffer;
187
        $output = "<?php \n";
188
        if ($compiler->_cache[ 'blockNesting' ] == 1) {
189
            $output .= "\$_smarty_tpl->inheritance->instanceBlock(\$_smarty_tpl, '$_className', $_name);\n";
190
        } else {
191
            $output .= "\$_smarty_tpl->inheritance->instanceBlock(\$_smarty_tpl, '$_className', $_name, \$this->tplIndex);\n";
192
        }
193
        $output .= "?>\n";
194
        $compiler->_cache[ 'blockNesting' ] --;
195
        if ($compiler->_cache[ 'blockNesting' ] == 0) {
196
            unset($compiler->_cache[ 'blockNesting' ]);
197
        }
198
        $compiler->has_code = true;
199
        $compiler->suppressNocacheProcessing = true;
200
        return $output;
201
    }
202
}
203