Completed
Push — master ( 5f432b...5d5b15 )
by Jens
15:50 queued 05:48
created

AbstractJsonDeserializeObject   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 228
Duplicated Lines 6.14 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 92.65%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 34
c 1
b 1
f 0
lcom 2
cbo 2
dl 14
loc 228
ccs 63
cts 68
cp 0.9265
rs 9.2

18 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 4 1
A toJson() 0 8 1
initialize() 0 1 ?
A __construct() 0 7 2
A __sleep() 0 4 1
A hasInterface() 0 15 4
A isPrimitive() 0 8 2
A isValidType() 0 4 3
A isType() 0 7 2
A isDeserializable() 0 4 2
A isDeserializableType() 7 7 3
A isTypeableType() 7 7 3
A getTyped() 0 10 3
A setRawData() 0 6 1
A getRaw() 0 4 2
A fromArray() 0 4 1
A of() 0 4 1
A toArray() 0 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Common;
7
8
/**
9
 * @package Commercetools\Core\Model\Common
10
 */
11
abstract class AbstractJsonDeserializeObject implements JsonDeserializeInterface, ObjectTreeInterface
12
{
13
    use ObjectTreeTrait;
14
15
    const JSON_DESERIALIZE_INTERFACE = 'Commercetools\Core\Model\Common\JsonDeserializeInterface';
16
    const TYPEABLE_INTERFACE = '\Commercetools\Core\Model\Common\TypeableInterface';
17
18
    abstract protected function initialize($field);
19
20
    protected $rawData = [];
21
    protected $typeData = [];
22
    protected $initialized = [];
23
24
    /**
25
     * @param array $data
26
     * @param Context|callable $context
27
     */
28 1102
    public function __construct(array $data = [], $context = null)
29
    {
30 1102
        $this->setContext($context);
31 1102
        if (!is_null($data)) {
32 1102
            $this->rawData = $this->typeData = $data;
33
        }
34 1102
    }
35
36
    protected static $primitives = [
37
        'bool' => 'is_bool',
38
        'int' => 'is_int',
39
        'string' => 'is_string',
40
        'float' => 'is_float',
41
        'array' => 'is_array'
42
    ];
43
44
    /**
45
     * @var bool[]
46
     */
47
    protected static $interfaces = [];
48
49
    public function __sleep()
50
    {
51
        return array_diff(array_keys(get_object_vars($this)), ['context']);
52
    }
53
54
    /**
55
     * @param $type
56
     * @param $interfaceName
57
     * @return bool
58
     * @internal
59
     */
60 415
    protected function hasInterface($type, $interfaceName)
61
    {
62 415
        $interfaceName = trim($interfaceName, '\\');
63 415
        $cacheKey = $interfaceName . '-' . $type;
64 415
        if (!isset(static::$interfaces[$cacheKey])) {
65 85
            $interface = false;
66 85
            if ($this->isPrimitive($type) === false
67 85
                && isset(class_implements($type)[$interfaceName])
68
            ) {
69 76
                $interface = true;
70
            }
71 85
            static::$interfaces[$cacheKey] = $interface;
72
        }
73 415
        return static::$interfaces[$cacheKey];
74
    }
75
76
    /**
77
     * @param $type
78
     * @return string|false
79
     * @internal
80
     */
81 714
    protected function isPrimitive($type)
82
    {
83 714
        if (!isset(static::$primitives[$type])) {
84 404
            return false;
85
        }
86
87 668
        return static::$primitives[$type];
88
    }
89
90
    /**
91
     * @param string|bool $type
92
     * @param mixed $value
93
     * @return bool
94
     */
95 704
    protected function isValidType($type, $value)
96
    {
97 704
        return !is_string($type) || is_null($value) || $this->isType($type, $value);
98
    }
99
100
    /**
101
     * @param string $type
102
     * @param mixed $value
103
     * @return bool
104
     * @internal
105
     */
106 699
    protected function isType($type, $value)
107
    {
108 699
        if ($typeFunction = $this->isPrimitive($type)) {
109 666
            return $typeFunction($value);
110
        }
111 385
        return $value instanceof $type;
112
    }
113
114
    /**
115
     * @param $value
116
     * @return bool
117
     */
118
    protected function isDeserializable($value)
119
    {
120
        return (is_object($value) && $this->hasInterface(get_class($value), static::JSON_DESERIALIZE_INTERFACE));
121
    }
122
123
    /**
124
     * @param $type
125
     * @return bool
126
     */
127 416 View Code Duplication
    protected function isDeserializableType($type)
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...
128
    {
129 416
        if (!is_string($type) || empty($type)) {
130 14
            return false;
131
        }
132 415
        return $this->hasInterface($type, static::JSON_DESERIALIZE_INTERFACE);
133
    }
134
135 402 View Code Duplication
    protected function isTypeableType($type)
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...
136
    {
137 402
        if (!is_string($type) || empty($type)) {
138 14
            return false;
139
        }
140 401
        return $this->hasInterface($type, static::TYPEABLE_INTERFACE);
141
    }
142
143 472
    protected function getTyped($offset)
144
    {
145 472
        if (!isset($this->initialized[$offset])) {
146 418
            $this->initialize($offset);
147
        }
148 472
        if (array_key_exists($offset, $this->typeData)) {
149 472
            return $this->typeData[$offset];
150
        }
151
        return $this->rawData[$offset];
152
    }
153
154
    /**
155
     * @param array $rawData
156
     * @internal
157
     * @return $this
158
     */
159 10
    public function setRawData(array $rawData)
160
    {
161 10
        $this->rawData = $this->typeData = $rawData;
162
163 10
        return $this;
164
    }
165
166
    /**
167
     * @param $field
168
     * @param $default
169
     * @return mixed
170
     */
171 415
    protected function getRaw($field, $default = null)
172
    {
173 415
        return isset($this->rawData[$field])? $this->rawData[$field]: $default;
174
    }
175
176
    /**
177
     * (PHP 5 &gt;= 5.4.0)<br/>
178
     * Specify data which should be serialized to JSON
179
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
180
     * @return mixed data which can be serialized by <b>json_encode</b>,
181
     * which is a value of any type other than a resource.
182
     */
183 359
    public function jsonSerialize()
184
    {
185 359
        return $this->toJson();
186
    }
187
188
    /**
189
     * @return array
190
     */
191 359
    protected function toJson()
192
    {
193
        $data = array_filter($this->typeData, function ($value) {
194 357
            return !is_null($value);
195 359
        });
196
197 359
        return $data;
198
    }
199
200
    /**
201
     * @return array
202
     */
203 5
    public function toArray()
204
    {
205
        $data = array_filter($this->typeData, function ($value) {
206 5
            return !is_null($value);
207 5
        });
208 5
        $data = array_map(
209 5
            function ($value) {
210 5
                if ($value instanceof JsonDeserializeInterface) {
211 1
                    return $value->toArray();
212
                }
213 5
                return $value;
214 5
            },
215
            $data
216
        );
217 5
        return $data;
218
    }
219
220
    /**
221
     * @param array $data
222
     * @param Context|callable $context
223
     * @return static
224
     */
225 585
    public static function fromArray(array $data, $context = null)
226
    {
227 585
        return new static($data, $context);
228
    }
229
230
    /**
231
     * @param Context|callable $context
232
     * @return static
233
     */
234 851
    final public static function of($context = null)
235
    {
236 851
        return new static([], $context);
237
    }
238
}
239