Passed
Branch master (f41941)
by compolom
05:10 queued 03:27
created

Section::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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 12
    public function __construct(string $sectionName, array $params)
22
    {
23 12
        $this->name = $sectionName;
24
25 12
        $data = [];
26 12
        foreach ($params as $name => $value) {
27 12
            $data[$name] = new Param($name, $value);
28
        }
29
30 12
        $this->params = $data;
31 12
    }
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 2
    public function getName(): string
77
    {
78 2
        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 1
    public function toArray(): array
92
    {
93 1
        $return = [];
94
95 1
        foreach ($this->params as $param) {
96 1
            $return += $param->toArray();
97
        }
98
99 1
        return $return;
100
    }
101
}
102