Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

Schema   C

Complexity

Total Complexity 42

Size/Duplication

Total Lines 471
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 42
lcom 7
cbo 1
dl 0
loc 471
rs 6.9315
c 1
b 0
f 0
ccs 0
cts 161
cp 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A setSearchable() 0 4 1
A getSearchable() 0 9 2
A setTitle() 0 4 1
A getTitle() 0 4 1
A setDescription() 0 4 1
A getDescription() 0 4 1
B setType() 0 21 5
A getType() 0 4 1
A getFormat() 0 4 1
A setFormat() 0 4 1
A setItems() 0 4 1
A getItems() 0 4 1
A setAdditionalProperties() 0 4 1
A getAdditionalProperties() 0 4 1
A setRequired() 0 4 1
A getRequired() 0 9 2
A setTranslatable() 0 8 2
A isTranslatable() 0 9 2
A setRefCollection() 0 4 1
A getRefCollection() 0 9 2
A setEventNames() 0 4 1
A getEventNames() 0 9 2
A setReadOnly() 0 4 1
A getReadOnly() 0 4 2
A __construct() 0 4 1
A removeProperty() 0 6 2
A getProperty() 0 4 1
A getProperties() 0 8 2
A addProperty() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Graviton Schema Document
4
 */
5
6
namespace Graviton\SchemaBundle\Document;
7
8
use \Doctrine\Common\Collections\ArrayCollection;
9
10
/**
11
 * Graviton\SchemaBundle\Document\Schema
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class Schema
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $title;
23
24
    /**
25
     * @var string
26
     */
27
    protected $description;
28
29
    /**
30
     * @var string
31
     */
32
    protected $type;
33
34
    /**
35
     * @var string
36
     */
37
    protected $format;
38
39
    /**
40
     * @var Schema
41
     */
42
    protected $items;
43
44
    /**
45
     * @var ArrayCollection
46
     */
47
    protected $properties;
48
49
    /**
50
     * @var Schema
51
     */
52
    protected $additionalProperties;
53
54
    /**
55
     * @var string[]
56
     */
57
    protected $required = array();
58
59
    /**
60
     * @var boolean
61
     */
62
    protected $translatable;
63
64
    /**
65
     * @var array
66
     */
67
    protected $refCollection = array();
68
69
    /**
70
     * possible event names this collection implements (queue events)
71
     *
72
     * @var array
73
     */
74
    protected $eventNames = array();
75
76
    /**
77
     * @var bool
78
     */
79
    protected $readOnly = false;
80
81
    /**
82
     * @var string[]
83
     */
84
    protected $searchable = array();
85
86
    /**
87
     * these are the BSON primitive types.
88
     * http://json-schema.org/latest/json-schema-core.html#anchor8
89
     * every type set *not* in this set will be carried over to 'format'
90
     *
91
     * @var string[]
92
     */
93
    protected $primitiveTypes = array(
94
        'array',
95
        'boolean',
96
        'integer',
97
        'number',
98
        'null',
99
        'object',
100
        'string'
101
    );
102
103
    /**
104
     * known non-primitive types we map to primitives here.
105
     * the type itself is set to the format.
106
     *
107
     * @var string[]
108
     */
109
    protected $specialTypeMapping = array(
110
        'extref' => 'string',
111
        'translatable' => 'object'
112
    );
113
114
    /**
115
     * Build properties
116
     */
117
    public function __construct()
118
    {
119
        $this->properties = new ArrayCollection();
120
    }
121
122
    /**
123
     * set title
124
     *
125
     * @param string $title title
126
     *
127
     * @return void
128
     */
129
    public function setTitle($title)
130
    {
131
        $this->title = $title;
132
    }
133
134
    /**
135
     * get title
136
     *
137
     * @return string
138
     */
139
    public function getTitle()
140
    {
141
        return $this->title;
142
    }
143
144
    /**
145
     * set description
146
     *
147
     * @param string $description description
148
     *
149
     * @return void
150
     */
151
    public function setDescription($description)
152
    {
153
        $this->description = $description;
154
    }
155
156
    /**
157
     * get description
158
     *
159
     * @return string
160
     */
161
    public function getDescription()
162
    {
163
        return $this->description;
164
    }
165
166
    /**
167
     * set type
168
     *
169
     * @param string $type type
170
     *
171
     * @return void
172
     */
173
    public function setType($type)
174
    {
175
        if ($type === 'int') {
176
            $type = 'integer';
177
        }
178
        if ($type === 'hash') {
179
            $type = 'object';
180
        }
181
182
        // handle non-primitive types
183
        if (!in_array($type, $this->primitiveTypes)) {
184
            $setType = 'string';
185
            if (isset($this->specialTypeMapping[$type])) {
186
                $setType = $this->specialTypeMapping[$type];
187
            }
188
            $this->type = $setType;
189
            $this->setFormat($type);
190
        } else {
191
            $this->type = $type;
192
        }
193
    }
194
195
    /**
196
     * get type
197
     *
198
     * @return string type
199
     */
200
    public function getType()
201
    {
202
        return $this->type;
203
    }
204
205
    /**
206
     * get format
207
     *
208
     * @return string format
209
     */
210
    public function getFormat()
211
    {
212
        return $this->format;
213
    }
214
215
    /**
216
     * sets format
217
     *
218
     * @param string $format format
219
     *
220
     * @return void
221
     */
222
    public function setFormat($format)
223
    {
224
        $this->format = $format;
225
    }
