PluginIf   F
last analyzed

Complexity

Total Complexity 67

Size/Duplication

Total Lines 232
Duplicated Lines 43.53 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 67
lcom 0
cbo 3
dl 101
loc 232
rs 3.04
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
F replaceKeywords() 101 176 63
A preProcessing() 0 4 1
A postProcessing() 0 14 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PluginIf often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PluginIf, and based on these observations, apply Extract Interface, too.

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
use Dwoo\Compilation\Exception as CompilationException;
24
25
/**
26
 * Conditional block, the syntax is very similar to the php one, allowing () || && and
27
 * other php operators. Additional operators and their equivalent php syntax are as follow :.
28
 * eq -> ==
29
 * neq or ne -> !=
30
 * gte or ge -> >=
31
 * lte or le -> <=
32
 * gt -> >
33
 * lt -> <
34
 * mod -> %
35
 * not -> !
36
 * X is [not] div by Y -> (X % Y) == 0
37
 * X is [not] even [by Y] -> (X % 2) == 0 or ((X/Y) % 2) == 0
38
 * X is [not] odd [by Y] -> (X % 2) != 0 or ((X/Y) % 2) != 0
39
 * This software is provided 'as-is', without any express or implied warranty.
40
 * In no event will the authors be held liable for any damages arising from the use of this software.
41
 */
42
class PluginIf extends BlockPlugin implements ICompilableBlock, IElseable
43
{
44
    /**
45
     * @param array $rest
46
     */
47
    public function init(array $rest)
0 ignored issues
show
Unused Code introduced by DSanchez
The parameter $rest 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 array    $params
53
     * @param array    $tokens
54
     * @param Compiler $compiler
55
     *
56
     * @return array
57
     * @throws CompilationException
58
     */
59
    public static function replaceKeywords(array $params, array $tokens, Compiler $compiler)
60
    {
61
        $p = array();
62
63
	foreach($params as $k => $v) {
64
            $v = (string)$v;
65
            if (substr($v, 0, 1) === '"' || substr($v, 0, 1) === '\'') {
66
                $vmod = strtolower(substr($v, 1, - 1));
67
            } else {
68
                $vmod = strtolower($v);
69
            }
70
            switch ($vmod) {
71
72 View Code Duplication
                case 'and':
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...
73
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
74
                        $p[] = '&&';
75
                    } else {
76
                        $p[] = $v;
77
                    }
78
                    break;
79 View Code Duplication
                case 'or':
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...
80
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
81
                        $p[] = '||';
82
                    } else {
83
                        $p[] = $v;
84
                    }
85
                    break;
86 View Code Duplication
                case 'xor':
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...
87
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
88
                        $p[] = '^';
89
                    } else {
90
                        $p[] = $v;
91
                    }
92
                    break;
93 View Code Duplication
                case 'eq':
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...
94
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
95
                        $p[] = '==';
96
                    } else {
97
                        $p[] = $v;
98
                    }
99
                    break;
100
                case 'ne':
101 View Code Duplication
                case 'neq':
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...
102
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
103
                        $p[] = '!=';
104
                    } else {
105
                        $p[] = $v;
106
                    }
107
                    break;
108
                case 'gte':
109 View Code Duplication
                case 'ge':
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...
110
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
111
                        $p[] = '>=';
112
                    } else {
113
                        $p[] = $v;
114
                    }
115
                    break;
116
                case 'lte':
117 View Code Duplication
                case 'le':
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...
118
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
119
                        $p[] = '<=';
120
                    } else {
121
                        $p[] = $v;
122
                    }
123
                    break;
124 View Code Duplication
                case 'gt':
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...
125
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
126
                        $p[] = '>';
127
                    } else {
128
                        $p[] = $v;
129
                    }
130
                    break;
131 View Code Duplication
                case 'lt':
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...
132
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
133
                        $p[] = '<';
134
                    } else {
135
                        $p[] = $v;
136
                    }
137
                    break;
138 View Code Duplication
                case 'mod':
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...
139
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
140
                        $p[] = '%';
141
                    } else {
142
                        $p[] = $v;
143
                    }
144
                    break;
145 View Code Duplication
                case 'not':
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...
146
                    if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
147
                        $p[] = '!';
148
                    } else {
149
                        $p[] = $v;
150
                    }
