Completed
Push — master ( cf38d9...2ecc50 )
by Rob
01:50
created

Sanitise::isSanitised()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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;
48
49
        $data = $this->strings->clean($data);
50
        $data = $this->urlService->remove($data);
51
52
        if ($this->input != $data) {
53
            $this->_sanitised = true;
54
        }
55
        $this->is_valid = true;
56
57
        $this->output = $data;
58
        return $data;
59
60
    }
61
62
    /**
63
     * @param $data
64
     * @param string $toEncoding
65
     * @param string $fromEncoding
66
     * @return array|false|string|string[]|null
67
     */
68
    public function cleanse($data, $toEncoding = 'utf-8', $fromEncoding = 'auto')
69
    {
70
71
        if (is_array($data)) {
72
            foreach ($data as $key => $value) {
73
                $data[$key] = $this->cleanse($value, $toEncoding, $fromEncoding);
74
            }
75
            return $data;
76
        }
77
78
        $this->input = $data;
79
        $data = $this->strings->clean($data);
80
        $data = mb_convert_encoding($data, $toEncoding, $fromEncoding);
81
        $data = htmlspecialchars_decode($data);
82
        $data = $this->strings->clean($data);
83
        if ($this->input != $data) {
84
            $this->_sanitised = true;
85
        }
86
        $this->output = $data;
87
        return $data;
88
    }
89
90
    /**
91
     * @param $string
92
     * @param string $delimiter
93
     * @return string
94
     */
95
    public function cleanseCsv($string, $delimiter = "|")
96
    {
97
        return trim(str_replace([$delimiter, "\n", "\r", "\t"], " ", $string));
98
    }
99
100
    /**
101
     * @param $data
102
     * @param string $type
103
     * @param int $stringLength
104
     * @return mixed|string
105
     */
106 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...
107
    {
108
109 1
        $this->_sanitised = null;
110
111 1
        if (is_array($data)) {
112
            foreach ($data as $key => $value) {
113
                $data[$key] = $this->disinfect($value, $type, $stringLength);
114
            }
115
            return $data;
116
        }
117
118 1
        $this->input = $data;
119
120 1
        $data = $this->strings->clean($data);
121 1
        $data = $this->strings->stringLength($data, $stringLength);
122
123 1
        switch ($type) {
124 1
            case "email":
125 1
                $filterResult = $this->filters->filterEmail($data);
126 1
                break;
127
128
            case "encoded":
129
                $filterResult = $this->filters->filterEncoded($data);
130
                break;
131
132
            case "number_float":
133
            case "float":
134
                $filterResult = $this->filters->filterFloat($data);
135
                break;
136
137
            case "number_int":
138
            case "int":
139
                $filterResult = $this->filters->filterInt($data);
140
                break;
141
142
            case "full_special_chars":
143
                $filterResult = $this->filters->filterFullSpecialChar($data);
144
                break;
145
146
            case "url":
147
                $filterResult = $this->filters->filterUrl($data);
148
                break;
149
150
            case "string":
151
                $filterResult = $this->filters->filterString($data);
152
                break;
153
154
            default:
155
            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...
156
                $filterResult = $this->filters->filterSpecial($data);
157
                break;
158
        }
159
160 1
        if ($this->input != $filterResult->getResult()) {
161
            $this->_sanitised = true;
162
        }
163
164 1
        $this->is_valid = $filterResult->isValid();
165 1
        $this->output = $filterResult->getResult();
166 1
        return $this->output;
167
    }
168
169
170
    /**
171
     * @return null
172
     */
173
    public function isSanitised()
174
    {
175
        return $this->_sanitised;
176
    }
177
178
    /**
179
     * Returns true if the data is valid
180
     * @return null
181
     */
182
    public function isValid()
183
    {
184
        return $this->is_valid;
185
    }
186
187 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...
188
    {
189 1
        $valid = false;
190 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...
191 1
            $valid = true;
192
        }
193 1
        return new SoteriaModel($this->input, $this->output, $valid);
194
    }
195
196
}