Validator   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 170
Duplicated Lines 14.12 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 98.28%

Importance

Changes 0
Metric Value
wmc 30
c 0
b 0
f 0
lcom 1
cbo 2
dl 24
loc 170
ccs 57
cts 58
cp 0.9828
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateCountry() 0 5 2
B validateCase() 0 16 5
A validateType() 0 11 3
A isFull() 0 4 1
A isAbbr() 0 4 1
B getStateAbbreviations() 12 12 5
B getStateNames() 12 12 5
B validate() 0 10 7

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 Tylercd100\Validator\State;
4
5
use Illuminate\Support\Str;
6
7
class Validator
8
{
9
    protected $params;
10
11
    protected $states = [
12
        "usa" => [
13
            ["abbr" => 'AL', "name" => 'Alabama'],
14
            ["abbr" => 'AK', "name" => 'Alaska'],
15
            ["abbr" => 'AZ', "name" => 'Arizona'],
16
            ["abbr" => 'AR', "name" => 'Arkansas'],
17
            ["abbr" => 'CA', "name" => 'California'],
18
            ["abbr" => 'CO', "name" => 'Colorado'],
19
            ["abbr" => 'CT', "name" => 'Connecticut'],
20
            ["abbr" => 'DC', "name" => 'District Of Columbia'],
21
            ["abbr" => 'DE', "name" => 'Delaware'],
22
            ["abbr" => 'FL', "name" => 'Florida'],
23
            ["abbr" => 'GA', "name" => 'Georgia'],
24
            ["abbr" => 'HI', "name" => 'Hawaii'],
25
            ["abbr" => 'ID', "name" => 'Idaho'],
26
            ["abbr" => 'IL', "name" => 'Illinois'],
27
            ["abbr" => 'IN', "name" => 'Indiana'],
28
            ["abbr" => 'IA', "name" => 'Iowa'],
29
            ["abbr" => 'KS', "name" => 'Kansas'],
30
            ["abbr" => 'KY', "name" => 'Kentucky'],
31
            ["abbr" => 'LA', "name" => 'Louisiana'],
32
            ["abbr" => 'ME', "name" => 'Maine'],
33
            ["abbr" => 'MD', "name" => 'Maryland'],
34
            ["abbr" => 'MA', "name" => 'Massachusetts'],
35
            ["abbr" => 'MI', "name" => 'Michigan'],
36
            ["abbr" => 'MN', "name" => 'Minnesota'],
37
            ["abbr" => 'MS', "name" => 'Mississippi'],
38
            ["abbr" => 'MO', "name" => 'Missouri'],
39
            ["abbr" => 'MT', "name" => 'Montana'],
40
            ["abbr" => 'NE', "name" => 'Nebraska'],
41
            ["abbr" => 'NV', "name" => 'Nevada'],
42
            ["abbr" => 'NH', "name" => 'New Hampshire'],
43
            ["abbr" => 'NJ', "name" => 'New Jersey'],
44
            ["abbr" => 'NM', "name" => 'New Mexico'],
45
            ["abbr" => 'NY', "name" => 'New York'],
46
            ["abbr" => 'NC', "name" => 'North Carolina'],
47
            ["abbr" => 'ND', "name" => 'North Dakota'],
48
            ["abbr" => 'OH', "name" => 'Ohio'],
49
            ["abbr" => 'OK', "name" => 'Oklahoma'],
50
            ["abbr" => 'OR', "name" => 'Oregon'],
51
            ["abbr" => 'PA', "name" => 'Pennsylvania'],
52
            ["abbr" => 'RI', "name" => 'Rhode Island'],
53
            ["abbr" => 'SC', "name" => 'South Carolina'],
54
            ["abbr" => 'SD', "name" => 'South Dakota'],
55
            ["abbr" => 'TN', "name" => 'Tennessee'],
56
            ["abbr" => 'TX', "name" => 'Texas'],
57
            ["abbr" => 'UT', "name" => 'Utah'],
58
            ["abbr" => 'VT', "name" => 'Vermont'],
59
            ["abbr" => 'VA', "name" => 'Virginia'],
60
            ["abbr" => 'WA', "name" => 'Washington'],
61
            ["abbr" => 'WV', "name" => 'West Virginia'],
62
            ["abbr" => 'WI', "name" => 'Wisconsin'],
63
            ["abbr" => 'WY', "name" => 'Wyoming'],
64
            ["abbr" => 'AS', "name" => 'American Samoa'],
65
            ["abbr" => 'FM', "name" => 'Federated States Of Micronesia'],
66
            ["abbr" => 'GU', "name" => 'Guam'],
67
            ["abbr" => 'MH', "name" => 'Marshall Islands'],
68
            ["abbr" => 'MP', "name" => 'Northern Mariana Islands'],
69
            ["abbr" => 'PW', "name" => 'Pala'],
70
            ["abbr" => 'PR', "name" => 'Puerto Rico'],
71
            ["abbr" => 'VI', "name" => 'Virgin Islands']
72
        ],
73
        "canada" => [
74
            ["abbr" => 'AB', "name" => 'Alberta'],
75
            ["abbr" => 'BC', "name" => 'British Columbia'],
76
            ["abbr" => 'MB', "name" => 'Manitoba'],
77
            ["abbr" => 'NB', "name" => 'New Brunswick'],
78
            ["abbr" => 'NL', "name" => 'Newfoundland And Labrador'],
79
            ["abbr" => 'NS', "name" => 'Nova Scotia'],
80
            ["abbr" => 'NT', "name" => 'Northwest Territories'],
81
            ["abbr" => 'NU', "name" => 'Nunavut'],
82
            ["abbr" => 'ON', "name" => 'Ontario'],
83
            ["abbr" => 'PE', "name" => 'Prince Edward Island'],
84
            ["abbr" => 'QC', "name" => 'Quebec'],
85
            ["abbr" => 'SK', "name" => 'Saskatchewan'],
86
            ["abbr" => 'YT', "name" => 'Yukon'],
87
        ],
88
    ];
89
90 30
    public function __construct(Parameters $params)
91
    {
92 30
        $this->params = $params;
93 30
    }
94
95 27
    public function validate($value)
96
    {
97 27
        return $value === null || $value === [] || (
98 27
            is_string($value) &&
99 27
            Str::length($value) > 0 &&
100 27
            $this->validateCountry($value) &&
101 27
            $this->validateCase($value) &&
102 24
            $this->validateType($value)
103 27
        );
104
    }
105
106 24
    protected function validateCountry($value)
107
    {
108 24
        $country = $this->params->getCountry();
109 24
        return $this->isAbbr($value, $country) || $this->isFull($value, $country);
110
    }
111
112 24
    protected function validateCase($value)
113
    {
114 24
        switch ($this->params->getCase()) {
115 24
            case 'lower':
116 3
                return $value === Str::lower($value);
117
118 21
            case 'upper':
119 9
                return $value === Str::upper($value);
120
121 12
            case 'title':
122 3
                return $this->isAbbr($value) ? $value === Str::upper($value) : $value === Str::title($value);
123
124 9
            default:
125 9
                return true;
126 9
        }
127
    }
128
129 24
    protected function validateType($value)
130
    {
131 24
        switch ($this->params->getType()) {
132 24
            case 'abbr':
133 6
                return $this->isAbbr($value);
134 18
            case 'full':
135
                return $this->isFull($value);
136 18
            default:
137 18
                return true;
138 18
        }
139
    }
140
141 24
    protected function isFull($value, $country = null)
142
    {
143 24
        return in_array(Str::title($value), $this->getStateNames($country));
144
    }
145
146 24
    protected function isAbbr($value, $country = null)
147
    {
148 24
        return in_array(Str::upper($value), $this->getStateAbbreviations($country));
149
    }
150
151 24 View Code Duplication
    protected function getStateAbbreviations($country = null)
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...
152
    {
153 24
        $x = [];
154 24
        foreach ($this->states as $c => $states) {
155 24
            if ($country === null || $c === $country) {
156 24
                foreach ($states as $state) {
157 24
                    $x[] = $state['abbr'];
158 24
                }
159 24
            }
160 24
        }
161 24
        return $x;
162
    }
163
164 24 View Code Duplication
    protected function getStateNames($country = null)
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...
165
    {
166 24
        $x = [];
167 24
        foreach ($this->states as $c => $states) {
168 24
            if ($country === null || $c === $country) {
169 24
                foreach ($states as $state) {
170 24
                    $x[] = $state['name'];
171 24
                }
172 24
            }
173 24
        }
174 24
        return $x;
175
    }
176
}
177