PostalObject   D
last analyzed

Complexity

Total Complexity 58

Size/Duplication

Total Lines 572
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 132
dl 0
loc 572
ccs 0
cts 159
cp 0
rs 4.5599
c 0
b 0
f 0
wmc 58

53 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A typePackage() 0 4 1
A __construct() 0 3 1
A addServiceOwnHand() 0 4 1
A getTagDv() 0 3 1
A getAdditionalServices() 0 3 1
A typeLetter() 0 4 1
A getTag() 0 3 1
A addServiceDeclaredValue() 0 4 1
A addServiceNoticeReceipt() 0 4 1
A typeRoll() 0 4 1
A setSize() 0 4 1
A getInvoiceNumber() 0 3 1
A getLength() 0 3 1
A getInvoiceValue() 0 3 1
A getOrderNumber() 0 3 1
A getModel() 0 3 1
A getRecipient() 0 3 1
A getHeight() 0 3 1
A getValueDeclared() 0 3 1
A getPostalUserCode() 0 3 1
A setModel() 0 4 1
A getInvoiceSeries() 0 3 1
A getWeight() 0 3 1
A getService() 0 3 1
A getCostCenter() 0 3 1
A getDescription() 0 3 1
A getValueCharge() 0 3 1
A getWidth() 0 3 1
A getSize() 0 3 1
A toPrint() 0 3 1
A getCubing() 0 3 1
A getDiameter() 0 3 1
A setLength() 0 5 1
A setTag() 0 7 1
A setValueDeclared() 0 5 1
A setRecipient() 0 5 1
A setHeight() 0 5 1
A setInvoiceSeries() 0 5 1
A setPostalUserCode() 0 5 1
A setCubing() 0 5 1
A setWeight() 0 5 1
A setInvoiceValue() 0 5 1
A setDescription() 0 5 1
A setService() 0 5 1
A setCostCenter() 0 5 1
A setOrderNumber() 0 5 1
A setDiameter() 0 5 1
A setValueCharged() 0 5 1
A calculateDv() 0 19 4
A setInvoiceNumber() 0 5 1
A setWidth() 0 5 1
A normalizeTag() 0 21 3

How to fix   Complexity   

Complex Class

