Completed
Push — master ( d15e2a...9605ab )
by Rob
01:57
created

Sanitise   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 24.75%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 2
dl 0
loc 207
ccs 25
cts 101
cp 0.2475
rs 9.76
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A removeUrl() 0 25 4
A cleanString() 0 5 1
A cleanse() 0 21 4
A cleanseCsv() 0 4 1
C disinfect() 0 62 14
A stringLength() 0 9 3
A isSanitised() 0 4 1
A isValid() 0 4 1
A result() 0 8 3
1
<?php
2
3
namespace devtoolboxuk\soteria\handlers;
4
5
6
use devtoolboxuk\soteria\classes\Filters;
7
use devtoolboxuk\soteria\models\SoteriaModel;
8
9
class Sanitise
0 ignored issues
show
Coding Style introduced by
The property $is_valid is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $_sanitised is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
10
{
11
12
    private $is_valid = null;
13
    private $_sanitised = null;
14
    private $filters;
15
    private $input;
16
    private $output;
17
18
    private $urlRegEx = '/\b((https?|ftp|file):\/\/|www\.)[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
19
20
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __construct.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
21
    {
22
        $this->filters = new Filters();
23
    }
24
25
    /**
26
     *
27
     * Removes URLs from strings
28
     *
29
     * @param array|string $data
30
     * @return array|string|string[]|null
31
     */
32
    public function removeUrl($data)
33
    {
34
        $this->_sanitised = null;
35
36
        if (is_array($data)) {
37
            foreach ($data as $key => $value) {
38
                $data[$key] = $this->removeUrl($value);
39
            }
40
            return $data;
41
        }
42
43
        $this->input = $data;
44
45
        $data = $this->cleanString($data);
46
        $data = preg_replace($this->urlRegEx, ' ', $data);
47
48
        if ($this->input != $data) {
49
            $this->_sanitised = true;
50
        }
51
        $this->is_valid = true;
52
53
        $this->output = $data;
54
        return $data;
55
56
    }
57
58
    /**
59
     * @param $data
60
     * @return string
61
     */
62 1
    private function cleanString($data)
63
    {
64 1
        $data = implode("", explode("\\", $data));
65 1
        return strip_tags(trim(stripslashes($data)));
66
    }
67
68
    /**
69
     * @param $data
70
     * @return array|false|string|string[]|null
71
     */
72
    public function cleanse($data)
73
    {
74
75
        if (is_array($data)) {
76
            foreach ($data as $key => $value) {
77
                $data[$key] = $this->cleanse($value);
78
            }
79
            return $data;
80
        }
81
82
        $this->input = $data;
83
        $data = $this->cleanString($data);
84
        $data = mb_convert_encoding($data, "utf-8", "auto");
85
        $data = htmlspecialchars_decode($data);
86
        $data = $this->cleanString($data);
87
        if ($this->input != $data) {
88
            $this->_sanitised = true;
89
        }
90
        $this->output = $data;
91
        return $data;
92
    }
93
94
    /**
95
     * @param $string
96
     * @param string $delimiter
97
     * @return string
98
     */
99
    public function cleanseCsv($string, $delimiter = "|")
100
    {
101
        return trim(str_replace([$delimiter, "\n", "\r", "\t"], " ", $string));
102
    }
103
104
    /**
105
     * @param $data
106
     * @param string $type
107
     * @param int $stringLength
108
     * @return mixed|string
109
     */
110 1
    public function disinfect($data, $type = 'special_chars', $stringLength = -1)
0 ignored issues
show
Complexity introduced by
This operation has 66 execution paths which exceeds the configured maximum of 10.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
111
    {
112
113 1
        $this->_sanitised = null;
114
115 1
        if (is_array($data)) {
116
            foreach ($data as $key => $value) {
117
                $data[$key] = $this->disinfect($value, $type, $stringLength);
118
            }
119
            return $data;
120
        }
121
122 1
        $this->input = $data;
123
124 1
        $data = $this->cleanString($data);
125 1
        $data = $this->stringLength($data, $stringLength);
126
127
        switch ($type) {
128 1
            case "email":
129 1
                $filterResult = $this->filters->filterEmail($data);
130 1
                break;
131
132
            case "encoded":
133
                $filterResult = $this->filters->filterEncoded($data);
134
                break;
135
136
            case "number_float":
137
            case "float":
138
                $filterResult = $this->filters->filterFloat($data);
139
                break;
140
141
            case "number_int":
142
            case "int":
143
                $filterResult = $this->filters->filtetInt($data);
0 ignored issues
show
Bug introduced by
The method filtetInt() does not seem to exist on object<devtoolboxuk\soteria\classes\Filters>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
                break;
145
146
            case "full_special_chars":
147
                $filterResult = $this->filters->filterFullSpecialChar($data);
148
                break;
149
150
            case "url":
151
                $filterResult = $this->filters->filterUrl($data);
152
                break;
153
154
            case "string":
155
                $filterResult = $this->filters->filterString($data);
156
                break;
157
158
            default:
159
            case "special_chars":
0 ignored issues
show
Unused Code introduced by
case 'special_chars': ...cial($data); break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
160
                $filterResult = $this->filters->filterSpecial($data);
161
                break;
162
        }
163
164 1
        if ($this->input != $filterResult->getResult()) {
165
            $this->_sanitised = true;
166
        }
167
168 1
        $this->is_valid = $filterResult->isValid();
169 1
        $this->output = $filterResult->getResult();
170 1
        return $this->output;
171
    }
172
173
    /**
174
     * @param $data
175
     * @param int $length
176
     * @return bool|string
177
     */
178 1
    private function stringLength($data, $length = -1)
179
    {
180 1
        if ($length > 0) {
181
            if (mb_strlen($data) > $length) {
182
                $data = substr($data, 0, $length);
183
            }
184
        }
185 1
        return $data;
186
    }
187
188
189
    /**
190
     * @return null
191
     */
192
    public function isSanitised()
193
    {
194
        return $this->_sanitised;
195
    }
196
197
    /**
198
     * Returns true if the data is valid
199
     * @return null
200
     */
201
    public function isValid()
202
    {
203
        return $this->is_valid;
204
    }
205
206 1
    function result()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for result.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
207
    {
208 1
        $valid = false;
209 1
        if (!$this->_sanitised && $this->is_valid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_sanitised of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
210 1
            $valid = true;
211 1
        }
212 1
        return new SoteriaModel($this->input, $this->output, $valid);
213
    }
214
215
}