InformationListOfLineParser::parse()   B
last analyzed

Complexity

Conditions 9
Paths 19

Size

Total Lines 64

Duplication

Lines 9
Ratio 14.06 %

Importance

Changes 0
Metric Value
cc 9
nc 19
nop 1
dl 9
loc 64
rs 7.2298
c 0
b 0
f 0

How to fix   Long Method   

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
 * @author stev leibelt <[email protected]>
4
 * @since 2017-03-30
5
 */
6
7
namespace Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Parser;
8
9
use InvalidArgumentException;
10
use JonasRudolph\PHPComponents\StringUtility\Implementation\StringUtility;
11
use Net\Bazzline\Component\ApacheServerStatusParser\DomainModel\Information;
12
13
class InformationListOfLineParser implements ListOfLineParserInterface
14
{
15
    /** @var StringUtility */
16
    private $stringUtility;
17
18
19
20
    /**
21
     * InformationListOfLineParser constructor.
22
     *
23
     * @param StringUtility $stringUtility
24
     */
25
    public function __construct(
26
        StringUtility $stringUtility
27
    ) {
28
        $this->stringUtility = $stringUtility;
29
    }
30
31
    /**
32
     * @param string[] $listOfLine
33
     *
34
     * @return Information
35
     * @throws InvalidArgumentException
36
     * @todo make it beautiful to look at
37
     */
38
    public function parse(array $listOfLine)
39
    {
40
        //begin of dependencies
41
        $stringUtility = $this->stringUtility;
42
        //end of dependencies
43
44
        //begin of business logic
45
        $listOfLineHasMinimalSize = (count($listOfLine) > 3);
46
47
        if ($listOfLineHasMinimalSize) {
48
            $listOfMandatoryPropertyNameToStartsWithPrefix = [
49
                'date_of_built' => 'Server Built: ',
50
                'identifier'    => 'Apache Server Status for ',
51
                'mode_of_mpm'   => 'Server MPM: ',
52
                'version'       => 'Server Version: '
53
            ];
54
55
            $listOMandatoryProperties = [
56
                'date_of_built' => null,
57
                'identifier'    => null,
58
                'mode_of_mpm'   => null,
59
                'version'       => null
60
            ];
61
62
            foreach ($listOfLine as $line) {
63
                //stop repeating yourself, us the $listOfMandatoryPropertyNameToStartsWithPrefix
64
                //take a look to the StatisticListOfLineParser
65
                if ($stringUtility->startsWith($line, 'Apache Server Status for ')) {
66
                    $listOMandatoryProperties['identifier'] = substr($line, 25); //always use numbers if you are dealing with static strings
67
                } else if ($stringUtility->startsWith($line, 'Server Version: ')) {
68
                    $listOMandatoryProperties['version'] = substr($line, 16);
69
                } else if ($stringUtility->startsWith($line, 'Server MPM: ')) {
70
                    $listOMandatoryProperties['mode_of_mpm'] = substr($line, 12);
71
                } else if ($stringUtility->startsWith($line, 'Server Built: ')) {
72
                    $listOMandatoryProperties['date_of_built'] = substr($line, 14);
73
                }
74
            }
75
76 View Code Duplication
            foreach ($listOMandatoryProperties as $name => $mandatoryProperty) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
                if (is_null($mandatoryProperty)) {
78
                    throw new InvalidArgumentException(
79
                        'could not find mandatory property "'
80
                        . $listOfMandatoryPropertyNameToStartsWithPrefix[$name]
81
                        . '"'
82
                    );
83
                }
84
            }
85
86
            $information = new Information(
87
                $listOMandatoryProperties['date_of_built'],
88
                $listOMandatoryProperties['identifier'],
89
                $listOMandatoryProperties['mode_of_mpm'],
90
                $listOMandatoryProperties['version']
91
            );
92
        } else {
93
            throw new InvalidArgumentException(
94
                self::class . ' can not parse given list of line "'
95
                . implode(',', $listOfLine) . '"'
96
            );
97
        }
98
99
        return $information;
100
        //end of business logic
101
    }
102
}
103