Mapping   C
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 419
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 2

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 42
c 7
b 0
f 3
lcom 7
cbo 2
dl 0
loc 419
rs 6.9315

36 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getClassAlias() 0 4 1
A setClassAlias() 0 8 1
A getIdProperties() 0 4 1
A addIdProperty() 0 4 1
A hideProperty() 0 4 1
A addPropertyAlias() 0 6 1
A updatePropertyMappings() 0 14 2
A setPropertyNameAliases() 0 8 2
A setProperties() 0 4 1
A getProperties() 0 4 1
A getClassName() 0 4 1
A getResourceUrl() 0 4 1
A getAliasedProperties() 0 4 1
A getHiddenProperties() 0 4 1
A setHiddenProperties() 0 4 1
A getRelationships() 0 4 1
A addAdditionalRelationships() 0 4 1
A getMetaData() 0 4 1
A setMetaData() 0 4 1
A addMetaData() 0 4 1
A getSelfUrl() 0 4 1
A setSelfUrl() 0 4 1
A getRelatedUrl() 0 6 2
A setFilterKeys() 0 4 1
A getFilterKeys() 0 4 1
A setRelationshipUrls() 0 6 1
A getRelationshipSelfUrl() 0 6 2
A setUrls() 0 4 1
A getUrls() 0 4 1
A setCuries() 0 8 3
A getCuries() 0 4 1
A addIncludedResource() 0 4 1
A getIncludedResources() 0 4 1
A filteringIncludedResources() 0 4 1
A isFilteringIncludedResources() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Mapping often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Mapping, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace NilPortugues\Api\Mapping;
4
5
use NilPortugues\Api\Transformer\Helpers\RecursiveFormatterHelper;
6
7
class Mapping
8
{
9
    /**
10
     * @var string
11
     */
12
    private $className = '';
13
    /**
14
     * @var string
15
     */
16
    private $resourceUrlPattern = '';
17
    /**
18
     * @var string
19
     */
20
    private $classAlias = '';
21
    /**
22
     * @var array
23
     */
24
    private $aliasedProperties = [];
25
    /**
26
     * @var array
27
     */
28
    private $hiddenProperties = [];
29
    /**
30
     * @var array
31
     */
32
    private $idProperties = [];
33
    /**
34
     * @var array
35
     */
36
    private $relationships = [];
37
    /**
38
     * @var array
39
     */
40
    private $metaData = [];
41
    /**
42
     * @var string
43
     */
44
    private $selfUrl = '';
45
46
    /**
47
     * @var array
48
     */
49
    private $otherUrls = [];
50
51
    /**
52
     * @var array
53
     */
54
    private $relationshipSelfUrl = [];
55
56
    /**
57
     * @var array
58
     */
59
    private $filterKeys = [];
60
61
    /**
62
     * @var array
63
     */
64
    private $curies = [];
65
66
    /**
67
     * @var array
68
     */
69
    private $properties = [];
70
71
    /**
72
     * @var array
73
     */
74
    private $includedKeys = [];
75
76
    /**
77
     * @var bool
78
     */
79
    private $filteringIncluded = false;
80
81
    /**
82
     * @param       $className
83
     * @param null  $resourceUrlPattern
84
     * @param array $idProperties
85
     */
86
    public function __construct($className, $resourceUrlPattern = null, array $idProperties = [])
87
    {
88
        $this->className = (string) $className;
89
        $this->resourceUrlPattern = $resourceUrlPattern;
90
        $this->idProperties = $idProperties;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getClassAlias()
97
    {
98
        return $this->classAlias;
99
    }
100
101
    /**
102
     * @param string $aliasedClass
103
     *
104
     * @return $this
105
     */
106
    public function setClassAlias($aliasedClass)
107
    {
108
        $this->classAlias = RecursiveFormatterHelper::camelCaseToUnderscore(
109
            RecursiveFormatterHelper::namespaceAsArrayKey($aliasedClass)
110
        );
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function getIdProperties()
119
    {
120
        return (array) $this->idProperties;
121
    }
122
123
    /**
124
     * @param $idProperty
125
     */
126
    public function addIdProperty($idProperty)
127
    {
128
        $this->idProperties[] = (string) $idProperty;
129
    }
130
131
    /**
132
     * @param string $propertyName
133
     */
134
    public function hideProperty($propertyName)
135
    {
136
        $this->hiddenProperties[] = $propertyName;
137
    }
138
139
    /**
140
     * @param $propertyName
141
     * @param $propertyAlias
142
     */
143
    public function addPropertyAlias($propertyName, $propertyAlias)
144
    {
145
        $this->aliasedProperties[$propertyName] = $propertyAlias;
146
147
        $this->updatePropertyMappings($propertyName, $propertyAlias);
148
    }
149
150
    /**
151
     * @param $propertyName
152
     * @param $propertyAlias
153
     */
154
    private function updatePropertyMappings($propertyName, $propertyAlias)
155
    {
156
        if (\in_array($propertyName, $this->idProperties)) {
157
            $position = \array_search($propertyName, $this->idProperties, true);
158
            $this->idProperties[$position] = $propertyAlias;
159
        }
160
161
        $search = \sprintf('{%s}', $propertyName);
162
        $replace = \sprintf('{%s}', $propertyAlias);
163
164
        $this->selfUrl = \str_replace($search, $replace, $this->selfUrl);
165
        $this->resourceUrlPattern = \str_replace($search, $replace, $this->resourceUrlPattern);
166
        $this->otherUrls = \str_replace($search, $replace, $this->otherUrls);
167
    }
168
169
    /**
170
     * @param array $properties
171
     */
172
    public function setPropertyNameAliases(array $properties)
173
    {
174
        $this->aliasedProperties = \array_merge($this->aliasedProperties, $properties);
175
176
        foreach ($this->aliasedProperties as $propertyName => $propertyAlias) {
177
            $this->updatePropertyMappings($propertyName, $propertyAlias);
178
        }
179
    }
180
181
    /**
182
     * @param $properties
183
     */
184
    public function setProperties(array $properties)
185
    {
186
        $this->properties = $properties;
187
    }
188
189
    /**
190
     * @return array
191
     */
192
    public function getProperties()
193
    {
194
        return $this->properties;
195
    }
196
197
    /**
198
     * @return mixed
199
     */
200
    public function getClassName()
201
    {
202
        return $this->className;
203
    }
204
205
    /**
206
     */
207
    public function getResourceUrl()
208
    {
209
        return $this->resourceUrlPattern;
210
    }
211
212
    /**
213
     * @return array
214
     */
215
    public function getAliasedProperties()
216
    {
217
        return $this->aliasedProperties;
218
    }
219
220
    /**
221
     * @return array
222
     */
223
    public function getHiddenProperties()
224
    {
225
        return (array) $this->hiddenProperties;
226
    }
227
228
    /**
229
     * @param array $hidden
230
     */
231
    public function setHiddenProperties(array $hidden)
232
    {
233
        $this->hiddenProperties = \array_merge($this->hiddenProperties, $hidden);
234
    }
235
236
    /**
237
     * @return array
238
     */
239
    public function getRelationships()
240
    {
241
        return $this->relationships;
242
    }
243
244
    /**
245
     * @param array $relationships
246
     */
247
    public function addAdditionalRelationships(array $relationships)
248
    {
249
        $this->relationships = $relationships;
250
    }
251
252
    /**
253
     * @return array
254
     */
255
    public function getMetaData()
256
    {
257
        return $this->metaData;
258
    }
259
260
    /**
261
     * @param array $metaData
262
     */
263
    public function setMetaData(array $metaData)
264
    {
265
        $this->metaData = $metaData;
266
    }
267
268
    /**
269
     * @param string $key
270
     * @param        $value
271
     */
272
    public function addMetaData($key, $value)
273
    {
274
        $this->metaData[$key] = $value;
275
    }
276
277
    /**
278
     * @return string
279
     */
280
    public function getSelfUrl()
281
    {
282
        return $this->selfUrl;
283
    }
284
285
    /**
286
     * @param string $self
287
     *
288
     * @throws \InvalidArgumentException
289
     */
290
    public function setSelfUrl($self)
291
    {
292
        $this->selfUrl = (string) $self;
293
    }
294
295
    /**
296
     * @param $propertyName
297
     *
298
     * @return string
299
     */
300
    public function getRelatedUrl($propertyName)
301
    {
302
        return (!empty($this->relationshipSelfUrl[$propertyName]['related']))
303
            ? $this->relationshipSelfUrl[$propertyName]['related']
304
            : '';
305
    }
306
307
    /**
308
     * @param array $filterKeys
309
     */
310
    public function setFilterKeys(array $filterKeys)
311
    {
312
        $this->filterKeys = $filterKeys;
313
    }
314
315
    /**
316
     * @return array
317
     */
318
    public function getFilterKeys()
319
    {
320
        return (array) $this->filterKeys;
321
    }
322
323
    /**
324
     * @param string $propertyName
325
     * @param string $urls
326
     *
327
     * @return $this
328
     */
329
    public function setRelationshipUrls($propertyName, $urls)
330
    {
331
        $this->relationshipSelfUrl[$propertyName] = $urls;
332
333
        return $this;
334
    }
335
336
    /**
337
     * @param $propertyName
338
     *
339
     * @return string
340
     */
341
    public function getRelationshipSelfUrl($propertyName)
342
    {
343
        return (!empty($this->relationshipSelfUrl[$propertyName]['self']))
344
            ? $this->relationshipSelfUrl[$propertyName]['self']
345
            : '';
346
    }
347
348
    /**
349
     * @param array $urls
350
     */
351
    public function setUrls(array $urls)
352
    {
353
        $this->otherUrls = $urls;
354
    }
355
356
    /**
357
     * @return array
358
     */
359
    public function getUrls()
360
    {
361
        return $this->otherUrls;
362
    }
363
364
    /**
365
     * @param array $curies
366
     *
367
     * @throws MappingException
368
     */
369
    public function setCuries(array $curies)
370
    {
371
        if (empty($curies['name']) || empty($curies['href'])) {
372
            throw new MappingException('Curies must define "name" and "href" properties');
373
        }
374
375
        $this->curies = $curies;
376
    }
377
378
    /**
379
     * @return array
380
     */
381
    public function getCuries()
382
    {
383
        return $this->curies;
384
    }
385
386
387
    /**
388
     * Used by JSON API included resource filtering.
389
     *
390
     * @param $resource
391
     */
392
    public function addIncludedResource($resource) {
393
394
        $this->includedKeys[] = $resource;
395
    }
396
397
    /**
398
     * Returns the allowed included resources.
399
     *
400
     * @return array
401
     */
402
    public function getIncludedResources()
403
    {
404
        return $this->includedKeys;
405
    }
406
407
408
    /**
409
     * @param bool $filtering
410
     */
411
    public function filteringIncludedResources($filtering = true)
412
    {
413
        $this->filteringIncluded = $filtering;
414
    }
415
416
    /**
417
     * Returns true if included resource filtering has been set, false otherwise.
418
     *
419
     * @return bool
420
     */
421
    public function isFilteringIncludedResources()
422
    {
423
        return $this->filteringIncluded;
424
    }
425
}
426