Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

PaginationLinksEqualConstraint::matches()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 53
rs 9.1608
cc 5
nc 5
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiAssert\Constraint;
6
7
use PHPUnit\Framework\Constraint\Constraint;
8
use VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint;
9
use VGirol\JsonApiAssert\Members;
10
11
/**
12
 * A constraint class to assert that a link object equals an expected value.
13
 */
14
class PaginationLinksEqualConstraint extends Constraint
15
{
16
    /**
17
     * @var array
18
     */
19
    private $expected;
20
21
    /**
22
     * Class constructor.
23
     *
24
     * @param array $expected
25
     */
26
    public function __construct($expected)
27
    {
28
        $this->expected = $expected;
29
    }
30
31
    /**
32
     * Returns a string representation of the constraint.
33
     */
34
    public function toString(): string
35
    {
36
        return \sprintf(
37
            'equals %s',
38
            $this->exporter()->export($this->expected)
39
        );
40
    }
41
42
    /**
43
     * Evaluates the constraint for parameter $other. Returns true if the
44
     * constraint is met, false otherwise.
45
     *
46
     * @param mixed $other value or object to evaluate
47
     */
48
    protected function matches($other): bool
49
    {
50
        // Add missing members with false value
51
        $cleanExpected = array_merge(
52
            array_fill_keys(static::allowedMembers(), false),
53
            $this->expected
54
        );
55
        asort($cleanExpected);
56
57
        // Extract only pagination members from incoming json
58
        $cleanJson = array_intersect_key($other, array_flip($this->allowedMembers()));
59
        asort($cleanJson);
60
61
        // Search for unexpected members
62
        $notExpectedMembers = array_keys(
63
            array_filter(
64
                $cleanExpected,
65
                function ($value) {
66
                    return $value === false;
67
                }
68
            )
69
        );
70
        if (count(array_intersect_key($cleanJson, array_flip($notExpectedMembers))) !== 0) {
71
            return false;
72
        }
73
74
        // Extracts expected members
75
        $expectedMembers = array_filter(
76
            $cleanExpected,
77
            function ($value) {
78
                return $value !== false;
79
            }
80
        );
81
        if (array_keys($expectedMembers) != array_keys($cleanJson)) {
82
            return false;
83
        }
84
85
        // Extracts members whose value have to be tested
86
        $expectedValues = array_filter(
87
            $expectedMembers,
88
            function ($value) {
89
                return $value !== true;
90
            }
91
        );
92
93
        foreach ($expectedValues as $name => $expectedLink) {
94
            $constraint = new LinkEqualsConstraint($expectedLink);
95
            if ($constraint->check($cleanJson[$name]) === false) {
96
                return false;
97
            }
98
        }
99
100
        return true;
101
    }
102
103
    /**
104
     * Gets the list of allowed members for pagination links
105
     *
106
     * @return array
107
     */
108
    private static function allowedMembers(): array
109
    {
110
        return [
111
            Members::FIRST,
112
            Members::LAST,
113
            Members::PREV,
114
            Members::NEXT
115
        ];
116
    }
117
}
118