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