NumberFormat   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 343
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 91
dl 0
loc 343
ccs 106
cts 106
cp 1
rs 9.2
c 0
b 0
f 0
wmc 40

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getDomesticCarrierCodeFormattingRule() 0 3 1
A clearNationalPrefixFormattingRule() 0 5 1
A getFormat() 0 3 1
A hasNationalPrefixFormattingRule() 0 3 1
A hasFormat() 0 3 1
A getLeadingDigitsPattern() 0 3 1
A hasDomesticCarrierCodeFormattingRule() 0 3 1
A __construct() 0 3 1
A leadingDigitsPatternSize() 0 3 1
A setFormat() 0 6 1
A hasPattern() 0 3 1
A leadingDigitPatterns() 0 3 1
B mergeFrom() 0 23 7
A clear() 0 20 1
A setNationalPrefixOptionalWhenFormatting() 0 4 1
B fromArray() 0 17 7
A getNationalPrefixFormattingRule() 0 3 1
A hasNationalPrefixOptionalWhenFormatting() 0 3 1
A getPattern() 0 3 1
A setPattern() 0 6 1
A setNationalPrefixFormattingRule() 0 6 1
A toArray() 0 21 4
A addLeadingDigitsPattern() 0 5 1
A getNationalPrefixOptionalWhenFormatting() 0 3 1
A setDomesticCarrierCodeFormattingRule() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like NumberFormat 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.

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 NumberFormat, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace libphonenumber;
4
5
/**
6
 * Number Format
7
 */
8
class NumberFormat
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $pattern;
14
    /**
15
     * @var bool
16
     */
17
    protected $hasPattern = false;
18
19
    /**
20
     * @var string
21
     */
22
    protected $format;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $hasFormat = false;
28
29
    /**
30
     * @var array
31
     */
32
    protected $leadingDigitsPattern = array();
33
34
    /**
35
     * @var string
36
     */
37
    protected $nationalPrefixFormattingRule = '';
38
39
    /**
40
     * @var bool
41
     */
42
    protected $hasNationalPrefixFormattingRule = false;
43
    /**
44
     * @var bool
45
     */
46
    protected $nationalPrefixOptionalWhenFormatting = false;
47
48
    /**
49
     * @var bool
50
     */
51
    protected $hasNationalPrefixOptionalWhenFormatting = false;
52
53
    /**
54
     * @var string
55
     */
56
    protected $domesticCarrierCodeFormattingRule = '';
57
58
    /**
59
     * @var bool
60
     */
61
    protected $hasDomesticCarrierCodeFormattingRule = false;
62
63 1007
    public function __construct()
64
    {
65 1007
        $this->clear();
66 1007
    }
67
68
    /**
69
     * @return NumberFormat
70
     */
71 1007
    public function clear()
72
    {
73 1007
        $this->hasPattern = false;
74 1007
        $this->pattern = null;
75
76 1007
        $this->hasFormat = false;
77 1007
        $this->format = null;
78
79 1007
        $this->leadingDigitsPattern = array();
80
81 1007
        $this->hasNationalPrefixFormattingRule = false;
82 1007
        $this->nationalPrefixFormattingRule = '';
83
84 1007
        $this->hasNationalPrefixOptionalWhenFormatting = false;
85 1007
        $this->nationalPrefixOptionalWhenFormatting = false;
86
87 1007
        $this->hasDomesticCarrierCodeFormattingRule = false;
88 1007
        $this->domesticCarrierCodeFormattingRule = '';
89
90 1007
        return $this;
91
    }
92
93
    /**
94
     * @return boolean
95
     */
96 13
    public function hasPattern()
