Generic   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 7.11 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 14
loc 197
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A setDefaultValue() 0 5 1
A plainToHuman() 0 4 1
A getHumanValue() 0 4 1
A setHumanValue() 7 7 1
A getValue() 0 4 1
A setValue() 7 7 1
A isStackable() 0 4 1
A setStackable() 0 4 1
A pushHumanValue() 0 4 1
B pushValue() 0 25 6
A onUpdate() 0 3 1
A humanToPlain() 0 4 1
A setValueType() 0 4 1
A resetToDefault() 0 8 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
namespace PHPDaemon\Config\Entry;
3
4
/**
5
 * Config entry
6
 *
7
 * @package    Core
8
 * @subpackage Config
9
 *
10
 * @author     Vasily Zorin <[email protected]>
11
 */
12
class Generic
13
{
14
    use \PHPDaemon\Traits\ClassWatchdog;
15
    use \PHPDaemon\Traits\StaticObjectWatchdog;
16
17
    /** @var */
18
    public $defaultValue;
19
    /** @var */
20
    public $value;
21
    /** @var */
22
    public $valueType;
23
    /** @var */
24
    public $humanValue;
25
    /** @var */
26
    public $source;
27
    /** @var */
28
    public $revision;
29
    /** @var bool */
30
    public $hasDefaultValue = false;
31
    /** @var bool */
32
    protected $stackable = false;
33
34
    /**
35
     * Constructor
36
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    public function __construct()
39
    {
40
        if (func_num_args() === 1) {
41
            $this->setDefaultValue(func_get_arg(0));
42
            $this->setHumanValue(func_get_arg(0));
43
        }
44
    }
45
46
    /**
47
     * Set default value
48
     * @param mixed
49
     * @return void
50
     */
51
    public function setDefaultValue($value)
52
    {
53
        $this->defaultValue = $value;
54
        $this->hasDefaultValue = true;
55
    }
56
57
    /**
58
     * Converts plain value to human-readable
59
     * @param mixed
60
     * @return mixed
61
     */
62
    public static function plainToHuman($value)
63
    {
64
        return $value;
65
    }
66
67
    /**
68
     * Get human value
69
     * @return mixed
70
     */
71
    public function getHumanValue()
72
    {
73
        return $this->humanValue;
74
    }
75
76
    /**
77
     * Set human-readable value
78
     * @param mixed
79
     * @return void
80
     */
81 View Code Duplication
    public function setHumanValue($value)
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...
82
    {
83
        $this->humanValue = $value;
84
        $old = $this->value;
85
        $this->value = static::humanToPlain($value);
86
        $this->onUpdate($old);
87
    }
88
89
    /**
90
     * Get value
91
     * @return mixed
92
     */
93
    public function getValue()
94
    {
95
        return $this->value;
96
    }
97
98
    /**
99
     * Set value
100
     * @param mixed
101
     * @return void
102
     */
103 View Code Duplication
    public function setValue($value)
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...
104
    {
105
        $old = $this->value;
106
        $this->value = $value;
107
        $this->humanValue = static::plainToHuman($value);
108
        $this->onUpdate($old);
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function isStackable()
115
    {
116
        return $this->stackable;
117
    }
118
119
    /**
120
     * @param bool $b
121
     */
122
    public function setStackable($b = true)
123
    {
124
        $this->stackable = $b;
125
    }
126
127
    /**
128
     * Push human-readable value
129
     * @param $value
130
     * @return void
131
     */
132
    public function pushHumanValue($value)
133
    {
134
        $this->pushValue(static::humanToPlain($value));
135
    }
136
137
    /**
138
     * Push plain value
139
     * @param $value
140
     * @return void
141
     */
142
    public function pushValue($value)
143
    {
144
        $old = $this->value;
145
        if (!$this->stackable) {
146
            $this->setValue($value);
147
            return;
148
        }
149
        if (!is_array($this->value)) {
150
            $this->value = [$this->value, $value];
151
        } else {
152
            $f = false;
153
            foreach ($this->value as $k => $v) {
154
                if (!is_int($k)) {
155
                    $f = true;
156
                    break;
157
                }
158
            }
159
            if (!$f) {
160
                $this->value[] = $value;
161
            } else {
162
                $this->value = [$this->value, $value];
163
            }
164
        }
165
        $this->onUpdate($old);
166
    }
167
168
    /**
169
     * Called when
170
     * @return void
171
     */
172
    public function onUpdate($old)
173
    {
174
    }
175
176
    /**
177
     * Converts human-readable value to plain
178
     * @param mixed
179
     * @return mixed
180
     */
181
    public static function humanToPlain($value)
182
    {
183
        return $value;
184
    }
185
186
    /**
187
     * Set value type
188
     * @param mixed
189
     * @return void
190
     */
191
    public function setValueType($type)
192
    {
193
        $this->valueType = $type;
194
    }
195
196
    /**
197
     * Reset to default
198
     * @return boolean Success
199
     */
200
    public function resetToDefault()
201
    {
202
        if ($this->hasDefaultValue) {
203
            $this->setHumanValue($this->defaultValue);
204
            return true;
205
        }
206
        return false;
207
    }
208
}
209