Complex classes like PostalObject 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 PostalObject, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Eduardokum\CorreiosPhp\Entities;
3
4
use Eduardokum\CorreiosPhp\Contracts\Render\Printable as PrintableContract;
5
use Eduardokum\CorreiosPhp\Exception\InvalidArgumentException;
6
use Eduardokum\CorreiosPhp\Traits\MagicTrait;
7
8
class PostalObject implements PrintableContract
9
{
10
    use MagicTrait;
11
12
    private $tag;
13
    private $tagDv;
14
    private $service;
15
    private $cubing;
16
    private $weight;
17
    private $postalUserCode;
18
    private $costCenter;
19
    private $orderNumber;
20
    private $invoiceNumber;
21
    private $invoiceSeries;
22
    private $invoiceValue;
23
    private $description;
24
    private $valueCharge;
25
    private $valueDeclared = 0;
26
    private $length;
27
    private $height;
28
    private $width;
29
    private $diameter;
30
    private $additionalService = ['025' => '025'];
31
    private $type = '002';
32
    private $recipient;
33
    private $model = PrintableContract::MODEL_SINGLE;
34
    private $size = PrintableContract::SIZE_SMALL;
35
36
    public function __construct()
37
    {
38
        $this->setRecipient(new Recipient);
39
    }
40
41
    /**
42
     * @return $this
43
     */
44
    public function typeLetter()
45
    {
46
        $this->type = '001';
47
        return $this;
48
    }
49
50
    /**
51
     * @return $this
52
     */
53
    public function typePackage()
54
    {
55
        $this->type = '002';
56
        return $this;
57
    }
58
59
    /**
60
     * @return $this
61
     */
62
    public function typeRoll()
63
    {
64
        $this->type = '003';
65
        return $this;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getType()
72
    {
73
        return $this->type;
74
    }
75
76
    /**
77
     * @return $this
78
     */
79
    public function addServiceOwnHand()
80
    {
81
        $this->additionalService['002'] = '002';
82
        return $this;
83
    }
84
85
    /**
86
     * @return $this
87
     */
88
    public function addServiceDeclaredValue()
89
    {
90
        $this->additionalService['019'] = '019';
91
        return $this;
92
    }
93
94
    /**
95
     * @return $this
96
     */
97
    public function addServiceNoticeReceipt()
98
    {
99
        $this->additionalService['001'] = '001';
100
        return $this;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getAdditionalServices()
107
    {
108
        return $this->additionalService;
109
    }
110
111
    /**
112
     * @return mixed
113
     */
114
    public function getTag()
115
    {
116
        return $this->tag;
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122
    public function getTagDv()
123
    {
124
        return $this->tagDv;
125
    }
126
127
    /**
128
     * @param mixed $tag
129
     *
130
     * @return PostalObject
131
     */
132
    public function setTag($tag)
133
    {
134
        $tags = self::normalizeTag($tag);
135
        $this->tag = $tags['tag'];
136
        $this->tagDv = $tags['tagDv'];
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return mixed
143
     */
144
    public function getService()
145
    {
146
        return $this->service;
147
    }
148
149
    /**
150
     * @param mixed $service
151
     *
152
     * @return PostalObject
153
     */
154
    public function setService($service)
155
    {
156
        $this->service = $service;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @return mixed
163
     */
164
    public function getCubing()
165
    {
166
        return $this->cubing;
167
    }
168
169
    /**
170
     * @param mixed $cubing
171
     *
172
     * @return PostalObject
173
     */
174
    public function setCubing($cubing)
175
    {
176
        $this->cubing = $cubing;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @return mixed
183
     */
184
    public function getWeight()
185
    {
186
        return $this->weight;
187
    }
188
189
    /**
190
     * @param mixed $weight
191
     *
192
     * @return PostalObject
193
     */
194
    public function setWeight($weight)
195
    {
196
        $this->weight = $weight;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return Recipient
203
     */
204
    public function getRecipient()
205
    {
206
        return $this->recipient;
207
    }
208
209
    /**
210
     * @param Recipient $recipient
211
     *
212
     * @return PostalObject
213
     */
214
    public function setRecipient(Recipient $recipient)
215
    {
216
        $this->recipient = $recipient;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return mixed
223
     */
224
    public function getPostalUserCode()
225
    {
226
        return $this->postalUserCode;
227
    }
228
229
    /**
230
     * @param mixed $postalUserCode
231
     *
232
     * @return PostalObject
233
     */
234
    public function setPostalUserCode($postalUserCode)
235
    {
236
        $this->postalUserCode = $postalUserCode;
237
238
        return $this;
239
    }
240
241
    /**
242
     * @return mixed
243
     */
244
    public function getCostCenter()
245
    {
246
        return $this->costCenter;
247
    }
248
249
    /**
250
     * @param mixed $costCenter
251
     *
252
     * @return PostalObject
253
     */
254
    public function setCostCenter($costCenter)
255
    {
256
        $this->costCenter = $costCenter;
257
258
        return $this;
259
    }
260
261
    /**
262
     * @return mixed
263
     */
264
    public function getInvoiceNumber()
265
    {
266
        return $this->invoiceNumber;
267
    }
268
269
    /**
270
     * @param mixed $invoiceNumber
271
     *
272
     * @return PostalObject
273
     */
274
    public function setInvoiceNumber($invoiceNumber)
275
    {
276
        $this->invoiceNumber = $invoiceNumber;
277
278
        return $this;
279
    }
280
281
    /**
282
     * @return mixed
283
     */
284
    public function getOrderNumber()
285
    {
286
        return $this->orderNumber;
287
    }
288
289
    /**
290
     * @param mixed $orderNumber
291
     *
292
     * @return PostalObject
293
     */
294
    public function setOrderNumber($orderNumber)
295
    {
296
        $this->orderNumber = $orderNumber;
297
298
        return $this;
299
    }
300
301
    /**
302
     * @return mixed
303
     */
304
    public function getInvoiceSeries()
305
    {
306
        return $this->invoiceSeries;
307
    }
308
309
    /**
310
     * @param mixed $invoiceSeries
311
     *
312
     * @return PostalObject
313
     */
314
    public function setInvoiceSeries($invoiceSeries)
315
    {
316
        $this->invoiceSeries = $invoiceSeries;
317
318
        return $this;
319
    }
320
321
    /**
322
     * @return mixed
323
     */
324
    public function getInvoiceValue()
325
    {
326
        return $this->invoiceValue;
327
    }
328
329
    /**
330
     * @param mixed $invoiceValue
331
     *
332
     * @return PostalObject
333
     */
334
    public function setInvoiceValue($invoiceValue)
335
    {
336
        $this->invoiceValue = $invoiceValue;
337
338
        return $this;
339
    }
340
341
    /**
342
     * @return mixed
343
     */
344
    public function getDescription()
345
    {
346
        return $this->description;
347
    }
348
349
    /**
350
     * @param mixed $description
351
     *
352
     * @return PostalObject
353
     */
354
    public function setDescription($description)
355
    {
356
        $this->description = $description;
357
358
        return $this;
359
    }
360
361
    /**
362
     * @return mixed
363
     */
364
    public function getValueCharge()
365
    {
366
        return $this->valueCharge;
367
    }
368
369
    /**
370
     * @param mixed $valueCharge
371
     *
372
     * @return PostalObject
373
     */
374
    public function setValueCharged($valueCharge)
375
    {
376
        $this->valueCharge = $valueCharge;
377
378
        return $this;
379
    }
380
381
    /**
382
     * @return mixed
383
     */
384
    public function getValueDeclared()
385
    {
386
        return $this->valueDeclared;
387
    }
388
389
    /**
390
     * @param mixed $valueDeclared
391
     *
392
     * @return PostalObject
393
     */
394
    public function setValueDeclared($valueDeclared)
395
    {
396
        $this->valueDeclared = $valueDeclared;
397
398
        return $this;
399
    }
400
401
    /**
402
     * @return mixed
403
     */
404
    public function getLength()
405
    {
406
        return $this->length;
407
    }
408
409
    /**
410
     * @param mixed $length
411
     *
412
     * @return PostalObject
413
     */
414
    public function setLength($length)
415
    {
416
        $this->length = $length;
417
418
        return $this;
419
    }
420
421
    /**
422
     * @return mixed
423
     */
424
    public function getHeight()
425
    {
426
        return $this->height;
427
    }
428
429
    /**
430
     * @param mixed $height
431
     *
432
     * @return PostalObject
433
     */
434
    public function setHeight($height)
435
    {
436
        $this->height = $height;
437
438
        return $this;
439
    }
440
441
    /**
442
     * @return mixed
443
     */
444
    public function getWidth()
445
    {
446
        return $this->width;
447
    }
448
449
    /**
450
     * @param mixed $width
451
     *
452
     * @return PostalObject
453
     */
454
    public function setWidth($width)
455
    {
456
        $this->width = $width;
457
458
        return $this;
459
    }
460
461
    /**
462
     * @return mixed
463
     */
464
    public function getDiameter()
465
    {
466
        return $this->diameter;
467
    }
468
469
    /**
470
     * @param mixed $diameter
471
     *
472
     * @return PostalObject
473
     */
474
    public function setDiameter($diameter)
475
    {
476
        $this->diameter = $diameter;
477
478
        return $this;
479
    }
480
481
    /**
482
     * @return string
483
     */
484
    public function getModel()
485
    {
486
        return $this->model;
487
    }
488
489
    /**
490
     * @param $model
491
     *
492
     * @return $this
493
     */
494
    public function setModel($model)
495
    {
496
        $this->model = $model;
497
        return $this;
498
    }
499
500
    /**
501
     * @return string
502
     */
503
    public function getSize()
504
    {
505
        return $this->size;
506
    }
507
508
    /**
509
     * @param $size
510
     *
511
     * @return $this
512
     */
513
    public function setSize($size)
514
    {
515
        $this->size = $size;
516
        return $this;
517
    }
518
519
    /**
520
     * @return array
521
     */
522
    public function toPrint()
523
    {
524
        return [$this];
525
    }
526
527
    /**
528
     * @param $tag
529
     *
530
     * @return string
531
     */
532
    public static function calculateDv($tag)
533
    {
534
        if (!preg_match('/(?<prefix>\D{2})?\s?(?<number>\d{8})\s?(?<sufix>\D{2})?/', $tag, $matches)) {
535
            throw new InvalidArgumentException("Invalid tag '$tag'");
536
        }
537
        $matches += ['prefix' => null, 'sufix' => ''];
538
        $chars = str_split($matches['number'], 1);
539
        $sums = str_split("86423597", 1);
540
        $rest = array_reduce($chars, function (&$sum, $a) use (&$sums) {
541
                return $sum += $a * array_shift($sums);
542
            }) % 11;
543
        $dv = 5;
544
        if ($rest != 0) {
545
            $dv = $rest == 1 ? 0 : 11 - $rest;
546
        }
547
        return vsprintf('%s%s%s', [
548
            $matches['prefix'],
549
            $matches['number'] . $dv,
550
            $matches['sufix']
551
        ]);
552
    }
553
554
    /**
555
     * @param $tag
556
     *
557
     * @return array
558
     */
559
    public static function normalizeTag($tag)
560
    {
561
        if (!preg_match('/(?<prefix>\D{2})?\s?(?<number>\d{8,9})\s?(?<sufix>\D{2})?/', $tag, $matches)) {
562
            throw new InvalidArgumentException(sprintf("Tag '%s' is not acceptable.", $tag));
563
        }
564
565
        $matches = array_filter($matches);
566
        if (strlen($matches['number']) == 8) {
567
            return [
568
                'tag' => $tag,
569
                'tagDv' => self::calculateDv($tag)
570
            ];
571
        }
572
573
        return [
574
            'tag' => vsprintf('%s%s%s', [
575
                $matches['prefix'],
576
                substr($matches['number'], 0, -1),
577
                $matches['sufix'],
578
            ]),
579
            'tagDv' => $tag,
580
        ];
581
    }
582
}
583