Completed
Push — master ( d40022...a8a6e5 )
by Arne
01:50
created

Configuration   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 219
Duplicated Lines 1.83 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 23
lcom 3
cbo 2
dl 4
loc 219
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPath() 0 4 1
A setPath() 0 11 2
A getExclude() 0 4 1
A setExclude() 0 6 1
A addExclusion() 0 6 1
A getIdentity() 0 4 1
A setIdentity() 0 6 1
A getVaults() 0 4 1
A hasVault() 0 4 1
A getVaultByTitle() 0 9 2
A addVault() 0 11 2
C exchangeArray() 4 38 7
A getArrayCopy() 0 11 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
2
3
namespace Archivr;
4
5
use Archivr\Exception\Exception;
6
use Zend\Stdlib\ArraySerializableInterface;
7
8
class Configuration implements ArraySerializableInterface
9
{
10
    /**
11
     * The local base path of the archive.
12
     *
13
     * @var string
14
     */
15
    protected $path;
16
17
    /**
18
     * Set of excluded paths.
19
     *
20
     * @var string[]
21
     */
22
    protected $exclude = [];
23
24
    /**
25
     * Identity to be visible in synchronization log.
26
     *
27
     * @var string
28
     */
29
    protected $identity;
30
31
    /**
32
     * Map of vault configurations by identifier.
33
     *
34
     * @var VaultConfiguration[]
35
     */
36
    protected $vaults = [];
37
38
    public function __construct(string $localPath = './')
39
    {
40
        $this->setPath($localPath);
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getPath(): string
47
    {
48
        return $this->path;
49
    }
50
51
    /**
52
     * @param string $path
53
     *
54
     * @return Configuration
55
     */
56
    public function setPath(string $path): Configuration
57
    {
58
        if (substr($path, -1) !== DIRECTORY_SEPARATOR)
59
        {
60
            $path .= DIRECTORY_SEPARATOR;
61
        }
62
63
        $this->path = $path;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return \string[]
70
     */
71
    public function getExclude(): array
72
    {
73
        return $this->exclude;
74
    }
75
76
    /**
77
     * @param \string[] $paths
78
     *
79
     * @return Configuration
80
     */
81
    public function setExclude(array $paths): Configuration
82
    {
83
        $this->exclude = array_values($paths);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($paths) of type array<integer,object<string>> is incompatible with the declared type array<integer,string> of property $exclude.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $path
90
     *
91
     * @return Configuration
92
     */
93
    public function addExclusion(string $path): Configuration
94
    {
95
        $this->exclude[] = $path;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getIdentity()
104
    {
105
        return $this->identity;
106
    }
107
108
    /**
109
     * @param string $identity
110
     *
111
     * @return Configuration
112
     */
113
    public function setIdentity(string $identity): Configuration
114
    {
115
        $this->identity = $identity;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return VaultConfiguration[]
122
     */
123
    public function getVaults(): array
124
    {
125
        return $this->vaults;
126
    }
127
128
    /**
129
     * @param string $title
130
     * @return bool
131
     */
132
    public function hasVault(string $title): bool
133
    {
134
        return isset($this->vaults[$title]);
135
    }
136
137
    /**
138
     * @param string $title
139
     *
140
     * @return VaultConfiguration
141
     */
142
    public function getVaultByTitle(string $title)
143
    {
144
        if (!isset($this->vaults[$title]))
145
        {
146
            throw new \InvalidArgumentException("Unknown vault configuration requested: {$title}");
147
        }
148
149
        return $this->vaults[$title];
150
    }
151
152
    /**
153
     * @param VaultConfiguration $configuration
154
     *
155
     * @return Configuration
156
     * @throws Exception
157
     */
158
    public function addVault(VaultConfiguration $configuration): Configuration
159
    {
160
        if (isset($this->vaults[$configuration->getTitle()]))
161
        {
162
            throw new Exception(sprintf('Trying to add vault with duplicate title %s.', $configuration->getTitle()));
163
        }
164
165
        $this->vaults[$configuration->getTitle()] = $configuration;
166
167
        return $this;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function exchangeArray(array $array)
174
    {
175 View Code Duplication
        if ($diff = array_diff(array_keys($array), array_keys(get_object_vars($this))))
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...
176
        {
177
            throw new \InvalidArgumentException("Invalid index(es): " . implode(',', $diff));
178
        }
179
180
        foreach ($array as $key => $value)
181
        {
182
            if ($key === 'vaults')
183
            {
184
                if (!is_array($value))
185
                {
186
                    throw new \InvalidArgumentException();
187
                }
188
189
                $this->vaults = [];
190
191
                foreach ($value as $val)
192
                {
193
                    if (!is_array($val))
194
                    {
195
                        throw new \InvalidArgumentException();
196
                    }
197
198
                    $vaultConfig = new VaultConfiguration('dummy');
199
                    $vaultConfig->exchangeArray($val);
200
201
                    $this->addVault($vaultConfig);
202
                }
203
            }
204
            else
205
            {
206
                // using setter to prevent skipping validation
207
                call_user_func([$this, 'set' . ucfirst($key)], $value);
208
            }
209
        }
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function getArrayCopy()
216
    {
217
        $return = get_object_vars($this);
218
        $return['vaults'] = array_values(array_map(function(VaultConfiguration $vaultConfiguration) {
219
220
            return $vaultConfiguration->getArrayCopy();
221
222
        }, $this->vaults));
223
224
        return $return;
225
    }
226
}
227