Conditions | 18 |
Paths | 18 |
Total Lines | 71 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 6 |
CRAP Score | 222.0043 |
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_modifier_escape($string, $esc_type = 'html', $char_set = 'ISO-8859-1') |
|
23 | { |
||
24 | switch ($esc_type) { |
||
25 | 2 | case 'html': |
|
26 | 2 | return htmlspecialchars($string, ENT_QUOTES, $char_set); |
|
27 | |||
28 | 2 | case 'html_entity_decode': |
|
29 | 2 | return html_entity_decode($string, ENT_QUOTES, $char_set); |
|
|
|||
30 | |||
31 | case 'htmlall': |
||
32 | return htmlentities($string, ENT_QUOTES, $char_set); |
||
33 | |||
34 | case 'url': |
||
35 | return rawurlencode($string); |
||
36 | |||
37 | case 'urlpathinfo': |
||
38 | return str_replace('%2F','/',rawurlencode($string)); |
||
39 | |||
40 | case 'quotes': |
||
41 | // escape unescaped single quotes |
||
42 | return preg_replace("%(?<!\\\\)'%", "\\'", $string); |
||
43 | |||
44 | case 'hex': |
||
45 | // escape every character into hex |
||
46 | $return = ''; |
||
47 | for ($x=0; $x < strlen($string); $x++) { |
||
48 | $return .= '%' . bin2hex($string[$x]); |
||
49 | } |
||
50 | return $return; |
||
51 | |||
52 | case 'hexentity': |
||
53 | $return = ''; |
||
54 | for ($x=0; $x < strlen($string); $x++) { |
||
55 | $return .= '&#x' . bin2hex($string[$x]) . ';'; |
||
56 | } |
||
57 | return $return; |
||
58 | |||
59 | case 'decentity': |
||
60 | $return = ''; |
||
61 | for ($x=0; $x < strlen($string); $x++) { |
||
62 | $return .= '&#' . ord($string[$x]) . ';'; |
||
63 | } |
||
64 | return $return; |
||
65 | |||
66 | case 'javascript': |
||
67 | // escape quotes and backslashes, newlines, etc. |
||
68 | return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/')); |
||
69 | |||
70 | case 'mail': |
||
71 | // safe way to display e-mail address on a web page |
||
72 | return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string); |
||
73 | |||
74 | case 'nonstd': |
||
75 | // escape non-standard chars, such as ms document quotes |
||
76 | $_res = ''; |
||
77 | for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) { |
||
78 | $_ord = ord(substr($string, $_i, 1)); |
||
79 | // non-standard char, escape it |
||
80 | if($_ord >= 126){ |
||
81 | $_res .= '&#' . $_ord . ';'; |
||
82 | } |
||
83 | else { |
||
84 | $_res .= substr($string, $_i, 1); |
||
85 | } |
||
86 | } |
||
87 | return $_res; |
||
88 | |||
89 | default: |
||
90 | return $string; |
||
91 | } |
||
92 | 1 | } |
|
93 | |||
97 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.