Completed
Pull Request — master (#145)
by Graham
09:30
created

PhoneNumberDesc   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.19%

Importance

Changes 0
Metric Value
wmc 42
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 282
rs 8.295
ccs 101
cts 105
cp 0.9619

26 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 10 1
A getPossibleLength() 0 4 1
A setPossibleLength() 0 4 1
A __construct() 0 4 1
A clearPossibleLength() 0 4 1
A getPossibleLengthLocalOnly() 0 4 1
A setPossibleLengthLocalOnly() 0 4 1
A clearPossibleLengthLocalOnly() 0 4 1
A getNationalNumberPattern() 0 4 1
A setNationalNumberPattern() 0 7 1
A addPossibleLength() 0 6 2
A addPossibleLengthLocalOnly() 0 6 2
A hasNationalNumberPattern() 0 4 1
A clearNationalNumberPattern() 0 6 1
A hasPossibleNumberPattern() 0 4 1
A getPossibleNumberPattern() 0 4 1
A clearPossibleNumberPattern() 0 6 1
A setPossibleNumberPattern() 0 7 1
A hasExampleNumber() 0 4 1
A getExampleNumber() 0 4 1
A setExampleNumber() 0 7 1
A clearExampleNumber() 0 7 1
A mergeFrom() 0 16 4
A exactlySameAs() 0 6 3
A toArray() 0 18 4
B fromArray() 0 16 7

How to fix   Complexity   

Complex Class

Complex classes like PhoneNumberDesc often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PhoneNumberDesc, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace libphonenumber;
4
5
/**
6
 * Phone Number Description
7
 */
8
class PhoneNumberDesc
9
{
10
    protected $hasNationalNumberPattern = false;
11
    protected $nationalNumberPattern = "";
12
    protected $hasPossibleNumberPattern = false;
13
    protected $possibleNumberPattern = "";
14
    protected $hasExampleNumber = false;
15
    protected $exampleNumber = "";
16
    /**
17
     * @var array
18
     */
19
    protected $possibleLength;
20
    /**
21
     * @var array
22
     */
23
    protected $possibleLengthLocalOnly;
24
25 1376
    public function __construct()
26
    {
27 1376
        $this->clear();
28 1376
    }
29
30
    /**
31
     * @return PhoneNumberDesc
32
     */
33 1376
    public function clear()
34
    {
35 1376
        $this->clearNationalNumberPattern();
36 1376
        $this->clearPossibleNumberPattern();
37 1376
        $this->clearPossibleLength();
38 1376
        $this->clearPossibleLengthLocalOnly();
39 1376
        $this->clearExampleNumber();
40
41 1376
        return $this;
42
    }
43
44
    /**
45
     * @return array
46
     */
47 3280
    public function getPossibleLength()
48
    {
49 3280
        return $this->possibleLength;
50
    }
51
52
    /**
53
     * @param array $possibleLength
54
     */
55 1367
    public function setPossibleLength($possibleLength)
56
    {
57 1367
        $this->possibleLength = $possibleLength;
58 1367
    }
59
60 8
    public function addPossibleLength($possibleLength)
61
    {
62 8
        if (!in_array($possibleLength, $this->possibleLength)) {
63 8
            $this->possibleLength[] = $possibleLength;
64
        }
65 8
    }
66
67 1376
    public function clearPossibleLength()
68
    {
69 1376
        $this->possibleLength = array();
70 1376
    }
71
72
    /**
73
     * @return array
74
     */
75 3232
    public function getPossibleLengthLocalOnly()
76
    {
77 3232
        return $this->possibleLengthLocalOnly;
78
    }
79
80
    /**
81
     * @param array $possibleLengthLocalOnly
82
     */
83 1352
    public function setPossibleLengthLocalOnly($possibleLengthLocalOnly)
84
    {
85 1352
        $this->possibleLengthLocalOnly = $possibleLengthLocalOnly;
86 1352
    }
87
88 4
    public function addPossibleLengthLocalOnly($possibleLengthLocalOnly)
89
    {
90 4
        if (!in_array($possibleLengthLocalOnly, $this->possibleLengthLocalOnly)) {
91 4
            $this->possibleLengthLocalOnly[] = $possibleLengthLocalOnly;
92
        }
93 4
    }
94
95 1376
    public function clearPossibleLengthLocalOnly()
96
    {
97 1376
        $this->possibleLengthLocalOnly = array();
98 1376
    }
99
100
    /**
101
     * @return boolean
102
     */
103 485
    public function hasNationalNumberPattern()
104
    {
105 485
        return $this->hasNationalNumberPattern;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 2933
    public function getNationalNumberPattern()
112
    {
113 2933
        return $this->nationalNumberPattern;
114
    }
115
116
    /**
117
     * @param string $value
118
     * @return PhoneNumberDesc
119
     */
120 1358
    public function setNationalNumberPattern($value)
121
    {
122 1358
        $this->hasNationalNumberPattern = true;
123 1358
        $this->nationalNumberPattern = $value;
124
125 1358
        return $this;
126
    }
127
128
    /**
129
     * @return PhoneNumberDesc
130
     */
131 1376
    public function clearNationalNumberPattern()
132
    {
133 1376
        $this->hasNationalNumberPattern = false;
134 1376
        $this->nationalNumberPattern = '';
135 1376
        return $this;
136
    }
137
138
    /**
139
     * @return boolean
140
     */
141 485
    public function hasPossibleNumberPattern()
142
    {
143 485
        return $this->hasPossibleNumberPattern;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 506
    public function getPossibleNumberPattern()
150
    {
151 506
        return $this->possibleNumberPattern;
152
    }
153
154
    /**
155
     * @return PhoneNumberDesc
156
     */
157 1376
    public function clearPossibleNumberPattern()
158
    {
159 1376
        $this->hasPossibleNumberPattern = false;
160 1376
        $this->possibleNumberPattern = '';
161 1376
        return $this;
162
    }
163
164
    /**
165
     * @param string $value
166
     * @return PhoneNumberDesc
167
     */
168 1357
    public function setPossibleNumberPattern($value)
169
    {
170 1357
        $this->hasPossibleNumberPattern = true;
171 1357
        $this->possibleNumberPattern = $value;
172
173 1357
        return $this;
174
    }
175
176
    /**
177
     * @return string
178
     */
179 5345
    public function hasExampleNumber()
180
    {
181 5345
        return $this->hasExampleNumber;
182
    }
183
184
    /**
185
     * @return string
186
     */
187 3182
    public function getExampleNumber()
188
    {
189 3182
        return $this->exampleNumber;
190
    }
191
192
    /**
193
     * @param string $value
194
     * @return PhoneNumberDesc
195
     */
196 1334
    public function setExampleNumber($value)
197
    {
198 1334
        $this->hasExampleNumber = true;
199 1334
        $this->exampleNumber = $value;
200
201 1334
        return $this;
202
    }
203
204
    /**
205
     * @return PhoneNumberDesc
206
     */
207 1376
    public function clearExampleNumber()
208
    {
209 1376
        $this->hasExampleNumber = false;
210 1376
        $this->exampleNumber = '';
211
212 1376
        return $this;
213
    }
214
215
    /**
216
     * @param PhoneNumberDesc $other
217
     * @return PhoneNumberDesc
218
     */
219 3
    public function mergeFrom(PhoneNumberDesc $other)
220
    {
221 3
        if ($other->hasNationalNumberPattern()) {
222 3
            $this->setNationalNumberPattern($other->getNationalNumberPattern());
223
        }
224 3
        if ($other->hasPossibleNumberPattern()) {
225 3
            $this->setPossibleNumberPattern($other->getPossibleNumberPattern());
226
        }
227 3
        if ($other->hasExampleNumber()) {
228 3
            $this->setExampleNumber($other->getExampleNumber());
229
        }
230 3
        $this->setPossibleLength($other->getPossibleLength());
231 3
        $this->setPossibleLengthLocalOnly($other->getPossibleLengthLocalOnly());
232
233 3
        return $this;
234
    }
235
236
    /**
237
     * @param PhoneNumberDesc $other
238
     * @return boolean
239
     */
240
    public function exactlySameAs(PhoneNumberDesc $other)
241
    {
242
        return $this->nationalNumberPattern === $other->nationalNumberPattern &&
243
        $this->possibleNumberPattern === $other->possibleNumberPattern &&
244
        $this->exampleNumber === $other->exampleNumber;
245
    }
246
247
    /**
248
     * @return array
249
     */
250 482
    public function toArray()
251
    {
252 482
        $data = array();
253 482
        if ($this->hasNationalNumberPattern()) {
254 482
            $data['NationalNumberPattern'] = $this->getNationalNumberPattern();
255
        }
256 482
        if ($this->hasPossibleNumberPattern()) {
257 482
            $data['PossibleNumberPattern'] = $this->getPossibleNumberPattern();
258
        }
259 482
        if ($this->hasExampleNumber()) {
260 482
            $data['ExampleNumber'] = $this->getExampleNumber();
261
        }
262
263 482
        $data['PossibleLength'] = $this->getPossibleLength();
264 482
        $data['PossibleLengthLocalOnly'] = $this->getPossibleLengthLocalOnly();
265
266 482
        return $data;
267
    }
268
269
    /**
270
     * @param array $input
271
     * @return PhoneNumberDesc
272
     */
273 1348
    public function fromArray(array $input)
274
    {
275 1348
        if (isset($input['NationalNumberPattern']) && $input['NationalNumberPattern'] != '') {
276 1348
            $this->setNationalNumberPattern($input['NationalNumberPattern']);
277
        }
278 1348
        if (isset($input['PossibleNumberPattern']) && $input['NationalNumberPattern'] != '') {
279 1348
            $this->setPossibleNumberPattern($input['PossibleNumberPattern']);
280
        }
281 1348
        if (isset($input['ExampleNumber']) && $input['NationalNumberPattern'] != '') {
282 1330
            $this->setExampleNumber($input['ExampleNumber']);
283
        }
284 1348
        $this->setPossibleLength($input['PossibleLength']);
285 1348
        $this->setPossibleLengthLocalOnly($input['PossibleLengthLocalOnly']);
286
287 1348
        return $this;
288
    }
289
}
290