Passed
Push — master ( 8f83e8...e4d779 )
by Chema
02:15
created

SegmentFactory::segmentFromArray()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 11.0274

Importance

Changes 0
Metric Value
cc 10
eloc 22
nc 10
nop 1
dl 0
loc 30
rs 7.6666
c 0
b 0
f 0
ccs 18
cts 23
cp 0.7826
crap 11.0274

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdifactParser\Segments;
6
7
final class SegmentFactory
8
{
9
    /** @var CustomSegmentFactoryInterface|null */
10
    private $customSegmentsFactory;
11
12 7
    public function __construct(?CustomSegmentFactoryInterface $customSegmentsFactory)
13
    {
14 7
        $this->customSegmentsFactory = $customSegmentsFactory;
15 7
    }
16
17 7
    public function segmentFromArray(array $rawArray): SegmentInterface
18
    {
19 7
        $customSegment = $this->customSegment($rawArray);
20
21 7
        if ($customSegment) {
22 1
            return $customSegment;
23
        }
24
25 7
        $name = $rawArray[0];
26
27 7
        switch ($name) {
28 7
            case 'UNH':
29 7
                return new UNHMessageHeader($rawArray);
30 6
            case 'DTM':
31
                return new DTMDateTimePeriod($rawArray);
32 6
            case 'NAD':
33
                return new NADNameAddress($rawArray);
34 6
            case 'MEA':
35
                return new MEADimensions($rawArray);
36 6
            case 'CNT':
37 3
                return new CNTControl($rawArray);
38 6
            case 'PCI':
39
                return new PCIPackageId($rawArray);
40 6
            case 'BGM':
41
                return new BGMBeginningOfMessage($rawArray);
42 6
            case 'UNT':
43 6
                return new UNTMessageFooter($rawArray);
44
        }
45
46 5
        return new UnknownSegment($rawArray);
47
    }
48
49 7
    private function customSegment(array $rawArray): ?SegmentInterface
50
    {
51 7
        if ($this->customSegmentsFactory) {
52 1
            $segment = $this->customSegmentsFactory->segmentFromArray($rawArray);
53 1
            if ($segment) {
54 1
                return $segment;
55
            }
56
        }
57
58 7
        return null;
59
    }
60
}
61