| Conditions | 6 |
| Paths | 4 |
| Total Lines | 52 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 | /** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
||
| 66 | $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8'); |
||
| 67 | |||
| 68 | // Remove any attribute starting with "on" or xmlns |
||
| 69 | $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data); |
||
| 70 | |||
| 71 | // Remove javascript: and vbscript: protocols |
||
| 72 | $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', |
||
| 73 | '$1=$2nojavascript...', $data); |
||
| 74 | $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', |
||
| 75 | '$1=$2novbscript...', $data); |
||
| 76 | $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', |
||
| 77 | '$1=$2nomozbinding...', $data); |
||
| 78 | |||
| 79 | // Only works in IE: <span style="width: expression(alert('Ping!'));"></span> |
||
| 80 | $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', |
||
| 81 | '$1>', $data); |
||
| 82 | $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', |
||
| 83 | '$1>', $data); |
||
| 84 | $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', |
||
| 85 | '$1>', $data); |
||
| 86 | |||
| 87 | // Remove namespaced elements (we do not need them) |
||
| 88 | $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data); |
||
| 89 | |||
| 90 | do { |
||
| 91 | // Remove really unwanted tags |
||
| 92 | $old_data = $data; |
||
| 93 | $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', |
||
| 94 | '', $data); |
||
| 95 | } while ($old_data !== $data); |
||
| 96 | |||
| 97 | return $data; |
||
| 98 | } |
||
| 99 | |||
| 108 |
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: