Section   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A count() 0 4 1
A toArray() 0 11 3
A offsetExists() 0 5 1
A getRealPropertyName() 0 4 1
A offsetGet() 0 5 2
A offsetSet() 0 5 1
A offsetUnset() 0 5 1
B imposeDefault() 0 25 6
1
<?php
2
namespace PHPDaemon\Config;
3
4
use PHPDaemon\Config\Entry\Generic;
5
6
/**
7
 * Config section
8
 *
9
 * @property Entry\Boolean enable
10
 * @property Entry\Boolean allowoverridebinpath
11
 * @property Entry\Boolean allowoverridechroot
12
 * @property Entry\Boolean allowoverrideuser
13
 * @property Entry\Boolean allowoverridegroup
14
 * @property Entry\Boolean allowoverridecwd
15
 * @property Entry\Generic cwd
16
 * @property Entry\Generic indexfiles
17
 * @property Entry\Generic mastersocket
18
 * @package    Core
19
 * @subpackage Config
20
 *
21
 * @author     Vasily Zorin <[email protected]>
22
 * @dynamic_fields
23
 */
24
class Section implements \ArrayAccess
25
{
26
    use \PHPDaemon\Traits\ClassWatchdog;
27
28
    /** @var */
29
    public $source;
30
    /** @var */
31
    public $revision;
32
33
    /**
34
     * Constructor
35
     * @param hash
36
     * @return object
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($arr = [])
39
    {
40
        foreach ($arr as $k => $v) {
41
            if (!is_object($v)) {
42
                $e = new Generic;
43
                $e->setHumanValue($v);
44
                $this->{$k} = $e;
45
            } else {
46
                $this->{$k} = $v;
47
            }
48
        }
49
    }
50
51
    /**
52
     * Count elements
53
     * @return integer
54
     */
55
    public function count()
56
    {
57
        return count($this) - 2;
58
    }
59
60
    /**
61
     * toArray handler
62
     * @return hash
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
63
     */
64
    public function toArray()
65
    {
66
        $arr = [];
67
        foreach ($this as $k => $entry) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<PHPDaemon\Config\Section> is not traversable.
Loading history...
68
            if (!$entry instanceof Generic) {
69
                continue;
70
            }
71
            $arr[$k] = $entry->value;
72
        }
73
        return $arr;
74
    }
75
76
    /**
77
     * Checks if property exists
78
     * @param string Property name
79
     * @return boolean Exists?
80
     */
81
82
    public function offsetExists($prop)
83
    {
84
        $prop = $this->getRealPropertyName($prop);
85
        return property_exists($this, $prop);
86
    }
87
88
    /**
89
     * Get real property name
90
     * @param string Property name
91
     * @return string Real property name
92
     */
93
    public function getRealPropertyName($prop)
94
    {
95
        return str_replace('-', '', strtolower($prop));
96
    }
97
98
    /**
99
     * Get property by name
100
     * @param string Property name
101
     * @return mixed
102
     */
103
    public function offsetGet($prop)
104
    {
105
        $prop = $this->getRealPropertyName($prop);
106
        return isset($this->{$prop}) ? $this->{$prop}->value : null;
107
    }
108
109
    /**
110
     * Set property
111
     * @param string Property name
112
     * @param mixed  Value
113
     * @return void
114
     */
115
    public function offsetSet($prop, $value)
116
    {
117
        $prop = $this->getRealPropertyName($prop);
118
        $this->{$prop} = $value;
119
    }
120
121
    /**
122
     * Unset property
123
     * @param string Property name
124
     * @return void
125
     */
126
    public function offsetUnset($prop)
127
    {
128
        $prop = $this->getRealPropertyName($prop);
129
        unset($this->{$prop});
130
    }
131
132
    /**
133
     * Impose default config
134
     * @param array {"setting": "value"}
135
     * @return void
136
     */
137
    public function imposeDefault($settings = [])
138
    {
139
        foreach ($settings as $name => $value) {
140
            $name = strtolower(str_replace('-', '', $name));
141
            if (!isset($this->{$name})) {
142
                if (is_scalar($value)) {
143
                    $this->{$name} = new Generic($value);
144
                } else {
145
                    $this->{$name} = $value;
146
                }
147
            } elseif ($value instanceof Section) {
148
                $value->imposeDefault($value);
0 ignored issues
show
Documentation introduced by
$value is of type object<PHPDaemon\Config\Section>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
            } else {
150
                $current = $this->{$name};
151
                if (!is_object($value)) {
152
                    $this->{$name} = new Generic($value);
153
                } else {
154
                    $this->{$name} = $value;
155
                }
156
                $this->{$name}->setHumanValue($current->value);
157
                $this->{$name}->source = $current->source;
158
                $this->{$name}->revision = $current->revision;
159
            }
160
        }
161
    }
162
}
163