Completed
Push — master ( 6e606c...bb0f1c )
by Karsten
13:18
created

MapMapper::awakeKeepNulls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 6
1
<?php
2
/**
3
 * File was created 30.09.2015 07:46
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property;
7
8
use PeekAndPoke\Component\Collections\Collection;
9
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsMap;
10
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
11
use PeekAndPoke\Component\Slumber\Core\Codec\Mapper;
12
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
13
14
/**
15
 * @method AsMap getOptions()
16
 *
17
 * @author Karsten J. Gerber <[email protected]>
18
 */
19
class MapMapper extends AbstractCollectionMapper
20
{
21
    /**
22
     * @param Slumberer          $slumberer
23
     * @param array|\Traversable $value
24
     *
25
     * @return null|\stdClass We return a std class as this will ensure json-encode will create something like {"0":1}
26 17
     */
27 View Code Duplication
    public function slumber(Slumberer $slumberer, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28 17
    {
29 8
        if (false === $this->isIterable($value)) {
30
            return null;
31
        }
32 9
33 9
        $nested = $this->nested;
34 9
35
        if ($nested->getOptions()->keepNullValuesInCollections()) {
36 9
            return $this->slumberKeepNulls($slumberer, $value, $nested);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 27 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...per::slumberKeepNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
37
        }
38 5
39
        return $this->slumberFilterNulls($slumberer, $value, $nested);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 27 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...r::slumberFilterNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
    }
41 5
42 5
    /**
43
     * @param Awaker             $awaker
44
     * @param array|\Traversable $value
45
     *
46 9
     * @return array|Collection
47
     */
48 View Code Duplication
    public function awake(Awaker $awaker, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        if (false === $this->isIterable($value)) {
51
            return $this->createAwakeResult([]);
52
        }
53
54
        $nested = $this->nested;
55 20
56
        if ($nested->getOptions()->keepNullValuesInCollections()) {
57 20
            return $this->awakeKeepNulls($awaker, $value, $nested);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 48 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...apper::awakeKeepNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
58 12
        }
59
60
        return $this->awakeFilterNulls($awaker, $value, $nested);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 48 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...per::awakeFilterNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61 8
    }
62 8
63 8
    /**
64
     * @param Slumberer $slumberer
65 8
     * @param array     $value
66
     * @param Mapper    $nested
67 6
     *
68
     * @return \stdClass
69
     */
70 6
    private function slumberKeepNulls(Slumberer $slumberer, $value, Mapper $nested)
71 6
    {
72
        $result = new \stdClass();
73
74
        foreach ($value as $k => $v) {
75
            $result->$k = $nested->slumber($slumberer, $v);
76 8
        }
77
78
        return $result;
79
    }
80
81
    /**
82
     * @param Slumberer $slumberer
83
     * @param array     $value
84
     * @param Mapper    $nested
85
     *
86
     * @return \stdClass
87
     */
88
    private function slumberFilterNulls(Slumberer $slumberer, $value, Mapper $nested)
89
    {
90
        $result = new \stdClass();
91
92 View Code Duplication
        foreach ($value as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
94
            $slumbering = $nested->slumber($slumberer, $v);
95
96
            // check if we should keep nulls
97
            if ($slumbering !== null) {
98
                $result->$k = $slumbering;
99
            }
100
        }
101
102
        return $result;
103
    }
104
105
    /**
106
     * @param Awaker $awaker
107
     * @param array  $value
108
     * @param Mapper $nested
109
     *
110
     * @return array
111
     */
112
    private function awakeKeepNulls(Awaker $awaker, $value, Mapper $nested)
113
    {
114
        $result = [];
115
116
        foreach ($value as $k => $v) {
117
            $result[$k] = $nested->awake($awaker, $v);
118
        }
119
120
        return $result;
121
122
    }
123
124
    /**
125
     * @param Awaker $awaker
126
     * @param array  $value
127
     * @param Mapper $nested
128
     *
129
     * @return array
130
     */
131
    private function awakeFilterNulls(Awaker $awaker, $value, Mapper $nested)
132
    {
133
        $result = [];
134
135 View Code Duplication
        foreach ($value as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
137
            $awoken = $nested->awake($awaker, $v);
138
139
            // check if we should keep nulls
140
            if ($awoken !== null) {
141
                $result[$k] = $awoken;
142
            }
143
        }
144
145
        return $result;
146
    }
147
}
148