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

ArrayExpectations   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 30.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 28
loc 92
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A isArray() 0 4 1
A hasLength() 0 4 1
A isEmpty() 0 4 1
A isNotEmpty() 0 4 1
A isAny() 14 14 4
A isAll() 14 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}