Completed
Push — master ( 9ef838...0845f2 )
by Rob
01:58
created

Sanitise   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 35.29%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 5
dl 0
loc 185
ccs 30
cts 85
cp 0.3529
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A removeUrl() 0 24 4
A cleanse() 0 21 4
A cleanseCsv() 0 4 1
C disinfect() 0 62 14
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\classes\Strings;
8
use devtoolboxuk\soteria\classes\Url;
9
use devtoolboxuk\soteria\models\SoteriaModel;
10
11
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...
12
{
13
14
    private $is_valid = null;
15
    private $_sanitised = null;
16
    private $filters;
17
    private $input;
18
    private $output;
19
    private $strings;
20
    private $urlService;
21
22
    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...
23
    {
24
        $this->filters = new Filters();
25
        $this->strings = new Strings();
26
        $this->urlService = new Url();
27
    }
28
29
    /**
30
     *
31
     * Removes URLs from strings
32
     *
33
     * @param array|string $data
34
     * @return array|string|string[]|null
35
     */
36
    public function removeUrl($data)
37
    {
38
        $this->_sanitised = null;
39
40
        if (is_array($data)) {
41
            foreach ($data as $key => $value) {
42
                $data[$key] = $this->removeUrl($value);
43
            }
44
            return $data;
45
        }
46
47
        $this->input = $data = trim($data);
48
        $data = $this->urlService->remove($data);
49
        $data = trim($data);
50
51
        if ($this->input != $data) {
52
            $this->_sanitised = true;
53
        }
54
        $this->is_valid = true;
55
56
        $this->output = $data;
57
        return $data;
58
59
    }
60
61
    /**
62
     * @param $data
63
     * @param string $toEncoding
64
     * @param string $fromEncoding
65
     * @return array|false|string|string[]|null
66
     */
67
    public function cleanse($data, $toEncoding = 'utf-8', $fromEncoding = 'auto')
68
    {
69
70
        if (is_array($data)) {
71
            foreach ($data as $key => $value) {
72
                $data[$key] = $this->cleanse($value, $toEncoding, $fromEncoding);
73
            }
74
            return $data;
75
        }
76
77
        $this->input = $data = trim($data);
78
        $data = $this->strings->clean($data);
79
        $data = mb_convert_encoding($data, $toEncoding, $fromEncoding);
80
        $data = htmlspecialchars_decode($data);
81
        $data = $this->strings->clean($data);
82
        if ($this->input != $data) {
83
            $this->_sanitised = true;
84
        }
85
        $this->output = $data;
86
        return $data;
87
    }
88
89
    /**
90
     * @param $string
91
     * @param string $delimiter
92
     * @return string
93
     */
94
    public function cleanseCsv($string, $delimiter = "|")
95
    {
96
        return trim(str_replace([$delimiter, "\n", "\r", "\t"], " ", $string));
97
    }
98
99
    /**
100
     * @param $data
101
     * @param string $type
102
     * @param int $stringLength
103
     * @return mixed|string
104
     */
105 2
    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...
106
    {
107
108 2
        $this->_sanitised = null;
109
110 2
        if (is_array($data)) {
111
            foreach ($data as $key => $value) {
112
                $data[$key] = $this->disinfect($value, $type, $stringLength);
113
            }
114
            return $data;
115
        }
116
117 2
        $this->input = $data = trim($data);
118
119 2
        $data = $this->strings->clean($data);
120 2
        $data = $this->strings->stringLength($data, $stringLength);
121
122 2
        switch ($type) {
123 2
            case "email":
124 1
                $filterResult = $this->filters->filterEmail($data);
125 1
                break;
126
127 1
            case "encoded":
128
                $filterResult = $this->filters->filterEncoded($data);
129
                break;
130
131 1
            case "number_float":
132 1
            case "float":
133
                $filterResult = $this->filters->filterFloat($data);
134
                break;
135
136 1
            case "number_int":
137 1
            case "int":
138
                $filterResult = $this->filters->filterInt($data);
139
                break;
140
141 1
            case "full_special_chars":
142
                $filterResult = $this->filters->filterFullSpecialChar($data);
143
                break;
144
145 1
            case "url":
146
                $filterResult = $this->filters->filterUrl($data);
147
                break;
148
149 1
            case "string":
150
                $filterResult = $this->filters->filterString($data);
151
                break;
152
153
            default:
154 1
            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...
155 1
                $filterResult = $this->filters->filterSpecial($data);
156 1
                break;
157
        }
158
159 2
        if ($this->input != $filterResult->getResult()) {
160
            $this->_sanitised = true;
161
        }
162
163 2
        $this->is_valid = $filterResult->isValid();
164 2
        $this->output = $filterResult->getResult();
165 2
        return $this->output;
166
    }
167
168
169
    /**
170
     * @return null
171
     */
172
    public function isSanitised()
173
    {
174
        return $this->_sanitised;
175
    }
176
177
    /**
178
     * Returns true if the data is valid
179
     * @return null
180
     */
181
    public function isValid()
182
    {
183
        return $this->is_valid;
184
    }
185
186 2
    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...
187
    {
188 2
        $valid = false;
189 2
        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...
190 2
            $valid = true;
191
        }
192 2
        return new SoteriaModel($this->input, $this->output, $valid);
193
    }
194
195
}