GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JsonSerializer::stockObjectJson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Common\Transport;
4
5
use OpenStack\Common\Api\Parameter;
6
use OpenStack\Common\JsonPath;
7
8
/**
9
 * Class responsible for populating the JSON body of a {@see GuzzleHttp\Message\Request} object.
10
 *
11
 * @package OpenStack\Common\Transport
12
 */
13
class JsonSerializer
14
{
15
    /**
16
     * Populates the actual value into a JSON field, i.e. it has reached the end of the line and no
17
     * further nesting is required.
18
     *
19
     * @param Parameter $param     The schema that defines how the JSON field is being populated
20
     * @param mixed     $userValue The user value that is populating a JSON field
21
     * @param array     $json      The existing JSON structure that will be populated
22
     *
23
     * @return array|mixed
24
     */
25 70
    private function stockValue(Parameter $param, $userValue, array $json): array
26
    {
27 70
        $name = $param->getName();
28 70
        if ($path = $param->getPath()) {
29 7
            $jsonPath = new JsonPath($json);
30 7
            $jsonPath->set(sprintf("%s.%s", $path, $name), $userValue);
31 7
            $json = $jsonPath->getStructure();
32 70
        } elseif ($name) {
33 67
            $json[$name] = $userValue;
34 67
        } else {
35 12
            $json[] = $userValue;
36
        }
37
38 70
        return $json;
39
    }
40
41
    /**
42
     * Populates a value into an array-like structure.
43
     *
44
     * @param Parameter $param     The schema that defines how the JSON field is being populated
45
     * @param mixed     $userValue The user value that is populating a JSON field
46
     *
47
     * @return array|mixed
48
     */
49 12
    private function stockArrayJson(Parameter $param, array $userValue): array
50
    {
51 12
        $elems = [];
52 12
        foreach ($userValue as $item) {
53 12
            $elems = $this->stockJson($param->getItemSchema(), $item, $elems);
0 ignored issues
show
Bug introduced by
It seems like $param->getItemSchema() can be null; however, stockJson() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
54 12
        }
55 12
        return $elems;
56
    }
57
58
    /**
59
     * Populates a value into an object-like structure.
60
     *
61
     * @param Parameter $param     The schema that defines how the JSON field is being populated
62
     * @param mixed     $userValue The user value that is populating a JSON field
63
     *
64
     * @return array
65
     */
66 21
    private function stockObjectJson(Parameter $param, \stdClass $userValue): array
67
    {
68 21
        $object = [];
69 21
        foreach ($userValue as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $userValue of type object<stdClass> is not traversable.
Loading history...
70 21
            $object = $this->stockJson($param->getProperty($key), $val, $object);
0 ignored issues
show
Bug introduced by
It seems like $param->getProperty($key) can be null; however, stockJson() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71 21
        }
72 21
        return $object;
73
    }
74
75
    /**
76
     * A generic method that will populate a JSON structure with a value according to a schema. It
77
     * supports multiple types and will delegate accordingly.
78
     *
79
     * @param Parameter $param     The schema that defines how the JSON field is being populated
80
     * @param mixed     $userValue The user value that is populating a JSON field
81
     * @param array     $json      The existing JSON structure that will be populated
82
     *
83
     * @return array
84
     */
85 70
    public function stockJson(Parameter $param, $userValue, array $json): array
86
    {
87 70
        if ($param->isArray()) {
88 12
            $userValue = $this->stockArrayJson($param, $userValue);
89 70
        } elseif ($param->isObject()) {
90 21
            $userValue = $this->stockObjectJson($param, $this->serializeObjectValue($userValue));
91 21
        }
92
        // Populate the final value
93 70
        return $this->stockValue($param, $userValue, $json);
94
    }
95
96
    private function serializeObjectValue($value)
97
    {
98
        if (is_object($value)) {
99
            if ($value instanceof Serializable) {
100
                $value = $value->serialize();
101
            } elseif (!($value instanceof \stdClass)) {
102
                throw new \InvalidArgumentException(sprintf(
103
                    'When an object value is provided, it must either be \stdClass or implement the Serializable '
104
                    . 'interface, you provided %s', print_r($value, true)
105
                ));
106
            }
107
        }
108
109
        return (object) $value;
110
    }
111
}
112