Conditions | 11 |
Paths | 256 |
Total Lines | 91 |
Lines | 20 |
Ratio | 21.98 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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') { |
|
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') { |
|
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') { |
|
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') { |
|
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) { |
||
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 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.