Passed
Branch master (5e7742)
by compolom
03:18 queued 01:36
created

Section   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 25.61 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 21
loc 82
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getName() 0 4 1
A __toString() 0 9 2
A remove() 7 7 2
A update() 7 7 2
A getParam() 0 4 1
A add() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\IniObject;
6
7
use InvalidArgumentException;
8
9
class Section
10
{
11
    private $name;
12
13
    private $params;
14
15
    /**
16
     * Section constructor.
17
     *
18
     * @param string $sectionName
19
     * @param array $params
20
     */
21 10
    public function __construct(string $sectionName, array $params)
22
    {
23 10
        $this->name = $sectionName;
24
25 10
        $data = [];
26 10
        foreach ($params as $name => $value) {
27 10
            $data[$name] = new Param($name, $value);
28
        }
29
30 10
        $this->params = $data;
31 10
    }
32
33
    /**
34
     * @param string $name
35
     * @param $value
36
     */
37 1 View Code Duplication
    public function add(string $name, $value): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
38
    {
39 1
        if (isset($this->params[$name])) {
40 1
            throw new InvalidArgumentException('Overwrite parameter denied');
41
        }
42 1
        $this->params[$name] = new Param($name, $value);
43 1
    }
44
45
    /**
46
     * @param string $name
47
     */
48 1 View Code Duplication
    public function remove(string $name): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
49
    {
50 1
        if (! isset($this->params[$name])) {
51 1
            throw new InvalidArgumentException('Parameter not found for remove');
52
        }
53 1
        unset($this->params[$name]);
54 1
    }
55
56 1 View Code Duplication
    public function update(string $name, $value): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
57
    {
58 1
        if (! isset($this->params[$name])) {
59 1
            throw new InvalidArgumentException('Parameter not found for update');
60
        }
61 1
        $this->params[$name] = new Param($name, $value);
62 1
    }
63
64
    /**
65
     * @param string $name
66
     * @return mixed|null
67
     */
68 4
    public function getParam(string $name)
69
    {
70 4
        return $this->params[$name]->getValue();
71
    }
72
73
    /**
74
     * @return string
75
     */
76 1
    public function getName(): string
77
    {
78 1
        return $this->name;
79
    }
80
81 1
    public function __toString(): string
82
    {
83 1
        $return = '[' . $this->getName() . ']' . PHP_EOL . PHP_EOL;
84 1
        foreach ($this->params as $param) {
85 1
            $return .= $param;
86
        }
87
88 1
        return $return . PHP_EOL;
89
    }
90
}
91