Completed
Push — dev ( a70858...5b49da )
by nonanerz
06:08 queued 06:04
created

QuestionNormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 35.7%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 41
loc 41
ccs 5
cts 14
cp 0.357
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A supportsNormalization() 4 4 1
A normalize() 15 15 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
namespace AppBundle\Serializer;
4
5
use AppBundle\Entity\Survey\SurveyQuestion;
6
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
7
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
8
use Symfony\Component\Serializer\Exception\LogicException;
9
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
10
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
11
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
13
14 View Code Duplication
class QuestionNormalizer extends ObjectNormalizer
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    /**
17
     * SurveyNormalizer constructor.
18
     *
19
     * @param ClassMetadataFactoryInterface|null  $classMDF
20
     * @param NameConverterInterface|null         $nameCv
21
     * @param PropertyAccessorInterface|null      $propAs
22
     * @param PropertyTypeExtractorInterface|null $propTE
23
     */
24 5
    public function __construct($classMDF, $nameCv, $propAs, $propTE)
25
    {
26 5
        parent::__construct($classMDF, $nameCv, $propAs, $propTE);
27 5
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 5
    public function supportsNormalization($data, $format = null)
33
    {
34 5
        return $data instanceof SurveyQuestion;
35
    }
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function normalize($object, $format = null, array $context = [])
40
    {
41
        if (!$this->serializer instanceof NormalizerInterface) {
42
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
43
        }
44
        /** @var SurveyQuestion $question */
45
        $question = &$object;
46
47
        return $this->serializer->normalize(new \ArrayObject([
48
            'id' => $question->getId(),
49
            'title' => $question->getTitle(),
50
            'orderNumber' => $question->getOrderNumber(),
51
            'variants' => $question->getVariants(),
52
        ]), $format, $context);
53
    }
54
}
55