| Conditions | 16 |
| Paths | 31 |
| Total Lines | 60 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 19 |
| CRAP Score | 33.1577 |
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 |
||
| 22 | 1 | function smarty_function_math($params, &$smarty) |
|
| 23 | { |
||
| 24 | // be sure equation parameter is present |
||
| 25 | 3 | if (empty($params['equation'])) { |
|
| 26 | $smarty->trigger_error("math: missing equation parameter"); |
||
| 27 | return; |
||
| 28 | } |
||
| 29 | |||
| 30 | // strip out backticks, not necessary for math |
||
| 31 | 3 | $equation = str_replace('`','',$params['equation']); |
|
| 32 | |||
| 33 | // make sure parenthesis are balanced |
||
| 34 | 3 | if (substr_count($equation,"(") != substr_count($equation,")")) { |
|
| 35 | $smarty->trigger_error("math: unbalanced parenthesis"); |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | // match all vars in equation, make sure all are passed |
||
| 40 | 3 | preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!",$equation, $match); |
|
| 41 | 3 | $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10', |
|
| 42 | 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan'); |
||
| 43 | |||
| 44 | 3 | foreach($match[1] as $curr_var) { |
|
| 45 | 1 | if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) { |
|
| 46 | $smarty->trigger_error("math: function call $curr_var not allowed"); |
||
| 47 | 1 | return; |
|
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | 3 | foreach($params as $key => $val) { |
|
| 52 | 3 | if ($key != "equation" && $key != "format" && $key != "assign") { |
|
| 53 | // make sure value is not empty |
||
| 54 | 1 | if (strlen($val)==0) { |
|
| 55 | $smarty->trigger_error("math: parameter $key is empty"); |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | 1 | if (!is_numeric($val)) { |
|
| 59 | $smarty->trigger_error("math: parameter $key: is not numeric"); |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | 3 | $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation); |
|
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | 3 | eval("\$smarty_math_result = ".$equation.";"); |
|
|
|
|||
| 67 | |||
| 68 | 3 | if (empty($params['format'])) { |
|
| 69 | 3 | if (empty($params['assign'])) { |
|
| 70 | return $smarty_math_result; |
||
| 71 | } else { |
||
| 72 | 3 | $smarty->assign($params['assign'],$smarty_math_result); |
|
| 73 | } |
||
| 74 | } else { |
||
| 75 | if (empty($params['assign'])){ |
||
| 76 | printf($params['format'],$smarty_math_result); |
||
| 77 | } else { |
||
| 78 | $smarty->assign($params['assign'],sprintf($params['format'],$smarty_math_result)); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | 3 | } |
|
| 82 | |||
| 85 | ?> |
On one hand,
evalmight 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,evalprevents some optimization that they perform.