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