Test Failed
Push — main ( f5d985...a31f0b )
by Paul
08:37
created

Arguments   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 80.56%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
eloc 34
c 1
b 0
f 1
dl 0
loc 132
ccs 29
cts 36
cp 0.8056
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A array() 0 3 1
A __toString() 0 3 1
A cast() 0 3 1
A toArray() 0 3 1
A sanitize() 0 6 1
A set() 0 4 1
A exists() 0 3 1
A replace() 0 4 1
A merge() 0 5 1
A get() 0 7 2
A offsetUnset() 0 6 1
A isEmpty() 0 3 1
A offsetGet() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Modules\Sanitizer;
9
10
class Arguments extends \ArrayObject
11
{
12
    /**
13
     * @param mixed $args
14 26
     */
15
    public function __construct($args = [])
16 26
    {
17 8
        if ($args instanceof Arguments) {
18
            $args = $args->toArray();
19 26
        } else {
20
            $args = Arr::consolidate($args);
21 26
        }
22
        parent::__construct($args, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
23
    }
24
25
    public function __toString(): string
26
    {
27 7
        return serialize($this->toArray());
28
    }
29 7
30
    /**
31
     * @param mixed $key
32
     */
33
    public function array($key, array $fallback = []): array
34
    {
35
        return Arr::consolidate($this->get($key, $fallback));
36
    }
37 8
38
    /**
39 8
     * @param mixed $key
40
     *
41
     * @return mixed
42
     */
43
    public function cast($key, string $cast, $fallback = null)
44
    {
45
        return Cast::to($cast, $this->get($key, $fallback));
46
    }
47 34
48
    public function exists(string $key): bool
49 34
    {
50 34
        return $this->offsetExists($key);
51 25
    }
52 34
53
    /**
54
     * @param mixed $key
55
     * @param mixed $fallback
56
     *
57
     * @return mixed
58
     */
59
    public function get($key, $fallback = null)
60
    {
61
        $value = Arr::get($this->getArrayCopy(), $key, null);
62
        if (is_null($fallback)) {
63
            return $value;
64
        }
65
        return Helper::ifEmpty($value, $fallback);
66 25
    }
67
68 25
    public function isEmpty(): bool
69 25
    {
70 25
        return empty($this->getArrayCopy());
71
    }
72
73
    /**
74
     * @return self
75
     */
76
    public function merge(array $data = [])
77
    {
78 25
        $storage = wp_parse_args($data, $this->getArrayCopy());
79
        $this->exchangeArray($storage);
80 25
        return $this;
81
    }
82
83
    /**
84
     * @param mixed $key
85
     *
86
     * @return mixed
87
     */
88 4
    #[\ReturnTypeWillChange]
89
    public function offsetGet($key)
90 4
    {
91 4
        return $this->get($key);
92 4
    }
93
94
    /**
95
     * @param mixed $key
96
     */
97
    #[\ReturnTypeWillChange]
98
    public function offsetUnset($key): void
99
    {
100
        $storage = $this->getArrayCopy();
101
        unset($storage[$key]);
102
        $this->exchangeArray($storage);
103
    }
104
105
    /**
106
     * @return self
107
     */
108
    public function replace(array $data = [])
109
    {
110
        $this->exchangeArray($data);
111
        return $this;
112
    }
113 31
114
    /**
115 31
     * @param mixed $key
116 31
     *
117
     * @return mixed
118
     */
119
    public function sanitize($key, string $sanitizer)
120
    {
121
        $sanitizers = ['key' => $sanitizer];
122
        $values = ['key' => $this->get($key)];
123 26
        $values = glsr(Sanitizer::class, compact('values', 'sanitizers'))->run();
124
        return Arr::get($values, 'key');
125 26
    }
126
127
    /**
128
     * @param mixed $value
129
     */
130
    public function set(string $path, $value): void
131
    {
132
        $storage = Arr::set($this->getArrayCopy(), $path, $value);
133
        $this->exchangeArray($storage);
134
    }
135
136
    /**
137
     * @param array $args Optional parameter that can be used to change the output
138
     */
139
    public function toArray(array $args = []): array
140
    {
141
        return Cast::toArrayDeep($this->getArrayCopy());
142
    }
143
}
144