97
    {
98 13
        return $this->hasPattern;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 372
    public function getPattern()
105
    {
106 372
        return $this->pattern;
107
    }
108
109
    /**
110
     * @param string $value
111
     * @return NumberFormat
112
     */
113 1003
    public function setPattern($value)
114
    {
115 1003
        $this->hasPattern = true;
116 1003
        $this->pattern = $value;
117
118 1003
        return $this;
119
    }
120
121
    /**
122
     * @return boolean
123
     */
124 214
    public function hasNationalPrefixOptionalWhenFormatting()
125
    {
126 214
        return $this->hasNationalPrefixOptionalWhenFormatting;
127
    }
128
129
    /**
130
     * @return boolean
131
     */
132 265
    public function getNationalPrefixOptionalWhenFormatting()
133
    {
134 265
        return $this->nationalPrefixOptionalWhenFormatting;
135
    }
136
137
    /**
138
     * @param boolean $nationalPrefixOptionalWhenFormatting
139
     */
140 998
    public function setNationalPrefixOptionalWhenFormatting($nationalPrefixOptionalWhenFormatting)
141
    {
142 998
        $this->hasNationalPrefixOptionalWhenFormatting = true;
143 998
        $this->nationalPrefixOptionalWhenFormatting = $nationalPrefixOptionalWhenFormatting;
144 998
    }
145
146
    /**
147
     * @return boolean
148
     */
149 16
    public function hasFormat()
150
    {
151 16
        return $this->hasFormat;
152
    }
153
154
    /**
155
     * @return string
156
     */
157 345
    public function getFormat()
158
    {
159 345
        return $this->format;
160
    }
161
162
    /**
163
     * @param string $value
164
     * @return NumberFormat
165
     */
166 1003
    public function setFormat($value)
167
    {
168 1003
        $this->hasFormat = true;
169 1003
        $this->format = $value;
170
171 1003
        return $this;
172
    }
173
174
    /**
175
     * @return string[]
176
     */
177 202
    public function leadingDigitPatterns()
178
    {
179 202
        return $this->leadingDigitsPattern;
180
    }
181
182
    /**
183
     * @return int
184
     */
185 171
    public function leadingDigitsPatternSize()
186
    {
187 171
        return count($this->leadingDigitsPattern);
188
    }
189
190
    /**
191
     * @param int $index
192
     * @return string
193
     */
194 127
    public function getLeadingDigitsPattern($index)
195
    {
196 127
        return $this->leadingDigitsPattern[$index];
197
    }
198
199
    /**
200
     * @param string $value
201
     * @return NumberFormat
202
     */
203 881
    public function addLeadingDigitsPattern($value)
204
    {
205 881
        $this->leadingDigitsPattern[] = $value;
206
207 881
        return $this;
208
    }
209
210
    /**
211
     * @return boolean
212
     */
213 214
    public function hasNationalPrefixFormattingRule()
214
    {
215 214
        return $this->hasNationalPrefixFormattingRule;
216
    }
217
218
    /**
219
     * @return string
220
     */
221 273
    public function getNationalPrefixFormattingRule()
222
    {
223 273
        return $this->nationalPrefixFormattingRule;
224
    }
225
226
    /**
227
     * @param string $value
228
     * @return NumberFormat
229
     */
230 550
    public function setNationalPrefixFormattingRule($value)
231
    {
232 550
        $this->hasNationalPrefixFormattingRule = true;
233 550
        $this->nationalPrefixFormattingRule = (string)$value;
234
235 550
        return $this;
236
    }
237
238
    /**
239
     * @return NumberFormat
240
     */
241 2
    public function clearNationalPrefixFormattingRule()
242
    {
243 2
        $this->nationalPrefixFormattingRule = '';
244
245 2
        return $this;
246
    }
247
248
    /**
249
     * @return boolean
250
     */
251 219
    public function hasDomesticCarrierCodeFormattingRule()
252
    {
253 219
        return $this->hasDomesticCarrierCodeFormattingRule;
254
    }
255
256
    /**
257
     * @return string
258
     */
259 21
    public function getDomesticCarrierCodeFormattingRule()
260
    {
261 21
        return $this->domesticCarrierCodeFormattingRule;
262
    }
263
264
    /**
265
     * @param string $value
266
     * @return NumberFormat
267
     */
268 70
    public function setDomesticCarrierCodeFormattingRule($value)
269
    {
270 70
        $this->hasDomesticCarrierCodeFormattingRule = true;
271 70
        $this->domesticCarrierCodeFormattingRule = (string)$value;
272
273 70
        return $this;
274
    }
275
276
    /**
277
     * @param NumberFormat $other
278
     * @return NumberFormat
279
     */
280 13
    public function mergeFrom(NumberFormat $other)
281
    {
282 13
        if ($other->hasPattern()) {
283 11
            $this->setPattern($other->getPattern());
284
        }
285 13
        if ($other->hasFormat()) {
286 13
            $this->setFormat($other->getFormat());
287
        }
288 13
        $leadingDigitsPatternSize = $other->leadingDigitsPatternSize();
289 13
        for ($i = 0; $i < $leadingDigitsPatternSize; $i++) {
290 5
            $this->addLeadingDigitsPattern($other->getLeadingDigitsPattern($i));
291
        }
292 13
        if ($other->hasNationalPrefixFormattingRule()) {
293 11
            $this->setNationalPrefixFormattingRule($other->getNationalPrefixFormattingRule());
294
        }
295 13
        if ($other->hasDomesticCarrierCodeFormattingRule()) {
296 8
            $this->setDomesticCarrierCodeFormattingRule($other->getDomesticCarrierCodeFormattingRule());
297
        }
298 13
        if ($other->hasNationalPrefixOptionalWhenFormatting()) {
299 11
            $this->setNationalPrefixOptionalWhenFormatting($other->getNationalPrefixOptionalWhenFormatting());
300
        }
301
302 13
        return $this;
303
    }
304
305
    /**
306
     * @return array
307
     */
308 201
    public function toArray()
309
    {
310 201
        $output = array();
311 201
        $output['pattern'] = $this->getPattern();
312 201
        $output['format'] = $this->getFormat();
313
314 201
        $output['leadingDigitsPatterns'] = $this->leadingDigitPatterns();
315
316 201
        if ($this->hasNationalPrefixFormattingRule()) {
317 106
            $output['nationalPrefixFormattingRule'] = $this->getNationalPrefixFormattingRule();
318
        }
319
320 201
        if ($this->hasDomesticCarrierCodeFormattingRule()) {
321 11
            $output['domesticCarrierCodeFormattingRule'] = $this->getDomesticCarrierCodeFormattingRule();
322
        }
323
324 201
        if ($this->hasNationalPrefixOptionalWhenFormatting()) {
325 201
            $output['nationalPrefixOptionalWhenFormatting'] = $this->getNationalPrefixOptionalWhenFormatting();
326
        }
327
328 201
        return $output;
329
    }
330
331
    /**
332
     * @param array $input
333
     */
334 989
    public function fromArray(array $input)
335
    {
336 989
        $this->setPattern($input['pattern']);
337 989
        $this->setFormat($input['format']);
338 989
        foreach ($input['leadingDigitsPatterns'] as $leadingDigitsPattern) {
339 877
            $this->addLeadingDigitsPattern($leadingDigitsPattern);
340
        }
341
342 989
        if (isset($input['nationalPrefixFormattingRule']) && $input['nationalPrefixFormattingRule'] !== '') {
343 542
            $this->setNationalPrefixFormattingRule($input['nationalPrefixFormattingRule']);
344
        }
345 989
        if (isset($input['domesticCarrierCodeFormattingRule']) && $input['domesticCarrierCodeFormattingRule'] !== '') {
346 62
            $this->setDomesticCarrierCodeFormattingRule($input['domesticCarrierCodeFormattingRule']);
347
        }
348
349 989
        if (isset($input['nationalPrefixOptionalWhenFormatting'])) {
350 989
            $this->setNationalPrefixOptionalWhenFormatting($input['nationalPrefixOptionalWhenFormatting']);
351
        }
352 989
    }
353
}
354