PluginSection   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 15.27 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 20
loc 131
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
A preProcessing() 0 4 1
D postProcessing() 20 91 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 * Compatibility plugin for smarty templates, do not use otherwise, this is deprecated.
26
 * This software is provided 'as-is', without any express or implied warranty.
27
 * In no event will the authors be held liable for any damages arising from the use of this software.
28
 */
29
class PluginSection extends BlockPlugin implements ICompilableBlock, IElseable
30
{
31
    public static $cnt = 0;
32
33
    /**
34
     * @param      $name
35
     * @param      $loop
36
     * @param null $start
37
     * @param null $step
38
     * @param null $max
39
     * @param bool $show
40
     */
41
    public function init($name, $loop, $start = null, $step = null, $max = null, $show = true)
0 ignored issues
show
Unused Code introduced by
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
The parameter $loop 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 $start 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 $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
The parameter $max 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 $show 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...
42
    {
43
    }
44
45
    /**
46
     * @param Compiler $compiler
47
     * @param array    $params
48
     * @param string   $prepend
49
     * @param string   $append
50
     * @param string   $type
51
     *
52
     * @return string
53
     */
54
    public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
55
    {
56
        return '';
57
    }
58
59
    /**
60
     * @param Compiler $compiler
61
     * @param array    $params
62
     * @param string   $prepend
63
     * @param string   $append
64
     * @param string   $content
65
     *
66
     * @return string
67
     */
68
    public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
69
    {
70
        $output = Compiler::PHP_OPEN;
71
        $params = $compiler->getCompiledParams($params);
72
73
        // assigns params
74
        $loop  = $params['loop'];
75
        $start = $params['start'];
76
        $max   = $params['max'];
77
        $name  = $params['name'];
78
        $step  = $params['step'];
79
        $show  = $params['show'];
80
81
        // gets unique id
82
        $cnt = self::$cnt ++;
83
84
        $output .= '$this->globals[\'section\'][' . $name . '] = array();' . "\n" . '$_section' . $cnt . ' =& $this->globals[\'section\'][' . $name . '];' . "\n";
85
86 View Code Duplication
        if ($loop !== 'null') {
0 ignored issues
show
Duplication introduced by
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...
87
            $output .= '$_section' . $cnt . '[\'loop\'] = is_array($tmp = ' . $loop . ') ? count($tmp) : max(0, (int) $tmp);' . "\n";
88
        } else {
89
            $output .= '$_section' . $cnt . '[\'loop\'] = 1;' . "\n";
90
        }
91
92 View Code Duplication
        if ($show !== 'null') {
0 ignored issues
show
Duplication introduced by
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...
93
            $output .= '$_section' . $cnt . '[\'show\'] = ' . $show . ";\n";
94
        } else {
95
            $output .= '$_section' . $cnt . '[\'show\'] = true;' . "\n";
96
        }
97
98 View Code Duplication
        if ($name !== 'null') {
0 ignored issues
show
Duplication introduced by
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...
99
            $output .= '$_section' . $cnt . '[\'name\'] = ' . $name . ";\n";
100
        } else {
101
            $output .= '$_section' . $cnt . '[\'name\'] = true;' . "\n";
102
        }
103
104
        if ($max !== 'null') {
105
            $output .= '$_section' . $cnt . '[\'max\'] = (int)' . $max . ";\n" . 'if($_section' . $cnt . '[\'max\'] < 0) { $_section' . $cnt . '[\'max\'] = $_section' . $cnt . '[\'loop\']; }' . "\n";
106
        } else {
107
            $output .= '$_section' . $cnt . '[\'max\'] = $_section' . $cnt . '[\'loop\'];' . "\n";
108
        }
109
110 View Code Duplication
        if ($step !== 'null') {
0 ignored issues
show
Duplication introduced by
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...
111
            $output .= '$_section' . $cnt . '[\'step\'] = (int)' . $step . ' == 0 ? 1 : (int) ' . $step . ";\n";
112
        } else {
113
            $output .= '$_section' . $cnt . '[\'step\'] = 1;' . "\n";
114
        }
115
116
        if ($start !== 'null') {
117
            $output .= '$_section' . $cnt . '[\'start\'] = (int)' . $start . ";\n";
118
        } else {
119
            $output .= '$_section' . $cnt . '[\'start\'] = $_section' . $cnt . '[\'step\'] > 0 ? 0 : $_section' . $cnt . '[\'loop\'] - 1;' . "\n" . 'if ($_section' . $cnt . '[\'start\'] < 0) { $_section' . $cnt . '[\'start\'] = max($_section' . $cnt . '[\'step\'] > 0 ? 0 : -1, $_section' . $cnt . '[\'loop\'] + $_section' . $cnt . '[\'start\']); } ' . "\n" . 'else { $_section' . $cnt . '[\'start\'] = min($_section' . $cnt . '[\'start\'], $_section' . $cnt . '[\'step\'] > 0 ? $_section' . $cnt . '[\'loop\'] : $_section' . $cnt . '[\'loop\'] -1); }' . "\n";
120
        }
121
122
        /*		if ($usesAny) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
123
                    $output .= "\n".'$this->globals["section"]['.$name.'] = array'."\n(";
124
                    if ($usesIndex) $output .="\n\t".'"index"		=> 0,';
125
                    if ($usesIteration) $output .="\n\t".'"iteration"		=> 1,';
126
                    if ($usesFirst) $output .="\n\t".'"first"		=> null,';
127
                    if ($usesLast) $output .="\n\t".'"last"		=> null,';
128
                    if ($usesShow) $output .="\n\t".'"show"		=> ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
129
                    if ($usesTotal) $output .="\n\t".'"total"		=> $this->isArray($_for'.$cnt.'_from) ? $this->count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
130
                    $out.="\n);\n".'$_section'.$cnt.'[\'glob\'] =& $this->globals["section"]['.$name.'];'."\n\n";
131
                }
132
        */
133
134
        $output .= 'if ($_section' . $cnt . '[\'show\']) {' . "\n";
135
        if ($start === 'null' && $step === 'null' && $max === 'null') {
136
            $output .= '	$_section' . $cnt . '[\'total\'] = $_section' . $cnt . '[\'loop\'];' . "\n";
137
        } else {
138
            $output .= '	$_section' . $cnt . '[\'total\'] = min(ceil(($_section' . $cnt . '[\'step\'] > 0 ? $_section' . $cnt . '[\'loop\'] - $_section' . $cnt . '[\'start\'] : $_section' . $cnt . '[\'start\'] + 1) / abs($_section' . $cnt . '[\'step\'])), $_section' . $cnt . '[\'max\']);' . "\n";
139
        }
140
        $output .= '	if ($_section' . $cnt . '[\'total\'] == 0) {' . "\n" . '		$_section' . $cnt . '[\'show\'] = false;' . "\n" . '	}' . "\n" . '} else {' . "\n" . '	$_section' . $cnt . '[\'total\'] = 0;' . "\n}\n";
141
        $output .= 'if ($_section' . $cnt . '[\'show\']) {' . "\n";
142
        $output .= "\t" . 'for ($this->scope[' . $name . '] = $_section' . $cnt . '[\'start\'], $_section' . $cnt . '[\'iteration\'] = 1; ' . '$_section' . $cnt . '[\'iteration\'] <= $_section' . $cnt . '[\'total\']; ' . '$this->scope[' . $name . '] += $_section' . $cnt . '[\'step\'], $_section' . $cnt . '[\'iteration\']++) {' . "\n";
143
        $output .= "\t\t" . '$_section' . $cnt . '[\'rownum\'] = $_section' . $cnt . '[\'iteration\'];' . "\n";
144
        $output .= "\t\t" . '$_section' . $cnt . '[\'index_prev\'] = $this->scope[' . $name . '] - $_section' . $cnt . '[\'step\'];' . "\n";
145
        $output .= "\t\t" . '$_section' . $cnt . '[\'index_next\'] = $this->scope[' . $name . '] + $_section' . $cnt . '[\'step\'];' . "\n";
146
        $output .= "\t\t" . '$_section' . $cnt . '[\'first\']      = ($_section' . $cnt . '[\'iteration\'] == 1);' . "\n";
147
        $output .= "\t\t" . '$_section' . $cnt . '[\'last\']       = ($_section' . $cnt . '[\'iteration\'] == $_section' . $cnt . '[\'total\']);' . "\n";
148
149
        $output .= Compiler::PHP_CLOSE . $content . Compiler::PHP_OPEN;
150
151
        $output .= "\n\t}\n} " . Compiler::PHP_CLOSE;
152
153
        if (isset($params['hasElse'])) {
154
            $output .= $params['hasElse'];
155
        }
156
157
        return $output;
158
    }
159
}
160