Passed
Push — master ( cdd133...439166 )
by Martin
03:26
created

Marshal::serializeItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace KingsonDe\Marshal;
6
7
use KingsonDe\Marshal\Data\Collection;
8
use KingsonDe\Marshal\Data\CollectionCallable;
9
use KingsonDe\Marshal\Data\DataStructure;
10
use KingsonDe\Marshal\Data\Item;
11
use KingsonDe\Marshal\Data\ItemCallable;
12
13
class Marshal {
14
15
    /**
16
     * @param DataStructure $dataStructure
17
     * @return array|null
18
     */
19 6
    public static function serialize(DataStructure $dataStructure) {
20 6
        $rawResponse = $dataStructure->build();
21
22 6
        return static::process($rawResponse);
23
    }
24
25 3
    public static function serializeItem(AbstractMapper $mapper, ...$data) {
26 3
        $item = new Item($mapper, ...$data);
27
28 3
        return static::serialize($item);
29
    }
30
31 1
    public static function serializeItemCallable(callable $mappingFunction, ...$data) {
32 1
        $item = new ItemCallable($mappingFunction, ...$data);
33
34 1
        return static::serialize($item);
35
    }
36
37 3
    public static function serializeCollection(AbstractMapper $mapper, ...$data) {
38 3
        $item = new Collection($mapper, ...$data);
39
40 3
        return static::serialize($item);
41
    }
42
43 2
    public static function serializeCollectionCallable(callable $mappingFunction, ...$data) {
44 2
        $item = new CollectionCallable($mappingFunction, ...$data);
45
46 2
        return static::serialize($item);
47
    }
48
49
    /**
50
     * @param array|null $rawResponse
51
     * @return array|null
52
     */
53 6
    private static function process($rawResponse) {
54 6
        if (!\is_array($rawResponse)) {
55 3
            return null;
56
        }
57
58 6
        $response = [];
59
60 6
        foreach ($rawResponse as $property => $value) {
61 6
            if ($value instanceof DataStructure) {
62 3
                $response[$property] = static::serialize($value);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response[$property] is correct as static::serialize($value) targeting KingsonDe\Marshal\Marshal::serialize() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63 3
                continue;
64
            }
65
66 6
            if (\is_array($value)) {
67 6
                $response[$property] = static::process($value);
68 6
                continue;
69
            }
70
71 6
            $response[$property] = $value;
72
        }
73
74 6
        return $response;
75
    }
76
}
77