AttributeNormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 25.42 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 15
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A normalize() 15 21 2
A supportsNormalization() 0 4 1

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 Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer;
4
5
use Pim\Bundle\CatalogBundle\Entity\Attribute;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Exception\NormalizeException;
8
use Actualys\Bundle\DrupalCommerceConnectorBundle\Guesser\NormalizerGuesser;
9
10
class AttributeNormalizer implements NormalizerInterface
11
{
12
    /**
13
     * @var NormalizerGuesser $normalizerGuesser
14
     */
15
    protected $normalizerGuesser;
16
17
    /**
18
     * @param NormalizerGuesser $normalizerGuesser
19
     */
20
    public function __construct(
21
      NormalizerGuesser $normalizerGuesser
22
    ) {
23
        $this->normalizerGuesser = $normalizerGuesser;
24
    }
25
26
    /**
27
     * @param  object                                                                          $attribute
28
     * @param  null                                                                            $format
29
     * @param  array                                                                           $context
30
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
31
     * @throws NormalizeException
32
     * @throws \Actualys\Bundle\DrupalCommerceConnectorBundle\Guesser\Exception\GuessException
33
     */
34
    public function normalize($attribute, $format = null, array $context = [])
35
    {
36
        $type = $attribute->getAttributeType();
37 View Code Duplication
        if ($normalizer = $this->normalizerGuesser->guessNormalizer(
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...
38
          $type,
39
          'attribute'
40
        )
41
        ) {
42
            $normalizedAttribute = $normalizer->normalize(
43
              $attribute,
44
              null,
45
              $context
46
            );
47
        } else {
48
            throw new NormalizeException(
49
              'Type field not supported: "'.$type.'".'
50
            );
51
        }
52
53
        return $normalizedAttribute;
54
    }
55
56
    /**
57
     * Checks whether the given class is supported for normalization by this normalizer
58
     *
59
     * @param mixed  $data   Data to normalize.
60
     * @param string $format The format being (de-)serialized from or into.
61
     *
62
     * @return boolean
63
     */
64
    public function supportsNormalization($data, $format = null)
65
    {
66
        return $data instanceof Attribute;
67
    }
68
}
69