PluginLoop::postProcessing()   F
last analyzed

Complexity

Conditions 25
Paths > 20000

Duplication

Lines 16
Ratio 19.51 %

Size

Total Lines 82
Code Lines 49

Importance

Changes 0
Metric Value
cc 25
eloc 49
nc 3594240
nop 5
dl 16
loc 82
rs 2.2136
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
 * Copyright (c) 2013-2016
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Blocks
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-19
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Blocks;
18
19
use Dwoo\Compiler;
20
use Dwoo\IElseable;
21
use Dwoo\Block\Plugin as BlockPlugin;
22
use Dwoo\ICompilable\Block as ICompilableBlock;
23
24
/**
25
 * Loops over an array and moves the scope into each value, allowing for shorter loop constructs.
26
 * Note that to access the array key within a loop block, you have to use the {$_key} variable,
27
 * you can not specify it yourself.
28
 * <pre>
29
 *  * from : the array that you want to iterate over
30
 *  * name : loop name to access it's iterator variables through {$.loop.name.var} see {@link
31
 *  http://wiki.dwoo.org/index.php/IteratorVariables} for details
32
 * </pre>
33
 * Example :
34
 * instead of a foreach block such as :
35
 * <code>
36
 * {foreach $variable value}
37
 *   {$value.foo} {$value.bar}
38
 * {/foreach}
39
 * </code>
40
 * you can do :
41
 * <code>
42
 * {loop $variable}
43
 *   {$foo} {$bar}
44
 * {/loop}
45
 * </code>
46
 * This software is provided 'as-is', without any express or implied warranty.
47
 * In no event will the authors be held liable for any damages arising from the use of this software.
48
 */
