PluginFor::postProcessing()   F
last analyzed

Complexity

Conditions 29
Paths > 20000

Duplication

Lines 6
Ratio 5.26 %

Size

Total Lines 114
Code Lines 69

Importance

Changes 0
Metric Value
cc 29
eloc 69
nc 19169280
nop 5
dl 6
loc 114
rs 2
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
 * Similar to the php for block
26
 * <pre>
27
 *  * name : for name to access it's iterator variables through {$.for.name.var} see {@link
28
 *  http://wiki.dwoo.org/index.php/IteratorVariables} for details
29
 *  * from : array to iterate from (which equals 0) or a number as a start value
30
 *  * to : value to stop iterating at (equals count($array) by default if you set an array in from)
31
 *  * step : defines the incrementation of the pointer at each iteration
32
 * </pre>
33
 * This software is provided 'as-is', without any express or implied warranty.
34
 * In no event will the authors be held liable for any damages arising from the use of this software.
35
 */
36
class PluginFor extends BlockPlugin implements ICompilableBlock, IElseable
37
{
38
    public static $cnt = 0;
39
40
    /**
41
     * @param      $name
42
     * @param      $from
43
     * @param null $to
44
     * @param int  $step
45
     * @param int  $skip
46
     */
47
    public function init($name, $from, $to = null, $step = 1, $skip = 0)
0 ignored issues
show
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...
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 $to 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 $step 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 $skip 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...
48
    {
49
    }
50
51
    /**
52
     * @param Compiler $compiler
53
     * @param array    $params
54
     * @param string   $prepend
55
     * @param string   $append
56
     * @param string   $type
57
     *
58
     * @return string
59
     */
60
    public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
61
    {
62
        // get block params and save the current template pointer to use it in the postProcessing method
63
        $currentBlock                         = &$compiler->getCurrentBlock();
64
        $currentBlock['params']['tplPointer'] = $compiler->getPointer();
65
66
        return '';
67
    }
68
69
    /**
70
     * @param Compiler $compiler
71
     * @param array    $params
72
     * @param string   $prepend
73
     * @param string   $append
74
     * @param string   $content
75
     *
76
     * @return string
77
     */
78
    public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
79
    {
80
        $params = $compiler->getCompiledParams($params);
81
        $tpl    = $compiler->getTemplateSource($params['tplPointer']);
82
83
        // assigns params
84
        $from = $params['from'];
85
        $name = $params['name'];
86
        $step = $params['step'];
87
        $to   = $params['to'];
88
89
        // evaluates which global variables have to be computed
90
        $varName       = '$dwoo.for.' . trim($name, '"\'') . '.';
91
        $shortVarName  = '$.for.' . trim($name, '"\'') . '.';
92
        $usesAny       = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
93
        $usesFirst     = strpos($tpl, $varName . 'first') !== false || strpos($tpl, $shortVarName . 'first') !== false;
94
        $usesLast      = strpos($tpl, $varName . 'last') !== false || strpos($tpl, $shortVarName . 'last') !== false;
95
        $usesIndex     = strpos($tpl, $varName . 'index') !== false || strpos($tpl, $shortVarName . 'index') !== false;
96
        $usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName . 'iteration') !== false || strpos($tpl, $shortVarName . 'iteration') !== false;
97
        $usesShow      = strpos($tpl, $varName . 'show') !== false || strpos($tpl, $shortVarName . 'show') !== false;
98
        $usesTotal     = $usesLast || strpos($tpl, $varName . 'total') !== false || strpos($tpl, $shortVarName . 'total') !== false;
99
100 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...
101
            $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true;
102
        }
103
104
        // gets foreach id
105
        $cnt = self::$cnt ++;
106
107
        // builds pre processing output for
108
        $out = Compiler::PHP_OPEN . "\n" . '$_for' . $cnt . '_from = ' . $from . ';' . "\n" . '$_for' . $cnt . '_to = ' . $to . ';' . "\n" . '$_for' . $cnt . '_step = abs(' . $step . ');' . "\n" . 'if (is_numeric($_for' . $cnt . '_from) && !is_numeric($_for' . $cnt . '_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }' . "\n" . '$tmp_shows = $this->isArray($_for' . $cnt . '_from, true) || (is_numeric($_for' . $cnt . '_from) && (abs(($_for' . $cnt . '_from - $_for' . $cnt . '_to)/$_for' . $cnt . '_step) !== 0 || $_for' . $cnt . '_from == $_for' . $cnt . '_to));';
109
        // adds for properties
