Conditions | 6 |
Paths | 4 |
Total Lines | 51 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
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 /** XssFilterMicro */ |
||
47 | private function doXssClean($data) |
||
48 | { |
||
49 | if (is_array($data) && count($data)) { |
||
50 | foreach ($data as $k => &$v) { |
||
51 | $v = $this->doXssClean($data[$k]); |
||
52 | } |
||
53 | |||
54 | return $data; |
||
55 | } |
||
56 | |||
57 | if (trim($data) === '') { |
||
58 | return $data; |
||
59 | } |
||
60 | |||
61 | // xss_clean function from Kohana framework 2.3.1 |
||
62 | $data = str_replace(['&', '<', '>'], ['&amp;', '&lt;', '&gt;'], $data); |
||
63 | $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data); |
||
64 | $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data); |
||
65 | $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8'); |
||
66 | |||
67 | // Remove any attribute starting with "on" or xmlns |
||
68 | $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data); |
||
69 | |||
70 | // Remove javascript: and vbscript: protocols |
||
71 | $data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', |
||
72 | '$1=$2nojavascript...', $data); |
||
73 | $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', |
||
74 | '$1=$2novbscript...', $data); |
||
75 | $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', |
||
76 | '$1=$2nomozbinding...', $data); |
||
77 | |||
78 | // Only works in IE: <span style="width: expression(alert('Ping!'));"></span> |
||
79 | $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', |
||
80 | '$1>', $data); |
||
81 | $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', |
||
82 | '$1>', $data); |
||
83 | $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', |
||
84 | '$1>', $data); |
||
85 | |||
86 | // Remove namespaced elements (we do not need them) |
||
87 | $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data); |
||
88 | |||
89 | do { |
||
90 | // Remove really unwanted tags |
||
91 | $old_data = $data; |
||
92 | $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', |
||
93 | '', $data); |
||
94 | } while ($old_data !== $data); |
||
95 | |||
96 | return $data; |
||
97 | } |
||
98 | |||
107 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: