Completed
Push — master ( 9a1fcc...2870b8 )
by Tomas
7s
created

InputParam::getValue()   C

Complexity

Conditions 15
Paths 14

Size

Total Lines 41
Code Lines 26

Duplication

Lines 18
Ratio 43.9 %

Code Coverage

Tests 22
CRAP Score 15.3888

Importance

Changes 11
Bugs 4 Features 5
Metric Value
c 11
b 4
f 5
dl 18
loc 41
ccs 22
cts 25
cp 0.88
rs 5.0504
cc 15
eloc 26
nc 14
nop 0
crap 15.3888

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tomaj\NetteApi\Params;
4
5
use Exception;
6
7
class InputParam implements ParamInterface
8
{
9
    const TYPE_POST      = 'POST';
10
    const TYPE_GET       = 'GET';
11
    const TYPE_FILE      = 'FILE';
12
    const TYPE_COOKIE    = 'COOKIE';
13
    const TYPE_POST_RAW  = 'POST_RAW';
14
15
    const OPTIONAL = false;
16
    const REQUIRED = true;
17
18
    /**
19
     * @var string
20
     */
21
    private $type;
22
23
    /**
24
     * @var string
25
     */
26
    private $key;
27
28
    /**
29
     * @var bool
30
     */
31
    private $required;
32
33
    /**
34
     * @var array|null
35
     */
36
    private $availableValues;
37
38
    /**
39
     * @var bool
40
     */
41
    private $multi;
42
43 45
    public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false)
44
    {
45 45
        $this->type = $type;
46 45
        $this->key = $key;
47 45
        $this->required = $required;
48 45
        $this->availableValues = $availableValues;
49 45
        $this->multi = $multi;
50 45
    }
51
52
    /**
53
     * @return string
54
     */
55 9
    public function getType()
56
    {
57 9
        return $this->type;
58
    }
59
60
    /**
61
     * @return string
62
     */
63 21
    public function getKey()
64
    {
65 21
        return $this->key;
66
    }
67
68
    /**
69
     * @return boolean
70
     */
71 6
    public function isRequired()
72
    {
73 6
        return $this->required;
74
    }
75
76 6
    public function getAvailableValues()
77
    {
78 6
        return $this->availableValues;
79
    }
80
81
    /**
82
     * @return bool
83
     */
84 21
    public function isMulti()
85
    {
86 21
        return $this->multi;
87
    }
88
89
    /**
90
     * Check if actual value from environment is valid
91
     *
92
     * @return bool
93
     *
94
     * @throws Exception if actual InputParam has unsupported type
95
     */
96 18
    public function isValid()
97
    {
98 18
        if ($this->required == self::OPTIONAL) {
99 18
            return true;
100 9
        }
101 6
102
        $value = $this->getValue();
103 3
        if ($this->availableValues !== null) {
104
            if (is_array($this->availableValues)) {
105 15
                return in_array($value, $this->availableValues);
106 15
            }
107 6
        }
108
109 9
        if ($this->required) {
110 3
            if ($value === null || $value == '') {
111
                return false;
112 6
            }
113 6
            if (is_string($this->availableValues)) {
114
                return $value == $this->availableValues;
115
            }
116
        }
117
        return true;
118
    }
119
120
    /**
121
     * Process environment variables like POST|GET|etc.. and return actual value
122
     *
123 36
     * @return mixed
124
     *
125 36
     * @throws Exception if actual InputParam has unsupported type
126 15
     */
127 6
    public function getValue()
0 ignored issues
show
Coding Style introduced by
getValue uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getValue uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getValue uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getValue uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
128
    {
129 9 View Code Duplication
        if ($this->type == self::TYPE_GET) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
130
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
131
                return $_GET[$this->key];
132 9
            }
133
            if ($this->isMulti()) {
134 30
                return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
135 12
            }
136 6
            return filter_input(INPUT_GET, $this->key);
137
        }
138 9 View Code Duplication
        if ($this->type == self::TYPE_POST) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
139
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
140
                return $_POST[$this->key];
141 9
            }
142
            if ($this->isMulti()) {
143 18
                return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
144 9
            }
145 6
            return filter_input(INPUT_POST, $this->key);
146 3
        }
147
        if ($this->type == self::TYPE_FILE) {
148 3
            if (isset($_FILES[$this->key])) {
149
                if ($this->isMulti()) {
150
                    return $this->processMultiFileUploads($_FILES[$this->key]);
151 3
                } else {
152
                    return $_FILES[$this->key];
153 9
                }
154 3
            }
155 3
            return null;
156
        }
157
        if ($this->type == self::TYPE_COOKIE) {
158 6
            if (isset($_COOKIE[$this->key])) {
159 3
                return $_COOKIE[$this->key];
160
            }
161
        }
162 3
        if ($this->type == self::TYPE_POST_RAW) {
163
            return file_get_contents("php://input");
164
        }
165 3
166
        throw new Exception('Invalid type');
167 3
    }
168 3
169 3
    private function processMultiFileUploads($files)
170 3
    {
171 3
        $result = [];
172 3
        foreach ($files as $key => $values) {
173 3
            foreach ($values as $index => $value) {
174
                $result[$index][$key] = $value;
175
            }
176
        }
177
        return $result;
178
    }
179
}
180