GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#168)
by
unknown
10:21
created

escape.php ➔ esc()   C

Complexity

Conditions 11
Paths 32

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 30
nc 32
nop 3
dl 0
loc 51
rs 5.7333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
use Zend\Escaper\Escaper;
4
5
if (! function_exists('esc'))
6
{
7
    /**
8
     * Escapes strings to make them safe for use
9
     * within HTML templates. Used by the auto-escaping
10
     * functionality in setVar() and available to
11
     * use within your views.
12
     *
13
     * Uses ZendFramework's Escaper to handle the actual escaping,
14
     * based on context. Valid contexts are:
15
     *      - html
16
     *      - htmlAttr
17
     *      - js
18
     *      - css
19
     *      - url
20
     *
21
     * References:
22
     *  - https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
23
     *  - http://framework.zend.com/manual/current/en/modules/zend.escaper.introduction.html
24
     *
25
     * @param $data
26
     * @param $context
27
     * @param escaper   // An instance of ZF's Escaper to avoid repeated class instantiation.
28
     *
29
     * @return string
30
     */
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
}