49
class PluginLoop extends BlockPlugin implements ICompilableBlock, IElseable
50
{
51
    public static $cnt = 0;
52
53
    /**
54
     * @param        $from
55
     * @param string $name
56
     */
57
    public function init($from, $name = 'default')
0 ignored issues
show
Unused Code introduced by DSanchez
The parameter $from 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 DSanchez
The parameter $name 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...
58
    {
59
    }
60
61
    /**
62
     * @param Compiler $compiler
63
     * @param array    $params
64
     * @param string   $prepend
65
     * @param string   $append
66
     * @param string   $type
67
     *
68
     * @return string
69
     */
70
    public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
71
    {
72
        // get block params and save the current template pointer to use it in the postProcessing method
73
        $currentBlock                         = &$compiler->getCurrentBlock();
74
        $currentBlock['params']['tplPointer'] = $compiler->getPointer();
75
76
        return '';
77
    }
78
79
    /**
80
     * @param Compiler $compiler
81
     * @param array    $params
82
     * @param string   $prepend
83
     * @param string   $append
84
     * @param string   $content
85
     *
86
     * @return string
87
     */
88
    public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
89
    {
90
        $params = $compiler->getCompiledParams($params);
91
        $tpl    = $compiler->getTemplateSource($params['tplPointer']);
92
93
        // assigns params
94
        $src  = $params['from'];
95
        $name = $params['name'];
96
97
        // evaluates which global variables have to be computed
98
        $varName       = '$dwoo.loop.' . trim($name, '"\'') . '.';
99
        $shortVarName  = '$.loop.' . trim($name, '"\'') . '.';
100
        $usesAny       = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
101
        $usesFirst     = strpos($tpl, $varName . 'first') !== false || strpos($tpl, $shortVarName . 'first') !== false;
102
        $usesLast      = strpos($tpl, $varName . 'last') !== false || strpos($tpl, $shortVarName . 'last') !== false;
103
        $usesIndex     = $usesFirst || strpos($tpl, $varName . 'index') !== false || strpos($tpl, $shortVarName . 'index') !== false;
104
        $usesIteration = $usesLast || strpos($tpl, $varName . 'iteration') !== false || strpos($tpl, $shortVarName . 'iteration') !== false;
105
        $usesShow      = strpos($tpl, $varName . 'show') !== false || strpos($tpl, $shortVarName . 'show') !== false;
106
        $usesTotal     = $usesLast || strpos($tpl, $varName . 'total') !== false || strpos($tpl, $shortVarName . 'total') !== false;
107
108 View Code Duplication
        if (strpos($name, '$this->scope[') !== false) {
0 ignored issues
show
Duplication introduced by DSanchez
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true;
110
        }
111
112
        // gets foreach id
113
        $cnt = self::$cnt ++;
114
115
        // builds pre processing output
116
        $pre = Compiler::PHP_OPEN . "\n" . '$_loop' . $cnt . '_data = ' . $src . ';';
117
        // adds foreach properties
118 View Code Duplication
        if ($usesAny) {
0 ignored issues
show
Duplication introduced by DSanchez
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
            $pre .= "\n" . '$this->globals["loop"][' . $name . '] = array' . "\n(";
120
            if ($usesIndex) {
121
                $pre .= "\n\t" . '"index"		=> 0,';
122
            }
123
            if ($usesIteration) {
124
                $pre .= "\n\t" . '"iteration"		=> 1,';
125
            }
126
            if ($usesFirst) {
127
                $pre .= "\n\t" . '"first"		=> null,';
128
            }
129
            if ($usesLast) {
130
                $pre .= "\n\t" . '"last"		=> null,';
131
            }
132
            if ($usesShow) {
133
                $pre .= "\n\t" . '"show"		=> $this->isTraversable($_loop' . $cnt . '_data, true),';
134
            }
135
            if ($usesTotal) {
136
                $pre .= "\n\t" . '"total"		=> $this->count($_loop' . $cnt . '_data),';
137
            }
138
            $pre .= "\n);\n" . '$_loop' . $cnt . '_glob =& $this->globals["loop"][' . $name . '];';
139
        }
140
        // checks if the loop must be looped
141
        $pre .= "\n" . 'if ($this->isTraversable($_loop' . $cnt . '_data' . (isset($params['hasElse']) ? ', true' : '') . ') == true)' . "\n{";
142
        // iterates over keys
143
        $pre .= "\n\t" . 'foreach ($_loop' . $cnt . '_data as $tmp_key => $this->scope["-loop-"])' . "\n\t{";
144
        // updates properties
145
        if ($usesFirst) {
146
            $pre .= "\n\t\t" . '$_loop' . $cnt . '_glob["first"] = (string) ($_loop' . $cnt . '_glob["index"] === 0);';
147
        }
148 View Code Duplication
        if ($usesLast) {
0 ignored issues
show
Duplication introduced by DSanchez
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
            $pre .= "\n\t\t" . '$_loop' . $cnt . '_glob["last"] = (string) ($_loop' . $cnt . '_glob["iteration"] === $_loop' . $cnt . '_glob["total"]);';
150
        }
151
        $pre .= "\n\t\t" . '$_loop' . $cnt . '_scope = $this->setScope(array("-loop-"));' . "\n/* -- loop start output */\n" . Compiler::PHP_CLOSE;
152
153
        // build post processing output and cache it
154
        $post = Compiler::PHP_OPEN . "\n" . '/* -- loop end output */' . "\n\t\t" . '$this->setScope($_loop' . $cnt . '_scope, true);';
155
        // update properties
156
        if ($usesIndex) {
157
            $post .= "\n\t\t" . '$_loop' . $cnt . '_glob["index"]+=1;';
158
        }
159
        if ($usesIteration) {
160
            $post .= "\n\t\t" . '$_loop' . $cnt . '_glob["iteration"]+=1;';
161
        }
162
        // end loop
163
        $post .= "\n\t}\n}\n" . Compiler::PHP_CLOSE;
164
        if (isset($params['hasElse'])) {
165
            $post .= $params['hasElse'];
166
        }
167
168
        return $pre . $content . $post;
169
    }
170
}
171