Completed
Branch master (9acd60)
by
unknown
09:48
created

ArraySerializerTrait::serializeMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Neomerx\JsonApi\Encoder\Serialize;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Iterator;
20
use \Neomerx\JsonApi\Exceptions\ErrorCollection;
21
use \Neomerx\JsonApi\Contracts\Document\ErrorInterface;
22
use \Neomerx\JsonApi\Contracts\Schema\ContainerInterface as CI;
23
use \Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
24
25
/**
26
 * @method CI getContainer()
27
 * @method array encodeDataToArray(CI $container, $data, EncodingParametersInterface $parameters = null)
28
 * @method array encodeIdentifiersToArray($data, EncodingParametersInterface $parameters = null)
29
 * @method array encodeErrorToArray(ErrorInterface $error)
30
 * @method array encodeErrorsToArray($errors)
31
 * @method array encodeMetaToArray($meta)
32
 *
33
 * @package Neomerx\JsonApi
34
 */
35
trait ArraySerializerTrait
36
{
37
    /**
38
     * @param object|array|Iterator|null       $data
39
     * @param EncodingParametersInterface|null $parameters
40
     *
41
     * @return array
42
     */
43 1
    public function serializeData($data, EncodingParametersInterface $parameters = null)
44
    {
45 1
        return $this->encodeDataToArray($this->getContainer(), $data, $parameters);
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by parameter $parameters on line 43 can be null; however, Neomerx\JsonApi\Encoder\...it::encodeDataToArray() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
46
    }
47
48
    /**
49
     * @param object|array|Iterator|null       $data
50
     * @param EncodingParametersInterface|null $parameters
51
     *
52
     * @return array
53
     */
54 1
    public function serializeIdentifiers($data, EncodingParametersInterface $parameters = null)
55
    {
56 1
        return $this->encodeIdentifiersToArray($data, $parameters);
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by parameter $parameters on line 54 can be null; however, Neomerx\JsonApi\Encoder\...odeIdentifiersToArray() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
57
    }
58
59
    /**
60
     * @param ErrorInterface $error
61
     *
62
     * @return array
63
     */
64 1
    public function serializeError(ErrorInterface $error)
65
    {
66 1
        return $this->encodeErrorToArray($error);
67
    }
68
69
    /**
70
     * @param ErrorInterface[]|ErrorCollection $errors
71
     *
72
     * @return array
73
     */
74 1
    public function serializeErrors($errors)
75
    {
76 1
        return $this->encodeErrorsToArray($errors);
77
    }
78
79
    /**
80
     * @param array|object $meta
81
     *
82
     * @return array
83
     */
84 1
    public function serializeMeta($meta)
85
    {
86 1
        return $this->encodeMetaToArray($meta);
87
    }
88
}
89