Completed
Push — master ( 93e29b...ebea66 )
by Randy
01:58
created

ArrayEnsurance::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Dgame\Ensurance;
4
5
use Dgame\Type\Type;
6
7
/**
8
 * Class ArrayEnsurance
9
 * @package Dgame\Ensurance
10
 */
11
final class ArrayEnsurance implements EnsuranceInterface
12
{
13
    use EnsuranceTrait;
14
15
    /**
16
     * ArrayEnsurance constructor.
17
     *
18
     * @param EnsuranceInterface $ensurance
19
     */
20
    public function __construct(EnsuranceInterface $ensurance)
21
    {
22
        $this->transferEnsurance($ensurance);
23 9
        $this->value = $ensurance->else([]);
24
    }
25 9
26 9
    private function forEach(callable $callback): array
27
    {
28
        return array_filter(array_map($callback, $this->value));
29
    }
30
31
    public function all(callable $callback): self
32
    {
33 1
        $this->ensure(count($this->forEach($callback)) === count($this->value));
34
35 1
        return $this;
36
    }
37 1
38
    public function any(callable $callback): self
39
    {
40
        $this->ensure(!empty($this->forEach($callback)));
41
42
        return $this;
43
    }
44
45 1
    public function allTypeOf(string $type): self
46
    {
47 1
        $type = Type::import($type);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $type. This often makes code more readable.
Loading history...
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
48
49 1
        return $this->all(function ($value) use ($type): bool {
50
            return $type->accept($value);
51
        });
52
    }
53
54
    public function anyTypeOf(string $type): self
55
    {
56
        $type = Type::import($type);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $type. This often makes code more readable.
Loading history...
57 1
58
        return $this->any(function ($value) use ($type): bool {
59 1
            return $type->accept($value);
60 1
        });
61
    }
62 1
63
    /**
64
     * @param $key
65
     *
66
     * @return ArrayEnsurance
67
     */
68
    public function hasKey($key): self
69
    {
70 1
        $this->ensure(array_key_exists($key, $this->value))
71
             ->orThrow('Key "%s" is not contained in %s', $key, $this->value);
72 1
73 1
        return $this;
74
    }
75 1
76
    /**
77
     * @param $value
78
     *
79
     * @return ArrayEnsurance
80
     */
81
    public function hasValue($value): self
82
    {
83 1
        $this->ensure(in_array($value, $this->value))
84
             ->orThrow('Value "%s" is not contained in ', $value, $this->value);
85 1
86 1
        return $this;
87
    }
88 1
89
    /**
90
     * @param int $length
91
     *
92
     * @return ArrayEnsurance
93
     */
94
    public function hasLengthOf(int $length): self
95
    {
96 1
        $count = count($this->value);
97
        $this->ensure($count === $length)->orThrow('array has not the length %d (%d)', $length, $count);
98 1
99 1
        return $this;
100
    }
101 1
102
    /**
103
     * @param int $length
104
     *
105
     * @return ArrayEnsurance
106
     */
107
    public function isShorterThan(int $length): self
108
    {
109 1
        $count = count($this->value);
110
        $this->ensure($count < $length)->orThrow('array is not shorter than %d (%d)', $length, $count);
111 1
112 1
        return $this;
113
    }
114 1
115
    /**
116
     * @param int $length
117
     *
118
     * @return ArrayEnsurance
119
     */
120 1
    public function isShorterOrEqualsTo(int $length): self
121
    {
122 1
        $count = count($this->value);
123 1
        $this->ensure($count <= $length)->orThrow('array is not shorter or equal to %d (%d)', $length, $count);
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * @param int $length
130
     *
131 1
     * @return ArrayEnsurance
132
     */
133 1
    public function isLongerThan(int $length): self
134 1
    {
135
        $count = count($this->value);
136 1
        $this->ensure($count > $length)->orThrow('array is longer than %d (%d)', $length, $count);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param int $length
143
     *
144
     * @return ArrayEnsurance
145
     */
146
    public function isLongerOrEqualTo(int $length): self
147
    {
148
        $count = count($this->value);
149
        $this->ensure($count >= $length)->orThrow('array is not longer or equal to %d (%d)', $length, $count);
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return ArrayEnsurance
156
     */
157
    public function isAssociative(): self
158
    {
159
        $count = count($this->value);
160
        $this->ensure(array_keys($this->value) !== range(0, $count - 1))
161
             ->orThrow('array %s is not associative', $this->value);
162
163
        return $this;
164
    }
165
166
    /**
167
     * @return ArrayEnsurance
168
     */
169
    public function isNotAssociative(): self
170
    {
171
        $count = count($this->value);
172
        $this->ensure(array_keys($this->value) === range(0, $count - 1))
173
             ->orThrow('array %s is associative', $this->value);
174
175
        return $this;
176
    }
177
178
    /**
179
     * @return ArrayEnsurance
180
     */
181
    public function isCallable(): self
182
    {
183
        $this->ensure(is_callable($this->value))->orThrow('Value is not a callable');
184
185
        return $this;
186
    }
187
}
188