Completed
Push — master ( bfd868...ee3a72 )
by Vincent
12:29 queued 10:26
created

AssertLinksObject   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 65
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assertIsValidLinksObject() 0 14 2
A assertIsValidLinkObject() 0 31 4
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use PHPUnit\Framework\Assert as PHPUnit;
5
use VGirol\JsonApiAssert\Messages;
6
use PHPUnit\Framework\ExpectationFailedException;
7
8
trait AssertLinksObject
9
{
10
    /**
11
     * Asserts that a links object is valid.
12
     *
13
     * @param array $links
14
     * @param array $allowedMembers
15
     * 
16
     * @throws PHPUnit\Framework\ExpectationFailedException
17
     */
18 11
    public static function assertIsValidLinksObject($links, $allowedMembers)
19
    {
20 11
        PHPUnit::assertIsArray(
21 11
            $links,
22 11
            Messages::LINKS_OBJECT_NOT_ARRAY
23
        );
24
25 10
        static::assertContainsOnlyAllowedMembers(
26 10
            $allowedMembers,
27 10
            $links
28
        );
29
30 7
        foreach ($links as $key => $link) {
31 7
            static::assertIsValidLinkObject($link);
32
        }
33 6
    }
34
35
    /**
36
     * Asserts that a link object is valid.
37
     *
38
     * @param array $link
39
     * 
40
     * @throws PHPUnit\Framework\ExpectationFailedException
41
     */
42 14
    public static function assertIsValidLinkObject($link)
43
    {
44
        try {
45 14
            PHPUnit::assertIsArray($link);
46 10
        } catch (ExpectationFailedException $e) {
47
            try {
48 10
                PHPUnit::assertIsString($link);
49 7
                return;
50 3
            } catch (ExpectationFailedException $e) {
51 3
                PHPUnit::assertNull(
52 3
                    $link,
53 3
                    Messages::LINK_OBJECT_IS_NOT_ARRAY
54
                );
55 1
                return;
56
            }
57
        }
58
59 4
        PHPUnit::assertArrayHasKey(
60 4
            'href',
61 4
            $link,
62 4
            Messages::LINK_OBJECT_MISS_HREF_MEMBER
63
        );
64
65 3
        $allowed = ['href', 'meta'];
66 3
        static::assertContainsOnlyAllowedMembers(
67 3
            $allowed,
68 3
            $link
69
        );
70
71 2
        if (isset($link['meta'])) {
72 2
            static::assertIsValidMetaObject($link['meta']);
0 ignored issues
show
Bug introduced by
The method assertIsValidMetaObject() does not exist on VGirol\JsonApiAssert\Asserts\AssertLinksObject. Did you maybe mean assertIsValidLinkObject()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            static::/** @scrutinizer ignore-call */ 
73
                    assertIsValidMetaObject($link['meta']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
        }
74 1
    }
75
}
76