Passed
Push — master ( a22c64...b9cca4 )
by Sebastian
03:46
created

Escape   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
C htmlEscape() 0 22 9
A sanitize() 0 3 1
B ordutf8() 0 30 5
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Filter\Rules;
13
14
/**
15
 * Return html entities.
16
 */
17
class Escape
18
{
19
    /**
20
     * Sanitize.
21
     *
22
     * @param mixed $value
23
     */
24 7
    public function sanitize(&$value)
25
    {
26 7
        $value = $this->htmlEscape($value);
27 7
    }
28
    
29
    /**
30
     * Return numerical part of the HTML encoding of the Unicode character.
31
     *
32
     * @param string $char
33
     * @return int
34
     */
35 7
    private function ordutf8(string $char) : int
36
    {
37 7
        $code = ord(substr($char, 0, 1));
0 ignored issues
show
Bug introduced by
It seems like substr($char, 0, 1) can also be of type false; however, parameter $string of ord() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $code = ord(/** @scrutinizer ignore-type */ substr($char, 0, 1));
Loading history...
38
39 7
        if ($code > 127) {
40
41
            //110xxxxx
42 3
            $bytes = 2;
43 3
            $count = 0;
44
45 3
            if ($code > 223) {
46
                //1110xxxx
47 2
                $bytes = 3;
48 2
                $count = 32;
49
            }
50
51 3
            if ($code > 239) {
52
                //11110xxx
53 1
                $bytes = 4;
54 1
                $count = 48;
55
            }
56
57 3
            $temp = $code - 192 - $count;
58
59 3
            for ($i = 1; $i < $bytes; $i++) {
60 3
                $code = $temp = $temp * 64 + ord(substr($char, $i, 1)) - 128;
61
            }
62
        }
63
64 7
        return $code;
65
    }
66
67
    /**
68
     * Convert char to html entities.
69
     *
70
     * @param string $string
71
     * @return string
72
     */
73 7
    private function htmlEscape(string $string) : string
74
    {
75 7
        $chars = preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY);
76 7
        $escaped = '';
77
78 7
        foreach ($chars as $key => $char) {
79 7
            $ord = $this->ordutf8($char, $key);
0 ignored issues
show
Unused Code introduced by
The call to Linna\Filter\Rules\Escape::ordutf8() has too many arguments starting with $key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            /** @scrutinizer ignore-call */ 
80
            $ord = $this->ordutf8($char, $key);

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. Please note the @ignore annotation hint above.

Loading history...
80
81
            if (
82 7
                ($ord > 32 && $ord < 48) ||
83 7
                ($ord > 57 && $ord < 65) ||
84 7
                ($ord > 90 && $ord < 97) ||
85 7
                ($ord > 122)
86
            ) {
87 7
                $escaped .= "&#{$ord};";
88 7
                continue;
89
            }
90
91 4
            $escaped .= $char;
92
        }
93
94 7
        return $escaped;
95
    }
96
}
97