ParseRelationshipLinksTrait   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 104
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parseRelationshipLinks() 0 40 10
B parseLinks() 0 40 7
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Parser\RelationshipData;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
22
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface;
23
24
/**
25
 * @package Neomerx\JsonApi
26
 */
27
trait ParseRelationshipLinksTrait
28
{
29
    /**
30
     * @param SchemaInterface $parentSchema
31
     * @param mixed           $parentData
32
     * @param string          $name
33
     * @param array           $description
34
     *
35
     * @return array
36
     *
37
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
38
     */
39 50
    private function parseRelationshipLinks(
40
        SchemaInterface $parentSchema,
41
        $parentData,
42
        string $name,
43
        array $description
44
    ): array {
45 50
        $addSelfLink    = $description[SchemaInterface::RELATIONSHIP_LINKS_SELF] ??
46 50
            $parentSchema->isAddSelfLinkInRelationshipByDefault($name);
47 50
        $addRelatedLink = $description[SchemaInterface::RELATIONSHIP_LINKS_RELATED] ??
48 50
            $parentSchema->isAddRelatedLinkInRelationshipByDefault($name);
49 50
        \assert(\is_bool($addSelfLink) === true || $addSelfLink instanceof LinkInterface);
50 50
        \assert(\is_bool($addRelatedLink) === true || $addRelatedLink instanceof LinkInterface);
51
52 50
        $schemaLinks = $description[SchemaInterface::RELATIONSHIP_LINKS] ?? [];
53 50
        \assert(\is_array($schemaLinks));
54
55
        // if `self` or `related` link was given as LinkInterface merge it with the other links
56 50
        $extraSchemaLinks = null;
57 50
        if (\is_bool($addSelfLink) === false) {
58 1
            $extraSchemaLinks[LinkInterface::SELF] = $addSelfLink;
59 1
            $addSelfLink                           = false;
60
        }
61 50
        if (\is_bool($addRelatedLink) === false) {
62 1
            $extraSchemaLinks[LinkInterface::RELATED] = $addRelatedLink;
63 1
            $addRelatedLink                           = false;
64
        }
65 50
        if (empty($extraSchemaLinks) === false) {
66
            // IDE do not understand it's defined without he line below
67 1
            \assert(isset($extraSchemaLinks));
68 1
            $schemaLinks = \array_merge($extraSchemaLinks, $schemaLinks);
69 1
            unset($extraSchemaLinks);
70
        }
71 50
        \assert(\is_bool($addSelfLink) === true && \is_bool($addRelatedLink) === true);
72
73 50
        $hasLinks = $addSelfLink === true || $addRelatedLink === true || empty($schemaLinks) === false;
74 50
        $links    = $hasLinks === true ?
75 50
            $this->parseLinks($parentSchema, $parentData, $name, $schemaLinks, $addSelfLink, $addRelatedLink) : null;
0 ignored issues
show
Documentation introduced by
$schemaLinks is of type array, but the function expects a object<Neomerx\JsonApi\P...ationshipData\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77 50
        return [$hasLinks, $links];
78
    }
79
80
    /**
81
     * @param SchemaInterface $parentSchema
82
     * @param mixed           $parentData
83
     * @param string          $relationshipName
84
     * @param iterable        $schemaLinks
85
     * @param bool            $addSelfLink
86
     * @param bool            $addRelatedLink
87
     *
88
     * @return iterable
89
     */
90 27
    private function parseLinks(
91
        SchemaInterface $parentSchema,
92
        $parentData,
93
        string $relationshipName,
94
        iterable $schemaLinks,
95
        bool $addSelfLink,
96
        bool $addRelatedLink
97
    ): iterable {
98 27
        $gotSelf    = false;
99 27
        $gotRelated = false;
100
101 27
        foreach ($schemaLinks as $name => $link) {
102 14
            \assert($link instanceof LinkInterface);
103 14
            if ($name === LinkInterface::SELF) {
104 10
                \assert($gotSelf === false);
105 10
                $gotSelf     = true;
106 10
                $addSelfLink = false;
107 5
            } elseif ($name === LinkInterface::RELATED) {
108 1
                \assert($gotRelated === false);
109 1
                $gotRelated     = true;
110 1
                $addRelatedLink = false;
111
            }
112
113 14
            yield $name => $link;
114
        }
115
116 27
        if ($addSelfLink === true) {
117 18
            $link = $parentSchema->getRelationshipSelfLink($parentData, $relationshipName);
118 18
            yield LinkInterface::SELF => $link;
119 18
            $gotSelf = true;
120
        }
121 27
        if ($addRelatedLink === true) {
122 15
            $link = $parentSchema->getRelationshipRelatedLink($parentData, $relationshipName);
123 15
            yield LinkInterface::RELATED => $link;
124 15
            $gotRelated = true;
125
        }
126
127
        // spec: check links has at least one of the following: self or related
128 27
        \assert($gotSelf || $gotRelated);
129 27
    }
130
}
131