PimCatalogPriceCollectionNormalizer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 1
dl 61
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 39 39 5
A supportsNormalization() 4 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\Attribute;
4
5
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6
use Pim\Bundle\CatalogBundle\Entity\Attribute;
7
8
/**
9
 * Class PimCatalogPriceCollection
10
 *
11
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer
12
 */
13 View Code Duplication
class PimCatalogPriceCollectionNormalizer implements NormalizerInterface
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...
14
{
15
    /**
16
     * @param  object                                                $attribute
17
     * @param  null                                                  $format
18
     * @param  array                                                 $context
19
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
20
     */
21
    public function normalize($attribute, $format = null, array $context = [])
22
    {
23
        /**@var Attribute $attribute * */
24
        $normalizedAttribute       = [
25
          'code'       => null,
26
          'type'       => null,
27
          'required'   => null,
28
          'labels'     => null,
29
          'parameters' => null,
30
        ];
31
        $availableLocales          = [];
32
        $attributeAvailableLocales = $attribute->getAvailableLocales();
33
        if (!is_null($attributeAvailableLocales)) {
34
            foreach ($attribute->getAvailableLocales() as $availableLocale) {
35
                $availableLocales [] = $availableLocale;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$availableLocales  was never initialized. Although not strictly required by PHP, it is generally a good practice to add $availableLocales  = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
36
            }
37
        }
38
39
        $normalizedAttribute['required']   = $attribute->isRequired();
40
        $normalizedAttribute['type']       = $attribute->getAttributeType();
41
        $normalizedAttribute['code']       = $attribute->getCode();
42
        $normalizedAttribute['parameters'] = [
43
          'scope'             => $attribute->isScopable(),
44
          'localizable'       => $attribute->isLocalizable(),
45
          'available_locales' => $availableLocales,
46
        ];
47
48
        if ($attribute->isLocalizable()) {
49
            foreach ($attribute->getTranslations() as $trans) {
50
                $normalizedAttribute['labels'][$trans->getLocale(
51
                )] = $trans->getLabel();
52
            }
53
        } else {
54
            $normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel(
55
            );
56
        }
57
58
        return $normalizedAttribute;
59
    }
60
61
    /**
62
     * Checks whether the given class is supported for normalization by this normalizer
63
     *
64
     * @param mixed  $data   Data to normalize.
65
     * @param string $format The format being (de-)serialized from or into.
66
     *
67
     * @return boolean
68
     */
69
    public function supportsNormalization($data, $format = null)
70
    {
71
        return $data instanceof Attribute;
72
    }
73
}
74