PimCatalogDateNormalizer::normalize()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 45
Code Lines 31

Duplication

Lines 45
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 45
loc 45
rs 8.439
cc 5
eloc 31
nc 4
nop 3
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 PimCatalogImage
10
 *
11
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer
12
 */
13 View Code Duplication
class PimCatalogDateNormalizer 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
32
        $availableLocales          = [];
33
        $attributeAvailableLocales = $attribute->getAvailableLocales();
34
        if (!is_null($attributeAvailableLocales)) {
35
            foreach ($attribute->getAvailableLocales() as $availableLocale) {
36
                $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...
37
            }
38
        }
39
40
        $normalizedAttribute['required']   = $attribute->isRequired();
41
        $normalizedAttribute['type']       = $attribute->getAttributeType();
42
        $normalizedAttribute['code']       = $attribute->getCode();
43
        $normalizedAttribute['parameters'] = [
44
          'scope'             => $attribute->isScopable(),
45
          'localizable'       => $attribute->isLocalizable(),
46
          'unique'            => $attribute->isUnique(),
47
          'locale_specific'   => '',
48
          'available_locales' => $availableLocales,
49
          'default_value'     => null,
50
          'date_min'          => $attribute->getDateMin(),
51
          'date_max'          => $attribute->getDateMax(),
52
        ];
53
54
        if ($attribute->isLocalizable()) {
55
            foreach ($attribute->getTranslations() as $trans) {
56
                $normalizedAttribute['labels'][$trans->getLocale(
57
                )] = $trans->getLabel();
58
            }
59
        } else {
60
            $normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel(
61
            );
62
        }
63
64
        return $normalizedAttribute;
65
    }
66
67
    /**
68
     * Checks whether the given class is supported for normalization by this normalizer
69
     *
70
     * @param mixed  $data   Data to normalize.
71
     * @param string $format The format being (de-)serialized from or into.
72
     *
73
     * @return boolean
74
     */
75
    public function supportsNormalization($data, $format = null)
76
    {
77
        return $data instanceof Attribute;
78
    }
79
}
80