InputSanitizer   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 0
dl 0
loc 100
ccs 52
cts 62
cp 0.8387
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C sanitize() 0 48 14
A sanitizeAll() 0 15 3
A isJson() 0 9 2
1
<?php
2
3
/**
4
 * Part of the InputSanitizer package.
5
 *
6
 * @package        InputSanitizer
7
 * @version        1.0.2
8
 * @author         Arthur Lorent <[email protected]>, Daniel Lucas <[email protected]>
9
 * @license        MIT
10
 * @copyright  (c) 2006-2017, ACID-Solutions SARL
11
 * @link           https://acid.fr
12
 */
13
14
namespace AcidSolutions\InputSanitizer;
15
16
class InputSanitizer
17
{
18
    /**
19
     * Cleans given input and returns cleaned data.
20
     *
21
     * @param mixed $entry           The data to clean
22
     * @param mixed $default         Value to return by default if the input data is falsy
23
     * @param bool  $jsonDecodeAssoc Should json_decode return an associative array instead of StdClass?
24
     *
25
     * @return mixed
26
     */
27 22
    public function sanitize($entry, $default = null, $jsonDecodeAssoc = false)
28
    {
29
        // we remove useless spaces
30 22
        if (is_string($entry)) {
31 18
            $entry = trim($entry);
32 18
        }
33
        // we sanitize the value
34 22
        switch (true) {
35 22
            case $entry === '':
36 22
            case $entry === 'null':
37 10
                $return = null;
38 10
                break;
39 18
            case $entry === 'false':
40 6
                $return = false;
41 6
                break;
42 18
            case $entry === 'true':
43 18
            case $entry === 'on':
44 8
                $return = true;
45 8
                break;
46 16
            case is_numeric($entry):
47 6
                if (((int) $entry != $entry)) {
48 2
                    $return = doubleval($entry);
49 2
                } else {
50 6
                    $return = intval($entry);
51
                }
52 6
                break;
53 14
            case $this->isJson($entry):
54 4
                $return = json_decode($entry, $jsonDecodeAssoc);
55
56 4
                if (is_array($return)) {
57 2
                    $return = (array) $this->sanitizeAll($return);
58 2
                }
59
60 4
                break;
61 12
            case is_array($entry):
62 2
                $return = $this->sanitizeAll($entry);
63 2
                break;
64 12
            default:
65 12
                $return = $entry;
66 12
                break;
67 22
        };
68
        // if the value is null or false, return the default value
69 22
        if (isset($default) && !$return) {
70 4
            return $default;
71
        }
72
73 20
        return $return;
74
    }
75
76
    /**
77
     * Recursive method to sanitize deep array of data.
78
     *
79
     * @param array $entries Array of data to sanitize
80
     *
81
     * @return array
82
     */
83 4
    private function sanitizeAll(array $entries)
84
    {
85 4
        $return = [];
86
        // for each entry contained into the array
87 4
        foreach ($entries as $key => $entry) {
88
            // we sanitize it
89 4
            if (is_array($entry)) {
90 4
                $return[$key] = $this->sanitizeAll($entry);
91 4
            } else {
92 4
                $return[$key] = $this->sanitize($entry);
93
            }
94 4
        }
95
96 4
        return $return;
97
    }
98
99
    /**
100
     * Check if an item is JSON.
101
     *
102
     * @param $string Item to check
103
     *
104
     * @return bool True if the given item is JSON
105
     */
106 14
    private function isJson($string)
107
    {
108 14
        if (!is_string($string)) {
109 12
            return false;
110
        }
111 6
        json_decode($string);
112
113 6
        return (json_last_error() == JSON_ERROR_NONE);
114
    }
115
}
116