1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Expectation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ArrayExpectations |
7
|
|
|
* @package Dgame\Expectation |
8
|
|
|
*/ |
9
|
|
|
final class ArrayExpectations |
10
|
|
|
{ |
11
|
|
|
use ConditionTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ArrayExpectations constructor. |
15
|
|
|
* |
16
|
|
|
* @param $value |
17
|
|
|
*/ |
18
|
|
|
public function __construct($value) |
19
|
|
|
{ |
20
|
|
|
if ($this->isArray($value)) { |
21
|
|
|
$this->value = $value; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $value |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
private function isArray($value): bool |
31
|
|
|
{ |
32
|
|
|
return $this->approveIf(is_array($value))->isApproved(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $length |
37
|
|
|
* |
38
|
|
|
* @return ArrayExpectations |
39
|
|
|
*/ |
40
|
|
|
public function hasLength(int $length): self |
41
|
|
|
{ |
42
|
|
|
return $this->approveIf(count($this->value) === $length); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ArrayExpectations |
47
|
|
|
*/ |
48
|
|
|
public function isEmpty(): self |
49
|
|
|
{ |
50
|
|
|
return $this->approveIf(empty($this->value)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return ArrayExpectations |
55
|
|
|
*/ |
56
|
|
|
public function isNotEmpty(): self |
57
|
|
|
{ |
58
|
|
|
return $this->approveIf(!empty($this->value)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param callable $callback |
63
|
|
|
* |
64
|
|
|
* @return ArrayExpectations |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function isAny(callable $callback): self |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if (!$this->isApproved()) { |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach ($this->value as $value) { |
73
|
|
|
if ($this->approveIf($callback($value))->isApproved()) { |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param callable $callback |
83
|
|
|
* |
84
|
|
|
* @return ArrayExpectations |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function isAll(callable $callback): self |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
if (!$this->isApproved()) { |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ($this->value as $value) { |
93
|
|
|
if (!$this->approveIf($callback($value))->isApproved()) { |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
} |
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.