smarty_function_counter()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 28
c 1
b 0
f 0
nc 1024
nop 2
dl 0
loc 39
rs 3.15

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsFunction
7
 */
8
/**
9
 * Smarty {counter} function plugin
10
 * Type:     function
11
 * Name:     counter
12
 * Purpose:  print out a counter value
13
 *
14
 * @author Monte Ohrt <monte at ohrt dot com>
15
 * @link   http://www.smarty.net/manual/en/language.function.counter.php {counter}
16
 *         (Smarty online manual)
17
 *
18
 * @param array                    $params   parameters
19
 * @param Smarty_Internal_Template $template template object
20
 *
21
 * @return string|null
22
 */
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