Completed
Pull Request — master (#269)
by greg
03:09
created

QuizAnswer   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 425
Duplicated Lines 3.53 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 49
c 1
b 0
f 0
lcom 3
cbo 2
dl 15
loc 425
rs 8.5455

30 Methods

Rating   Name   Duplication   Size   Complexity  
A createChrono() 0 5 1
A updateChrono() 0 4 1
A getId() 0 4 1
A setId() 0 6 1
A getQuestion() 0 4 1
A setQuestion() 0 6 1
A getAnswer() 0 4 1
A setAnswer() 0 6 1
A getExplanation() 0 4 1
A setExplanation() 0 6 1
A getJsonData() 0 4 1
A setJsonData() 0 6 1
A getVideo() 0 4 1
A setVideo() 0 6 1
A getImage() 0 4 1
A setImage() 0 6 1
A getPoints() 0 4 1
A setPoints() 0 6 1
A getPosition() 0 4 1
A setPosition() 0 6 1
A getCorrect() 0 4 1
A setCorrect() 0 6 1
A getCreatedAt() 0 4 1
A setCreatedAt() 0 6 1
A getUpdatedAt() 0 4 1
A setUpdatedAt() 0 6 1
A getArrayCopy() 0 6 1
F populate() 15 38 19
A setInputFilter() 0 4 1
A getInputFilter() 0 64 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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

1
<?php
2
namespace PlaygroundGame\Entity;
3
4
use Doctrine\ORM\Mapping as ORM;
5
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
6
use Doctrine\ORM\Mapping\PrePersist;
7
use Doctrine\ORM\Mapping\PreUpdate;
8
use Zend\InputFilter\InputFilter;
9
use Zend\InputFilter\Factory as InputFactory;
10
use Zend\InputFilter\InputFilterAwareInterface;
11
use Zend\InputFilter\InputFilterInterface;
12
13
/**
14
 * @ORM\Entity @HasLifecycleCallbacks
15
 * @ORM\Table(name="game_quiz_answer")
16
 */
17
class QuizAnswer implements InputFilterAwareInterface
18
{
19
    protected $inputFilter;
20
21
    /**
22
     * @ORM\Id
23
     * @ORM\Column(type="integer");
24
     * @ORM\GeneratedValue(strategy="AUTO")
25
     */
26
    protected $id;
27
28
    /**
29
     * @ORM\ManyToOne(targetEntity="QuizQuestion", inversedBy="answers")
30
     *
31
     **/
32
    protected $question;
33
34
    /**
35
     * @ORM\Column(type="text", nullable=true)
36
     */
37
    protected $answer;
38
39
    /**
40
     * Explanation of the answer
41
     * @ORM\Column(type="text", nullable=true)
42
     */
43
    protected $explanation;
44
45
    /**
46
     * @ORM\Column(name="json_data", type="text", nullable=true)
47
     */
48
    protected $jsonData;
49
50
    /**
51
     * @ORM\Column(type="string", nullable=true)
52
     */
53
    protected $video;
54
55
    /**
56
     * @ORM\Column(type="string", nullable=true)
57
     */
58
    protected $image;
59
60
    /**
61
     * The answer score in the game
62
     * @ORM\Column(type="integer", nullable=true)
63
     */
64
    protected $points = 0;
65
66
    /**
67
     * @ORM\Column(type="integer", nullable=false)
68
     */
69
    protected $position = 0;
70
71
    /**
72
     *
73
     * @ORM\Column(type="boolean", nullable=true)
74
     */
75
    protected $correct = 0;
76
77
    /**
78
     * @ORM\Column(type="datetime")
79
     */
80
    protected $created_at;
81
82
    /**
83
     * @ORM\Column(type="datetime")
84
     */
85
    protected $updated_at;
86
87
    /** @PrePersist */
88
    public function createChrono()
89
    {
90
        $this->created_at = new \DateTime("now");
91
        $this->updated_at = new \DateTime("now");
92
    }
93
94
    /** @PreUpdate */
95
    public function updateChrono()
96
    {
97
        $this->updated_at = new \DateTime("now");
98
    }
99
100
    /**
101
     * @return the unknown_type
102
     */
103
    public function getId()
104
    {
105
        return $this->id;
106
    }
107
108
    /**
109
     * @param unknown_type $id
110
     */
111
    public function setId($id)
112
    {
113
        $this->id = $id;
114
115
        return $this;
116
    }
117
118
    /**
119
     * @return the unknown_type
120
     */
121
    public function getQuestion()
122
    {
123
        return $this->question;
124
    }
125
126
    /**
127
     * @param unknown_type $question
128
     */
129
    public function setQuestion($question)
130
    {
131
        $this->question = $question;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @return the unknown_type
138
     */
139
    public function getAnswer()
140
    {
141
        return $this->answer;
142
    }
143
144
    /**
145
     * @param unknown_type $answer
146
     */
147
    public function setAnswer($answer)
148
    {
149
        $this->answer = $answer;
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return the unknown_type
156
     */
157
    public function getExplanation()
158
    {
159
        return $this->explanation;
160
    }
161
162
    /**
163
     * @param unknown_type $explanation
164
     */
165
    public function setExplanation($explanation)
166
    {
167
        $this->explanation = $explanation;
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return the string
174
     */
175
    public function getJsonData()
176
    {
177
        return $this->jsonData;
178
    }
179
180
    /**
181
     * @param string $hint
0 ignored issues
show
Bug introduced by
There is no parameter named $hint. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
182
     */
183
    public function setJsonData($jsonData)
184
    {
185
        $this->jsonData = $jsonData;
186
187
        return $this;
188
    }
189
190
    /**
191
     * @return the unknown_type
192
     */
193
    public function getVideo()
194
    {
195
        return $this->video;
196
    }
197
198
    /**
199
     * @param unknown_type $video
200
     */
201
    public function setVideo($video)
202
    {
203
        $this->video = $video;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return the unknown_type
210
     */
211
    public function getImage()
212
    {
213
        return $this->image;
214
    }
215
216
    /**
217
     * @param unknown_type $image
218
     */
219
    public function setImage($image)
220
    {
221
        $this->image = $image;
222
223
        return $this;
224
    }
225
226
    /**
227
     * @return integer unknown_type
228
     */
229
    public function getPoints()
230
    {
231
        return $this->points;
232
    }
233
234
    /**
235
     * @param unknown_type $points
236
     */
237
    public function setPoints($points)
238
    {
239
        $this->points = $points;
0 ignored issues
show
Documentation Bug introduced by
It seems like $points of type object<PlaygroundGame\Entity\unknown_type> is incompatible with the declared type integer of property $points.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
240
241
        return $this;
242
    }
243
244
    /**
245
     * @return integer unknown_type
246
     */
247
    public function getPosition()
248
    {
249
        return $this->position;
250
    }
251
252
    /**
253
     * @param unknown_type $position
254
     */
255
    public function setPosition($position)
256
    {
257
        $this->position = $position;
0 ignored issues
show
Documentation Bug introduced by
It seems like $position of type object<PlaygroundGame\Entity\unknown_type> is incompatible with the declared type integer of property $position.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
258
259
        return $this;
260
    }
261
262
    /**
263
     * @return integer unknown_type
264
     */
265
    public function getCorrect()
266
    {
267
        return $this->correct;
268
    }
269
270
    /**
271
     * @param unknown_type $correct
272
     */
273
    public function setCorrect($correct)
274
    {
275
        $this->correct = $correct;
0 ignored issues
show
Documentation Bug introduced by
It seems like $correct of type object<PlaygroundGame\Entity\unknown_type> is incompatible with the declared type integer of property $correct.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
276
277
        return $this;
278
    }
279
280
    /**
281
     * @return the unknown_type
282
     */
283
    public function getCreatedAt()
284
    {
285
        return $this->created_at;
286
    }
287
288
    /**
289
     * @param unknown_type $created_at
290
     */
291
    public function setCreatedAt($created_at)
292
    {
293
        $this->created_at = $created_at;
294
295
        return $this;
296
    }
297
298
    /**
299
     * @return the unknown_type
300
     */
301
    public function getUpdatedAt()
302
    {
303
        return $this->updated_at;
304
    }
305
306
    /**
307
     * @param unknown_type $updated_at
308
     */
309
    public function setUpdatedAt($updated_at)
310
    {
311
        $this->updated_at = $updated_at;
312
313
        return $this;
314
    }
315
316
    /**
317
     * Convert the object to an array.
318
     *
319
     * @return array
320
     */
321
    public function getArrayCopy()
322
    {
323
        $obj_vars = get_object_vars($this);
324
325
        return $obj_vars;
326
    }
327
328
    /**
329
     * Populate from an array.
330
     *
331
     * @param array $data
332
     */
333
    public function populate($data = array())
334
    {
335
        if (isset($data['answer']) && $data['answer'] !== null) {
336
            $this->answer = $data['answer'];
337
        }
338
339
        if (isset($data['explanation']) && $data['explanation'] !== null) {
340
            $this->explanation = $data['explanation'];
341
        }
342
343 View Code Duplication
        if (isset($data['jsonData']) && $data['jsonData'] !== null) {
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...
344
            $this->jsonData = $data['jsonData'];
345
        }
346
347 View Code Duplication
        if (isset($data['type']) && $data['type'] !== null) {
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...
348
            $this->type = $data['type'];
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
349
        }
350
351 View Code Duplication
        if (isset($data['position']) && $data['position'] !== null) {
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...
352
            $this->position = $data['position'];
353
        }
354
355 View Code Duplication
        if (isset($data['image']) && $data['image'] !== null) {
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...
356
            $this->image = $data['image'];
357
        }
358
359 View Code Duplication
        if (isset($data['video']) && $data['video'] !== null) {
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...
360
            $this->video = $data['video'];
361
        }
362
363
        if (isset($data['points']) && $data['points'] !== null) {
364
            $this->points = $data['points'];
365
        }
366
367
        if (isset($data['correct']) && $data['correct'] !== null) {
368
            $this->correct = $data['correct'];
369
        }
370
    }
371
372
    public function setInputFilter(InputFilterInterface $inputFilter)
373
    {
374
        throw new \Exception("Not used");
375
    }
376
377
    public function getInputFilter()
378
    {
379
        if (!$this->inputFilter) {
380
            $inputFilter = new InputFilter();
381
            $factory = new InputFactory();
382
383
            $inputFilter->add($factory->createInput(array(
384
                'name'       => 'id',
385
                'required'   => true,
386
                'filters' => array(
387
                    array('name'    => 'Int'),
388
                ),
389
            )));
390
391
            $inputFilter->add($factory->createInput(array(
392
                'name'     => 'answer',
393
                'required' => true,
394
            )));
395
396
            $inputFilter->add($factory->createInput(array(
397
                'name'     => 'position',
398
                'required' => true,
399
            )));
400
401
            $inputFilter->add($factory->createInput(array(
402
                'name'     => 'explanation',
403
                'required' => false,
404
            )));
405
406
            $inputFilter->add($factory->createInput(array(
407
                'name'     => 'correct',
408
                'required' => true,
409
                'validators' => array(
410
                    array(
411
                        'name'    => 'Between',
412
                        'options' => array(
413
                            'min'      => 0,
414
                            'max'      => 1,
415
                        ),
416
                    ),
417
                ),
418
            )));
419
420
            $inputFilter->add($factory->createInput(array(
421
                    'name'     => 'video',
422
                    'required' => false,
423
            )));
424
425
            $inputFilter->add($factory->createInput(array(
426
                'name'     => 'image',
427
                'required' => false,
428
            )));
429
430
            $inputFilter->add($factory->createInput(array(
431
                'name'     => 'jsonData',
432
                'required' => false,
433
                'allowEmpty' => true,
434
            )));
435
436
            $this->inputFilter = $inputFilter;
437
        }
438
439
        return $this->inputFilter;
440
    }
441
}
442