110
        if ($usesAny) {
111
            $out .= "\n" . '$this->globals["for"][' . $name . '] = array' . "\n(";
112
            if ($usesIndex) {
113
                $out .= "\n\t" . '"index"		=> 0,';
114
            }
115
            if ($usesIteration) {
116
                $out .= "\n\t" . '"iteration"		=> 1,';
117
            }
118
            if ($usesFirst) {
119
                $out .= "\n\t" . '"first"		=> null,';
120
            }
121
            if ($usesLast) {
122
                $out .= "\n\t" . '"last"		=> null,';
123
            }
124
            if ($usesShow) {
125
                $out .= "\n\t" . '"show"		=> $tmp_shows,';
126
            }
127
            if ($usesTotal) {
128
                $out .= "\n\t" . '"total"		=> $this->isArray($_for' . $cnt . '_from) ? floor($this->count($_for' . $cnt . '_from) / $_for' . $cnt . '_step) : (is_numeric($_for' . $cnt . '_from) ? abs(($_for' . $cnt . '_to + 1 - $_for' . $cnt . '_from)/$_for' . $cnt . '_step) : 0),';
129
            }
130
            $out .= "\n);\n" . '$_for' . $cnt . '_glob =& $this->globals["for"][' . $name . '];';
131
        }
132
        // checks if for must be looped
133
        $out .= "\n" . 'if ($tmp_shows)' . "\n{";
134
        // set from/to to correct values if an array was given
135
        $out .= "\n\t" . 'if ($this->isArray($_for' . $cnt . '_from' . (isset($params['hasElse']) ? ', true' : '') . ') == true) {
136
		$_for' . $cnt . '_to = is_numeric($_for' . $cnt . '_to) ? $_for' . $cnt . '_to - $_for' . $cnt . '_step : $this->count($_for' . $cnt . '_from) - 1;
137
		$_for' . $cnt . '_from = 0;
138
	}';
139
140
        // if input are pure numbers it shouldn't reorder them, if it's variables it gets too messy though so in that case a counter should be used
141
        $reverse     = false;
142
        $condition   = '<=';
143
        $incrementer = '+';
144
145
        if (preg_match('{^(["\']?)([0-9]+)\1$}', $from, $mN1) && preg_match('{^(["\']?)([0-9]+)\1$}', $to, $mN2)) {
146
            $from = (int)$mN1[2];
147
            $to   = (int)$mN2[2];
148
            if ($from > $to) {
149
                $reverse     = true;
150
                $condition   = '>=';
151
                $incrementer = '-';
152
            }
153
        }
154
155
        // reverse from and to if needed
156
        if (!$reverse) {
157
            $out .= "\n\t" . 'if ($_for' . $cnt . '_from > $_for' . $cnt . '_to) {
158
				$tmp = $_for' . $cnt . '_from;
159
				$_for' . $cnt . '_from = $_for' . $cnt . '_to;
160
				$_for' . $cnt . '_to = $tmp;
161
			}';
162
        }
163
164
        $out .= "\n\t" . 'for ($this->scope[' . $name . '] = $_for' . $cnt . '_from; $this->scope[' . $name . '] ' . $condition . ' $_for' . $cnt . '_to; $this->scope[' . $name . '] ' . $incrementer . '= $_for' . $cnt . '_step)' . "\n\t{";
165
        // updates properties
166
        if ($usesIndex) {
167
            $out .= "\n\t\t" . '$_for' . $cnt . '_glob["index"] = $this->scope[' . $name . '];';
168
        }
169
        if ($usesFirst) {
170
            $out .= "\n\t\t" . '$_for' . $cnt . '_glob["first"] = (string) ($_for' . $cnt . '_glob["iteration"] === 1);';
171
        }
172 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...
173
            $out .= "\n\t\t" . '$_for' . $cnt . '_glob["last"] = (string) ($_for' . $cnt . '_glob["iteration"] === $_for' . $cnt . '_glob["total"]);';
174
        }
175
        $out .= "\n/* -- for start output */\n" . Compiler::PHP_CLOSE;
176
177
        // build post processing output and cache it
178
        $postOut = Compiler::PHP_OPEN . '/* -- for end output */';
179
        // update properties
180
        if ($usesIteration) {
181
            $postOut .= "\n\t\t" . '$_for' . $cnt . '_glob["iteration"]+=1;';
182
        }
183
        // end loop
184
        $postOut .= "\n\t}\n}\n" . Compiler::PHP_CLOSE;
185
186
        if (isset($params['hasElse'])) {
187
            $postOut .= $params['hasElse'];
188
        }
189
190
        return $out . $content . $postOut;
191
    }
192
}
193