Completed
Branch master (e379bd)
by Pierre-Henry
33:06
created

function.counter.php ➔ smarty_function_counter()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 29
nc 1024
nop 2
dl 0
loc 50
rs 3.375
c 0
b 0
f 0

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