Completed
Push — master ( ccd5f6...55e22e )
by Tyler
03:42 queued 02:32
created

Base   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 165
Duplicated Lines 14.55 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 24
loc 165
ccs 32
cts 33
cp 0.9697
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A isFull() 0 4 1
A isAbbr() 0 4 1
A getSubject() 0 12 3
B getStateAbbreviations() 12 12 5
B getStateNames() 12 12 5
passes() 0 1 ?
message() 0 1 ?

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 LVR\State;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\Validator;
7
use Illuminate\Support\Str;
8
9
abstract class Base implements Rule
10
{
11
    protected $country;
12
    protected $subject;
13
14 9
    function __construct(string $country = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

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