Passed
Push — master ( cdf890...3fa5d8 )
by Vsevolods
03:14
created

Config   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 37.04 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 54.76%

Importance

Changes 0
Metric Value
dl 30
loc 81
ccs 23
cts 42
cp 0.5476
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 4 1
A get() 15 15 3
A getIterator() 0 4 1
A has() 15 15 3
A jsonSerialize() 0 4 1

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 declare(strict_types = 1);
2
3
namespace Venta\Config;
4
5
use ArrayIterator;
6
use IteratorAggregate;
7
use Venta\Contracts\Config\Config as ConfigContract;
8
9
/**
10
 * Class Config
11
 *
12
 * @package Venta\Config
13
 */
14
class Config implements IteratorAggregate, ConfigContract
15
{
16
    /**
17
     * Array of configuration items.
18
     *
19
     * @var array
20
     */
21
    protected $items = [];
22
23
    /**
24
     * Construct function.
25
     *
26
     * @param array $items
27
     */
28 9
    public function __construct(array $items)
29
    {
30 9
        $this->items = $items;
31 9
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 1
    public function all(): array
37
    {
38 1
        return $this->items;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 4 View Code Duplication
    public function get(string $key, $default = null)
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...
45
    {
46 4
        $keys = explode('.', $key);
47 4
        $items = $this->items;
48
49 4
        foreach ($keys as $key) {
50 4
            if (array_key_exists($key, $items)) {
51 4
                $items = $items[$key];
52
            } else {
53 4
                return $default;
54
            }
55
        }
56
57 4
        return $items;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getIterator()
64
    {
65
        return new ArrayIterator($this->items);
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 1 View Code Duplication
    public function has(string $key): bool
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...
72
    {
73 1
        $keys = explode('.', $key);
74 1
        $items = $this->items;
75
76 1
        foreach ($keys as $key) {
77 1
            if (array_key_exists($key, $items)) {
78 1
                $items = $items[$key];
79
            } else {
80 1
                return false;
81
            }
82
        }
83
84 1
        return true;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90 1
    public function jsonSerialize()
91
    {
92 1
        return $this->items;
93
    }
94
}