Conditions | 11 |
Paths | 1024 |
Total Lines | 39 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
23 | function smarty_function_counter($params, $template) |
||
24 | { |
||
25 | static $counters = array(); |
||
26 | $name = (isset($params[ 'name' ])) ? $params[ 'name' ] : 'default'; |
||
27 | if (!isset($counters[ $name ])) { |
||
28 | $counters[ $name ] = array('start' => 1, 'skip' => 1, 'direction' => 'up', 'count' => 1); |
||
29 | } |
||
30 | $counter =& $counters[ $name ]; |
||
31 | if (isset($params[ 'start' ])) { |
||
32 | $counter[ 'start' ] = $counter[ 'count' ] = (int)$params[ 'start' ]; |
||
33 | } |
||
34 | if (!empty($params[ 'assign' ])) { |
||
35 | $counter[ 'assign' ] = $params[ 'assign' ]; |
||
36 | } |
||
37 | if (isset($counter[ 'assign' ])) { |
||
38 | $template->assign($counter[ 'assign' ], $counter[ 'count' ]); |
||
39 | } |
||
40 | if (isset($params[ 'print' ])) { |
||
41 | $print = (bool)$params[ 'print' ]; |
||
42 | } else { |
||
43 | $print = empty($counter[ 'assign' ]); |
||
44 | } |
||
45 | if ($print) { |
||
46 | $retval = $counter[ 'count' ]; |
||
47 | } else { |
||
48 | $retval = null; |
||
49 | } |
||
50 | if (isset($params[ 'skip' ])) { |
||
51 | $counter[ 'skip' ] = $params[ 'skip' ]; |
||
52 | } |
||
53 | if (isset($params[ 'direction' ])) { |
||
54 | $counter[ 'direction' ] = $params[ 'direction' ]; |
||
55 | } |
||
56 | if ($counter[ 'direction' ] === 'down') { |
||
57 | $counter[ 'count' ] -= $counter[ 'skip' ]; |
||
58 | } else { |
||
59 | $counter[ 'count' ] += $counter[ 'skip' ]; |
||
60 | } |
||
61 | return $retval; |
||
62 | } |
||
63 |