ParseRelationshipDataTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 96
c 0
b 0
f 0
ccs 39
cts 39
cp 1
rs 10
wmc 13
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseRelationshipData() 0 38 5
B parseData() 0 33 8
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 IteratorAggregate;
22
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
23
use Neomerx\JsonApi\Contracts\Parser\EditableContextInterface;
24
use Neomerx\JsonApi\Contracts\Parser\RelationshipDataInterface;
25
use Neomerx\JsonApi\Contracts\Schema\IdentifierInterface;
26
use Neomerx\JsonApi\Contracts\Schema\PositionInterface;
27
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
28
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface;
29
use Neomerx\JsonApi\Exceptions\InvalidArgumentException;
30
use Neomerx\JsonApi\Parser\IdentifierAndResource;
31
use Traversable;
32
use function Neomerx\JsonApi\I18n\format as _;
33
34
/**
35
 * @package Neomerx\JsonApi
36
 */
37
trait ParseRelationshipDataTrait
38
{
39
    /**
40
     * @param FactoryInterface         $factory
41
     * @param SchemaContainerInterface $container
42
     * @param EditableContextInterface $context
43
     * @param string                   $parentType
44
     * @param string                   $name
45
     * @param array                    $description
46
     * @param int                      $nextLevel
47
     * @param string                   $nextPathPrefix
48
     *
49
     * @return array [has data, parsed data, next position]
50
     */
51 50
    private function parseRelationshipData(
52
        FactoryInterface $factory,
53
        SchemaContainerInterface $container,
54
        EditableContextInterface $context,
55
        string $parentType,
56
        string $name,
57
        array $description,
58
        int $nextLevel,
59
        string $nextPathPrefix
60
    ): array {
61 50
        $hasData = \array_key_exists(SchemaInterface::RELATIONSHIP_DATA, $description);
62
        // either no data or data should be array/object/null
63 50
        \assert(
64 50
            $hasData === false ||
65
            (
66 39
                \is_array($data = $description[SchemaInterface::RELATIONSHIP_DATA]) === true ||
67 33
                \is_object($data) === true ||
68 50
                $data === null
69
            )
70
        );
71
72 50
        $nextPosition = $factory->createPosition(
73 50
            $nextLevel,
74 50
            $nextPathPrefix . $name,
75 50
            $parentType,
76 50
            $name
77
        );
78
79 50
        $relationshipData = $hasData === true ? $this->parseData(
80 39
            $factory,
81 39
            $container,
82 39
            $context,
83 39
            $nextPosition,
84 39
            $description[SchemaInterface::RELATIONSHIP_DATA]
85 50
        ) : null;
86
87 50
        return [$hasData, $relationshipData, $nextPosition];
88
    }
89
90
    /**
91
     * @param FactoryInterface         $factory
92
     * @param SchemaContainerInterface $container
93
     * @param EditableContextInterface $context
94
     * @param PositionInterface        $position
95
     * @param mixed                    $data
96
     *
97
     * @return RelationshipDataInterface
98
     */
99 39
    private function parseData(
100
        FactoryInterface $factory,
101
        SchemaContainerInterface $container,
102
        EditableContextInterface $context,
103
        PositionInterface $position,
104
        $data
105
    ): RelationshipDataInterface {
106
        // support if data is callable (e.g. a closure used to postpone actual data reading)
107 39
        if (\is_callable($data) === true) {
108 1
            $data = \call_user_func($data);
109
        }
110
111 39
        if ($container->hasSchema($data) === true) {
112 27
            return $factory->createRelationshipDataIsResource($container, $context, $position, $data);
113 37
        } elseif ($data instanceof IdentifierInterface) {
114 1
            return $factory->createRelationshipDataIsIdentifier($container, $context, $position, $data);
115 36
        } elseif (\is_array($data) === true) {
116 34
            return $factory->createRelationshipDataIsCollection($container, $context, $position, $data);
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a object<Neomerx\JsonApi\C...cts\Factories\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...
117 6
        } elseif ($data instanceof Traversable) {
118 1
            return $factory->createRelationshipDataIsCollection(
119 1
                $container,
120 1
                $context,
121 1
                $position,
122 1
                $data instanceof IteratorAggregate ? $data->getIterator() : $data
0 ignored issues
show
Documentation introduced by
$data instanceof \Iterat...->getIterator() : $data is of type object<Traversable>, but the function expects a object<Neomerx\JsonApi\C...cts\Factories\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...
123
            );
124 5
        } elseif ($data === null) {
125 5
            return $factory->createRelationshipDataIsNull();
126
        }
127
128 1
        throw new InvalidArgumentException(
129 1
            _(IdentifierAndResource::MSG_NO_SCHEMA_FOUND, \get_class($data), $position->getPath())
130
        );
131
    }
132
}
133