Completed
Push — master ( 2870b8...7116d1 )
by Tomas
07:37
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 23
CRAP Score 15.3457

Importance

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

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 6
            return true;
100
        }
101
102 18
        $value = $this->getValue();
103 18
        if ($this->availableValues !== null) {
104 9
            if (is_array($this->availableValues)) {
105 6
                return in_array($value, $this->availableValues);
106
            }
107 3
        }
108
109 15
        if ($this->required) {
110 15
            if ($value === null || $value == '') {
111 6
                return false;
112
            }
113 9
            if (is_string($this->availableValues)) {
114 3
                return $value == $this->availableValues;
115
            }
116 6
        }
117 6
        return true;
118
    }
119
120
    /**
121
     * Process environment variables like POST|GET|etc.. and return actual value
122
     *
123
     * @return mixed
124
     *
125
     * @throws Exception if actual InputParam has unsupported type
126
     */
127 36
    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 36 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 15
            if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) {
131 6
                return $_GET[$this->key];
132
            }
133 9
            if ($this->isMulti()) {
134
                return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
135
            }
136 9
            return filter_input(INPUT_GET, $this->key);
137
        }
138 30 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 12
            if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) {
140 6
                return $_POST[$this->key];
141
            }
142 9
            if ($this->isMulti()) {
143
                return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
144
            }
145 9
            return filter_input(INPUT_POST, $this->key);
146
        }
147 18
        if ($this->type == self::TYPE_FILE) {
148 9
            if (isset($_FILES[$this->key])) {
149 6
                if ($this->isMulti()) {
150 3
                    return $this->processMultiFileUploads($_FILES[$this->key]);
151
                } else {
152 3
                    return $_FILES[$this->key];
153
                }
154
            }
155 3
            return null;
156
        }
157 9
        if ($this->type == self::TYPE_COOKIE) {
158 3
            if (isset($_COOKIE[$this->key])) {
159 3
                return $_COOKIE[$this->key];
160
            }
161
        }
162 6
        if ($this->type == self::TYPE_POST_RAW) {
163 3
            return file_get_contents("php://input");
164
        }
165
166 3
        throw new Exception('Invalid type');
167
    }
168
169 3
    private function processMultiFileUploads($files)
170
    {
171 3
        $result = [];
172 3
        foreach ($files as $key => $values) {
173 3
            foreach ($values as $index => $value) {
174 3
                $result[$index][$key] = $value;
175 3
            }
176 3
        }
177 3
        return $result;
178
    }
179
}
180