226
227
    /**
228
     * set items
229
     *
230
     * @param Schema $items items schema
231
     *
232
     * @return void
233
     */
234
    public function setItems($items)
235
    {
236
        $this->items = $items;
237
    }
238
239
    /**
240
     * get items
241
     *
242
     * @return Schema
243
     */
244
    public function getItems()
245
    {
246
        return $this->items;
247
    }
248
249
    /**
250
     * add a property
251
     *
252
     * @param string $name     property name
253
     * @param Schema $property property
254
     *
255
     * @return void
256
     */
257
    public function addProperty($name, $property)
258
    {
259
        $this->properties->set($name, $property);
260
    }
261
262
    /**
263
     * removes a property
264
     *
265
     * @param string $name property name
266
     *
267
     * @return void
268
     */
269
    public function removeProperty($name)
270
    {
271
        if (!$this->properties->containsKey($name)) {
272
            $this->properties->remove($this->properties->get($name));
273
        }
274
    }
275
276
    /**
277
     * returns a property
278
     *
279
     * @param string $name property name
280
     *
281
     * @return void|Schema property
282
     */
283
    public function getProperty($name)
284
    {
285
        return $this->properties->get($name);
286
    }
287
288
    /**
289
     * get properties
290
     *
291
     * @return ArrayCollection|null
292
     */
293
    public function getProperties()
294
    {
295
        if ($this->properties->isEmpty()) {
296
            return null;
297
        } else {
298
            return $this->properties;
299
        }
300
    }
301
302
    /**
303
     * set additionalProperties on schema
304
     *
305
     * @param Schema $schema schema to use for additionalProperties type
306
     *
307
     * @return void
308
     */
309
    public function setAdditionalProperties(Schema $schema)
310
    {
311
        $this->additionalProperties = $schema;
312
    }
313
314
    /**
315
     * get addtionalProperties for schema
316
     *
317
     * @return Schema
318
     */
319
    public function getAdditionalProperties()
320
    {
321
        return $this->additionalProperties;
322
    }
323
324
    /**
325
     * set required variables
326
     *
327
     * @param string[] $required arary of required fields
328
     *
329
     * @return void
330
     */
331
    public function setRequired($required)
332
    {
333
        $this->required = $required;
334
    }
335
336
    /**
337
     * get required fields
338
     *
339
     * @return string[]|null
340
     */
341
    public function getRequired()
342
    {
343
        $required = $this->required;
344
        if (empty($required)) {
345
            $required = null;
346
        }
347
348
        return $required;
349
    }
350
351
    /**
352
     * set translatable flag
353
     *
354
     * This flag is a local extension to json schema.
355
     *
356
     * @param boolean $translatable translatable flag
357
     *
358
     * @return void
359
     */
360
    public function setTranslatable($translatable)
361
    {
362
        if ($translatable === true) {
363
            $this->setType('translatable');
364
        } else {
365
            $this->setType('string');
366
        }
367
    }
368
369
    /**
370
     * get translatable flag
371
     *
372
     * @return boolean
373
     */
374
    public function isTranslatable()
375
    {
376
        $ret = false;
377
        if ($this->getFormat() == 'translatable') {
378
            $ret = true;
379
        }
380
381
        return $ret;
382
    }
383
384
    /**
385
     * set a array of urls that can extref refer to
386
     *
387
     * @param array $refCollection urls
388
     *
389
     * @return void
390
     */
391
    public function setRefCollection(array $refCollection)
392
    {
393
        $this->refCollection = $refCollection;
394
    }
395
396
    /**
397
     * get a collection of urls that can extref refer to
398
     *
399
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
400
     */
401
    public function getRefCollection()
402
    {
403
        $collection = $this->refCollection;
404
        if (empty($collection)) {
405
            $collection = null;
406
        }
407
408
        return $collection;
409
    }
410
411
    /**
412
     * set an array of possible event names
413
     *
414
     * @param array $eventNames event names
415
     *
416
     * @return void
417
     */
418
    public function setEventNames(array $eventNames)
419
    {
420
        $this->eventNames = array_values($eventNames);
421
    }
422
423
    /**
424
     * get a collection of possible event names
425
     *
426
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
427
     */
428
    public function getEventNames()
429
    {
430
        $collection = $this->eventNames;
431
        if (empty($collection)) {
432
            $collection = null;
433
        }
434
435
        return $collection;
436
    }
437
438
    /**
439
     * Set the readOnly flag
440
     *
441
     * @param bool $readOnly ReadOnly flag
442
     *
443
     * @return void
444
     */
445
    public function setReadOnly($readOnly)
446
    {
447
        $this->readOnly = (bool) $readOnly;
448
    }
449
450
    /**
451
     * Get the readOnly flag.
452
     * Returns null if the flag is set to false so the serializer will ignore it.
453
     *
454
     * @return bool|null true if readOnly isset to true or null if not
455
     */
456
    public function getReadOnly()
457
    {
458
        return $this->readOnly ? true : null;
459
    }
460
461
    /**
462
     * set searchable variables
463
     *
464
     * @param string[] $searchable arary of searchable fields
465
     *
466
     * @return void
467
     */
468
    public function setSearchable($searchable)
469
    {
470
        $this->searchable = $searchable;
471
    }
472
473
    /**
474
     * get searchable fields
475
     *
476
     * @return string[]|null
477
     */
478
    public function getSearchable()
479
    {
480
        $searchable = $this->searchable;
481
        if (empty($searchable)) {
482
            $searchable = null;
483
        }
484
485
        return $searchable;
486
    }
487
}
488