Test Failed
Push — master ( 31c635...ef6440 )
by Paul
07:17 queued 01:32
created

Arguments::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
8
class Arguments extends \ArrayObject
9
{
10
    /**
11
     * @param mixed $args
12
     */
13 24
    public function __construct($args)
14
    {
15 24
        if ($args instanceof Arguments) {
16
            $args = $args->toArray();
17
        } else {
18 24
            $args = Arr::consolidate($args);
19
        }
20 24
        parent::__construct($args, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
21 24
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function __toString()
27
    {
28
        return serialize($this->toArray());
29
    }
30
31
    /**
32
     * @param mixed $key
33
     * @param string $cast
34
     * @return mixed
35
     */
36
    public function cast($key, $cast)
37
    {
38
        return Cast::to($cast, $this->get($key));
39
    }
40
41
    /**
42
     * @param mixed $key
43
     * @param mixed $fallback
44
     * @return mixed
45
     */
46 24
    public function get($key, $fallback = null)
47
    {
48 24
        $value = Arr::get($this->toArray(), $key, $fallback);
49 24
        return isset($fallback)
50 15
            ? Helper::ifEmpty($value, $fallback)
51 24
            : $value;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function isEmpty()
58
    {
59
        return empty($this->toArray());
60
    }
61
62
    /**
63
     * @return self
64
     */
65 13
    public function merge(array $data = [])
66
    {
67 13
        $storage = wp_parse_args($data, $this->toArray());
68 13
        $this->exchangeArray($storage);
69 13
        return $this;
70
    }
71
72
    /**
73
     * @param mixed $key
74
     * @return mixed
75
     */
76
    #[\ReturnTypeWillChange]
77 15
    public function offsetGet($key)
78
    {
79 15
        return $this->get($key);
80
    }
81
82
    /**
83
     * @param mixed $key
84
     * @return void
85
     */
86
    #[\ReturnTypeWillChange]
87
    public function offsetUnset($key)
88
    {
89
        $storage = $this->toArray();
90
        unset($storage[$key]);
91
        $this->exchangeArray($storage);
92
    }
93
94
    /**
95
     * @param string $path
96
     * @param mixed $value
97
     * @return void
98
     */
99 23
    public function set($path, $value)
100
    {
101 23
        $storage = Arr::set($this->toArray(), $path, $value);
102 23
        $this->exchangeArray($storage);
103 23
    }
104
105
    /**
106
     * @return array
107
     */
108 25
    public function toArray()
109
    {
110 25
        return $this->getArrayCopy();
111
    }
112
}
113