151
                    break;
152
                case '<>':
153
                    $p[] = '!=';
154
                    break;
155
                case '==':
156
                case '!=':
157
                case '>=':
158
                case '<=':
159
                case '>':
160
                case '<':
161
                case '===':
162
                case '!==':
163
                case '%':
164
                case '!':
165
                case '^':
166
                    $p[] = $vmod;
167
                    break;
168
                case 'is':
169
                    if ($tokens[$k] !== Compiler::T_UNQUOTED_STRING) {
170
                        $p[] = $v;
171
                        break;
172
                    }
173
                    if (isset($params[$k + 1]) && strtolower(trim($params[$k + 1], '"\'')) === 'not' && $tokens[$k + 1] === Compiler::T_UNQUOTED_STRING) {
174
                        $negate = true;
175
                        next($params);
176
                    } else {
177
                        $negate = false;
178
                    }
179
                    $ptr = 1 + (int)$negate;
180
                    if ($tokens[$k + $ptr] !== Compiler::T_UNQUOTED_STRING) {
181
                        break;
182
                    }
183
                    if (!isset($params[$k + $ptr])) {
184
                        $params[$k + $ptr] = '';
185
                    } else {
186
                        $params[$k + $ptr] = trim($params[$k + $ptr], '"\'');
187
                    }
188
                    switch ($params[$k + $ptr]) {
189
190
                        case 'div':
191
                            if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') {
192
                                $p[] = ' % ' . $params[$k + $ptr + 2] . ' ' . ($negate ? '!' : '=') . '== 0';
193
                                next($params);
194
                                next($params);
195
                                next($params);
196
                            } else {
197
                                throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] div by $b", found ' . $params[$k - 1] . ' is ' . ($negate ? 'not ' : '') . 'div ' . $params[$k + $ptr + 1] . ' ' . $params[$k + $ptr + 2]);
198
                            }
199
                            break;
200 View Code Duplication
                        case 'even':
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...
201
                            $a = array_pop($p);
202
                            if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') {
203
                                $b   = $params[$k + $ptr + 2];
204
                                $p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '!' : '=') . '== 0';
205
                                next($params);
206
                                next($params);
207
                            } else {
208
                                $p[] = $a . ' % 2 ' . ($negate ? '!' : '=') . '== 0';
209
                            }
210
                            next($params);
211
                            break;
212 View Code Duplication
                        case 'odd':
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...
213
                            $a = array_pop($p);
214
                            if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') {
215
                                $b   = $params[$k + $ptr + 2];
216
                                $p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '=' : '!') . '== 0';
217
                                next($params);
218
                                next($params);
219
                            } else {
220
                                $p[] = $a . ' % 2 ' . ($negate ? '=' : '!') . '== 0';
221
                            }
222
                            next($params);
223
                            break;
224
                        default:
225
                            throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] (div|even|odd) [by $b]", found ' . $params[$k - 1] . ' is ' . $params[$k + $ptr + 1]);
226
                    }
227
                    break;
228
                default:
229
                    $p[] = $v;
230
            }
231
        }
232
233
        return $p;
234
    }
235
236
    /**
237
     * @param Compiler $compiler
238
     * @param array    $params
239
     * @param string   $prepend
240
     * @param string   $append
241
     * @param string   $type
242
     *
243
     * @return string
244
     */
245
    public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
246
    {
247
        return '';
248
    }
249
250
    /**
251
     * @param Compiler $compiler
252
     * @param array    $params
253
     * @param string   $prepend
254
     * @param string   $append
255
     * @param string   $content
256
     *
257
     * @return string
258
     */
259
    public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
260
    {
261
        $tokens = $compiler->getParamTokens($params);
262
        $params = $compiler->getCompiledParams($params);
263
        $pre    = Compiler::PHP_OPEN . 'if (' . implode(' ', self::replaceKeywords($params['*'], $tokens['*'], $compiler)) . ") {\n" . Compiler::PHP_CLOSE;
264
265
        $post = Compiler::PHP_OPEN . "\n}" . Compiler::PHP_CLOSE;
266
267
        if (isset($params['hasElse'])) {
268
            $post .= $params['hasElse'];
269
        }
270
271
        return $pre . $content . $post;
272
    }
273
}
274