Completed
Push — master ( 7d32f0...657179 )
by Richard
13:53 queued 04:43
created

function.math.php ➔ smarty_function_math()   D

Complexity

Conditions 22
Paths 81

Size

Total Lines 85
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 46
nc 81
nop 2
dl 0
loc 85
rs 4.7721
c 0
b 0
f 0

How to fix   Long Method    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
 * This plugin is only for Smarty2 BC
5
 *
6
 * @package    Smarty
7
 * @subpackage PluginsFunction
8
 */
9
10
/**
11
 * Smarty {math} function plugin
12
 * Type:     function<br>
13
 * Name:     math<br>
14
 * Purpose:  handle math computations in template
15
 *
16
 * @link     http://www.smarty.net/manual/en/language.function.math.php {math}
17
 *           (Smarty online manual)
18
 * @author   Monte Ohrt <monte at ohrt dot com>
19
 *
20
 * @param array                    $params   parameters
21
 * @param Smarty
22
 *
23
 * @return string|null
24
 */
25
function smarty_function_math($params, &$smarty)
26
{
27
    static $_allowed_funcs =
28
        array('int' => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true,
29
              'log' => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true, 'rand' => true,
30
              'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true);
31
    // be sure equation parameter is present
32
    if (empty($params[ 'equation' ])) {
33
        trigger_error("math: missing equation parameter", E_USER_WARNING);
34
35
        return;
36
    }
37
38
    $equation = $params[ 'equation' ];
39
40
    // make sure parenthesis are balanced
41
    if (substr_count($equation, "(") != substr_count($equation, ")")) {
42
        trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
43
44
        return;
45
    }
46
47
    // disallow backticks
48
    if (strpos($equation, '`') !== false) {
49
        trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
50
51
        return;
52
    }
53
54
    // also disallow dollar signs
55
    if (strpos($equation, '$') !== false) {
56
        trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
57
58
        return;
59
    }
60
61
    foreach ($params as $key => $val) {
62
        if ($key != "equation" && $key != "format" && $key != "assign") {
63
            // make sure value is not empty
64
            if (strlen($val) == 0) {
65
                trigger_error("math: parameter '{$key}' is empty", E_USER_WARNING);
66
67
                return;
68
            }
69
            if (!is_numeric($val)) {
70
                trigger_error("math: parameter '{$key}' is not numeric", E_USER_WARNING);
71
72
                return;
73
            }
74
        }
75
    }
76
77
    // match all vars in equation, make sure all are passed
78
    preg_match_all('!(?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)!', $equation, $match);
79
80
    foreach ($match[ 1 ] as $curr_var) {
81
        if ($curr_var && !isset($params[ $curr_var ]) && !isset($_allowed_funcs[ $curr_var ])) {
82
            trigger_error("math: function call '{$curr_var}' not allowed, or missing parameter '{$curr_var}'", E_USER_WARNING);
83
84
            return;
85
        }
86
    }
87
88
    foreach ($params as $key => $val) {
89
        if ($key != "equation" && $key != "format" && $key != "assign") {
90
            $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation);
91
        }
92
    }
93
    $smarty_math_result = null;
94
    eval("\$smarty_math_result = " . $equation . ";");
0 ignored issues
show
Coding Style introduced by
The function smarty_function_math() contains an eval expression.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
95
96
    if (empty($params[ 'format' ])) {
97
        if (empty($params[ 'assign' ])) {
98
            return $smarty_math_result;
99
        } else {
100
            $smarty->assign($params[ 'assign' ], $smarty_math_result);
101
        }
102
    } else {
103
        if (empty($params[ 'assign' ])) {
104
            printf($params[ 'format' ], $smarty_math_result);
105
        } else {
106
            $smarty->assign($params[ 'assign' ], sprintf($params[ 'format' ], $smarty_math_result));
107
        }
108
    }
109
}
110