Completed
Push — master ( 9d6b90...0a141c )
by Randy
11:08
created

ArrayExpectations::isAny()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 14
loc 14
rs 9.2
c 0
b 0
f 0
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
}