Completed
Push — master ( 9a2e94...dbda34 )
by Vincent
05:01 queued 12s
created

LinkEqualsConstraint::matches()   B

Complexity

Conditions 11
Paths 82

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 21
c 1
b 0
f 0
nc 82
nop 1
dl 0
loc 42
ccs 22
cts 22
cp 1
crap 11
rs 7.3166

How to fix   Complexity   

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\Members;
9
10
/**
11
 * A constraint class to assert that a link object equals an expected value.
12
 */
13
class LinkEqualsConstraint extends Constraint
14
{
15
    /**
16
     * @var array|string|null
17
     */
18
    private $expected;
19
20
    /**
21
     * Class constructor.
22
     *
23
     * @param array|string|null $expected
24
     */
25 48
    public function __construct($expected)
26
    {
27 48
        $this->expected = $expected;
28 48
    }
29
30
    /**
31
     * Returns a string representation of the constraint.
32
     */
33 27
    public function toString(): string
34
    {
35 27
        return \sprintf(
36 27
            'equals %s',
37 27
            $this->exporter()->export($this->expected)
38
        );
39
    }
40
41
    /**
42
     * Evaluates the constraint for parameter $other. Returns true if the
43
     * constraint is met, false otherwise.
44
     *
45
     * @param array|string|null $other value or object to evaluate
46
     */
47 48
    protected function matches($other): bool
48
    {
49 48
        if ($this->expected === null) {
50 6
            return ($other === null);
51
        }
52
53 42
        if ($other === null) {
54 3
            return false;
55
        }
56
57
        /** @var string $href */
58 39
        $href = \is_array($other) && isset($other[Members::LINK_HREF]) ? $other[Members::LINK_HREF] : $other;
59
60
        /** @var string $expectedHref */
61 39
        $expectedHref = \is_array($this->expected) && isset($this->expected[Members::LINK_HREF]) ?
62 39
            $this->expected[Members::LINK_HREF] : $this->expected;
63
64 39
        $linkElms = explode('?', $href);
65 39
        $expectedElms = explode('?', $expectedHref);
66
67 39
        if (count($expectedElms) != count($linkElms)) {
68 6
            return false;
69
        }
70
71 33
        if ($expectedElms[0] != $linkElms[0]) {
72 12
            return false;
73
        }
74
75 21
        if (count($linkElms) == 1) {
76 12
            return true;
77
        }
78
79 9
        $expectedQuery = explode('&', $expectedElms[1]);
80 9
        $linkQuery = explode('&', $linkElms[1]);
81
82 9
        if (count($expectedQuery) != count($linkQuery)) {
83 3
            return false;
84
        }
85
86 6
        $diff = array_diff($expectedQuery, $linkQuery);
87
88 6
        return count($diff) === 0;
89
    }
90
91
    /**
92
     * Evaluates the constraint for parameter $other. Returns true if the
93
     * constraint is met, false otherwise.
94
     *
95
     * @param mixed $other value or object to evaluate
96
     * @return boolean
97
     */
98 6
    public function check($other): bool
99
    {
100 6
        return $this->matches($other);
101
    }
102
}
103