Completed
Branch next (8c611a)
by Neomerx
02:36
created

parseRelationshipLinks()   B

Complexity

Conditions 11
Paths 96

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
nc 96
nop 4
dl 0
loc 41
ccs 26
cts 26
cp 1
crap 11
rs 7.3166
c 0
b 0
f 0

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 declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Parser\RelationshipData;
4
5
/**
6
 * Copyright 2015-2019 [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 44
    private function parseRelationshipLinks(
38
        SchemaInterface $parentSchema,
39
        $parentData,
40
        string $name,
41
        array $description
42
    ): array {
43 44
        $addSelfLink    = $description[SchemaInterface::RELATIONSHIP_LINKS_SELF] ??
44 44
            $parentSchema->isAddSelfLinkInRelationshipByDefault();
45 44
        $addRelatedLink = $description[SchemaInterface::RELATIONSHIP_LINKS_RELATED] ??
46 44
            $parentSchema->isAddRelatedLinkInRelationshipByDefault();
47 44
        assert(is_bool($addSelfLink) === true || $addSelfLink instanceof LinkInterface);
48 44
        assert(is_bool($addRelatedLink) === true || $addRelatedLink instanceof LinkInterface);
49
50 44
        $schemaLinks = array_key_exists(SchemaInterface::RELATIONSHIP_LINKS, $description) === true ?
51 44
            $description[SchemaInterface::RELATIONSHIP_LINKS] : [];
52 44
        assert(is_array($schemaLinks));
53
54
        // if `self` or `related` link was given as LinkInterface merge it with the other links
55 44
        $extraSchemaLinks = null;
56 44
        if (is_bool($addSelfLink) === false) {
57 1
            $extraSchemaLinks[LinkInterface::SELF] = $addSelfLink;
58 1
            $addSelfLink                           = false;
59
        }
60 44
        if (is_bool($addRelatedLink) === false) {
61 1
            $extraSchemaLinks[LinkInterface::RELATED] = $addRelatedLink;
62 1
            $addRelatedLink                           = false;
63
        }
64 44
        if (empty($extraSchemaLinks) === false) {
65
            // IDE do not understand it's defined without he line below
66 1
            assert(isset($extraSchemaLinks));
67 1
            $schemaLinks = array_merge($extraSchemaLinks, $schemaLinks);
68 1
            unset($extraSchemaLinks);
69
        }
70 44
        assert(is_bool($addSelfLink) === true && is_bool($addRelatedLink) === true);
71
72 44
        $hasLinks = $addSelfLink === true || $addRelatedLink === true || empty($schemaLinks) === false;
73 44
        $links    = $hasLinks === true ?
74 44
            $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...
75
76 44
        return [$hasLinks, $links];
77
    }
78
79
    /**
80
     * @param SchemaInterface $parentSchema
81
     * @param mixed           $parentData
82
     * @param string          $relationshipName
83
     * @param iterable        $schemaLinks
84
     * @param bool            $addSelfLink
85
     * @param bool            $addRelatedLink
86
     *
87
     * @return iterable
88
     */
89 21
    private function parseLinks(
90
        SchemaInterface $parentSchema,
91
        $parentData,
92
        string $relationshipName,
93
        iterable $schemaLinks,
94
        bool $addSelfLink,
95
        bool $addRelatedLink
96
    ): iterable {
97 21
        $gotSelf    = false;
98 21
        $gotRelated = false;
99
100 21
        foreach ($schemaLinks as $name => $link) {
101 11
            assert($link instanceof LinkInterface);
102 11
            if ($name === LinkInterface::SELF) {
103 7
                assert($gotSelf === false);
104 7
                $gotSelf     = true;
105 7
                $addSelfLink = false;
106 5
            } elseif ($name === LinkInterface::RELATED) {
107 1
                assert($gotRelated === false);
108 1
                $gotRelated     = true;
109 1
                $addRelatedLink = false;
110
            }
111
112 11
            yield $name => $link;
113
        }
114
115 21
        if ($addSelfLink === true) {
116 15
            $link = $parentSchema->getRelationshipSelfLink($parentData, $relationshipName);
117 15
            yield LinkInterface::SELF => $link;
118 15
            $gotSelf = true;
119
        }
120 21
        if ($addRelatedLink === true) {
121 12
            $link = $parentSchema->getRelationshipRelatedLink($parentData, $relationshipName);
122 12
            yield LinkInterface::RELATED => $link;
123 12
            $gotRelated = true;
124
        }
125
126
        // spec: check links has at least one of the following: self or related
127 21
        assert($gotSelf || $gotRelated);
128 21
    }
129
}
130