Completed
Branch master (2e1e23)
by Grzegorz
02:52 queued 28s
created

Validation   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 135
Duplicated Lines 20.74 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 22
c 6
b 1
f 1
lcom 1
cbo 2
dl 28
loc 135
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A validFieldList() 0 14 4
B validation() 0 17 6
A validNumber() 0 8 2
A validFilter() 14 14 4
A validSort() 14 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ComicVine\Api;
4
5
use ComicVine\Api\Filters\FilterCheck;
6
use ComicVine\Api\Filters\FilterValidation;
7
8
/**
9
 * Check validation of inputs for ControllerQuery.
10
 *
11
 * Class Validation
12
 *
13
 * @package grzgajda/comicvine-api
14
 * @author  Grzegorz Gajda <[email protected]>
15
 */
16
class Validation
17
{
18
    use FilterCheck;
19
    use FilterValidation;
20
21
    /**
22
     * Mock for enabled filters.
23
     *
24
     * @var array
25
     */
26
    protected $enabledFilters = [];
27
28
    /**
29
     * Validation constructor.
30
     *
31
     * @param array $filters
32
     */
33
    public function __construct($filters = [])
34
    {
35
        if ($filters !== []) {
36
            $this->enabledFilters = $filters;
37
        }
38
    }
39
40
    /**
41
     * Check validation for $input
42
     *
43
     * @param string       $type
44
     * @param string|array $input
45
     *
46
     * @return boolean|null
47
     */
48
    public function validation($type = "", $input)
49
    {
50
        switch ($type) {
51
            case 'field_list':
52
                return $this->validFieldList($input);
53
            case 'limit':
54
                return $this->validNumber('limit', $input, 0, 100);
55
            case 'offset':
56
                return $this->validNumber('offset', $input, 0);
57
            case 'filter':
58
                return $this->validFilter($input);
59
            case 'sort':
60
                return $this->validSort($input);
61
            default:
62
                return false;
63
        }
64
    }
65
66
    /**
67
     * Validation for FIELD_LIST parameter.
68
     *
69
     * @param string|array $input
70
     *
71
     * @return bool
72
     */
73
    protected function validFieldList($input)
74
    {
75
        if (is_array($input) === false) {
76
            return false;
77
        }
78
79
        foreach ($input as $key => $value) {
80
            if ($this->isKeyAndValueAre($key, 'int', $value, 'string') === false) {
81
                return false;
82
            }
83
        }
84
85
        return true;
86
    }
87
88
    /**
89
     * Check if offset or limit is valid.
90
     *
91
     * @param string       $type  Type of valid (offset or limit)
92
     * @param string|array $input Value
93
     * @param integer      $min   Min range what value can be
94
     * @param integer      $max   Max range what value can be
95
     *
96
     * @return $this|bool
97
     */
98
    protected function validNumber($type, $input, $min, $max = "")
99
    {
100
        if ($this->isIntAndBetween($input, $min, $max) === false) {
101
            return false;
102
        }
103
104
        return $this->isEnabledFilter($type, $this->enabledFilters);
105
    }
106
107
    /**
108
     * Validation for FILTER parameter.
109
     *
110
     * @param string|array $input
111
     *
112
     * @return boolean|null
113
     */
114 View Code Duplication
    protected function validFilter($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
115
    {
116
        if (is_array($input) === false) {
117
            return false;
118
        }
119
120
        foreach ($input as $key => $value) {
121
            if ($this->isKeyAndValueAre($key, 'string', $value, ['string', 'int', 'float']) === false) {
122
                return false;
123
            }
124
        }
125
126
        return $this->isEnabledFilter('filter', $this->enabledFilters);
127
    }
128
129
    /**
130
     * Validation for SORT parameter.
131
     *
132
     * @param string|array $input
133
     *
134
     * @return boolean|null
135
     */
136 View Code Duplication
    protected function validSort($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
137
    {
138
        if (is_array($input) === false) {
139
            return false;
140
        }
141
142
        foreach ($input as $key => $value) {
143
            if ($this->isParamAValue($key, 'string', $value, ['asc', 'desc']) === false) {
144
                return false;
145
            }
146
        }
147
148
        return $this->isEnabledFilter('sort', $this->enabledFilters);
149
    }
150
}