| Conditions | 11 |
| Paths | 32 |
| Total Lines | 51 |
| Code Lines | 30 |
| 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 |
||
| 31 | function esc($data, $context='html', $escaper=null) |
||
| 32 | { |
||
| 33 | if (is_array($data)) |
||
| 34 | { |
||
| 35 | foreach ($data as $key => &$value) |
||
| 36 | { |
||
| 37 | $value = esc($value, $context); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | $context = strtolower($context); |
||
| 42 | |||
| 43 | if (! is_object($escaper)) |
||
| 44 | { |
||
| 45 | $escaper = new Escaper(config_item('charset')); |
||
| 46 | } |
||
| 47 | |||
| 48 | // Valid context? |
||
| 49 | if (! in_array($context, ['html', 'htmlattr', 'js', 'css', 'url'])) |
||
| 50 | { |
||
| 51 | throw new \InvalidArgumentException('Invalid Context type: '. $context); |
||
| 52 | } |
||
| 53 | |||
| 54 | if (! is_string($data)) |
||
| 55 | { |
||
| 56 | return $data; |
||
| 57 | } |
||
| 58 | |||
| 59 | switch ($context) |
||
| 60 | { |
||
| 61 | case 'html': |
||
| 62 | $data = $escaper->escapeHtml($data); |
||
| 63 | break; |
||
| 64 | case 'htmlattr': |
||
| 65 | $data = $escaper->escapeHtmlAttr($data); |
||
| 66 | break; |
||
| 67 | case 'js': |
||
| 68 | $data = $escaper->escapeJs($data); |
||
| 69 | break; |
||
| 70 | case 'css': |
||
| 71 | $data = $escaper->escapeCss($data); |
||
| 72 | break; |
||
| 73 | case 'url': |
||
| 74 | $data = $escaper->escapeUrl($data); |
||
| 75 | break; |
||
| 76 | default: |
||
| 77 | break; |
||
| 78 | } |
||
| 79 | |||
| 80 | return $data; |
||
| 81 | } |
||
| 82 | } |