Completed
Push — master ( 1d2d63...5f432b )
by Jens
09:03
created

Attribute   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 234
Duplicated Lines 13.68 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 43
c 1
b 1
f 0
lcom 1
cbo 3
dl 32
loc 234
ccs 82
cts 82
cp 1
rs 8.3157

18 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldDefinitions() 0 7 1
B fieldDefinition() 0 18 5
A getApiType() 0 18 3
A setAttributeDefinition() 0 6 1
A initialize() 0 9 2
B guessApiType() 0 24 3
A hasKeys() 0 8 2
A guessUnknown() 0 4 2
A guessTextLike() 0 4 2
A guessBool() 0 4 2
A guessNumber() 0 4 2
A guessEnum() 8 8 3
A guessLocalizedEnum() 8 8 3
A guessMoney() 8 8 2
A guessReference() 8 8 2
A guessLocalizedText() 0 8 3
A guessSet() 0 8 2
A guessNested() 0 11 3

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 Attribute 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 Attribute, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 * @created: 04.02.15, 17:46
5
 */
6
7
namespace Commercetools\Core\Model\Common;
8
9
use Commercetools\Core\Model\ProductType\AttributeDefinition;
10
use Commercetools\Core\Model\ProductType\AttributeType;
11
12
/**
13
 * @package Commercetools\Core\Model\Common
14
 * @link https://dev.commercetools.com/http-api-projects-products.html#product-variant-attribute
15
 * @method string getName()
16
 * @method getValue()
17
 * @method Attribute setName(string $name = null)
18
 * @method Attribute setValue($value = null)
19
 */
20
class Attribute extends JsonObject
21
{
22
    // identifiers for the Api Product Attribute Types:
23
    const T_UNKNOWN = 'unknown';  // zero, should evaluate to false
24
25
    const PROP_VALUE = "value";
26
    const PROP_KEY = "key";
27
    const PROP_NAME = "name";
28
    const PROP_CURRENCY_CODE = "currencyCode";
29
    const PROP_CENT_AMOUNT = "centAmount";
30
    const PROP_TYPE_ID = "typeId";
31
    const PROP_ID = "id";
32
    const PROP_LABEL = "label";
33
34
    const API_BOOL = 'boolean';
35
    const API_NUMBER = 'number';
36
    const API_TEXT = 'text';
37
    const API_LTEXT = 'ltext';
38
    const API_LENUM = 'lenum';
39
    const API_ENUM = 'enum';
40
    const API_MONEY = 'money';
41
    const API_DATE = 'date';
42
    const API_TIME = 'time';
43
    const API_DATETIME = 'datetime';
44
    const API_SET = 'set';
45
    const API_NESTED = 'nested';
46
    const API_REFERENCE = 'reference';
47
48
    protected static $types = [];
49
50 26
    public function fieldDefinitions()
51
    {
52
        return [
53 26
            static::PROP_NAME => [self::TYPE => 'string'],
54 26
            static::PROP_VALUE => [],
55
        ];
56
    }
57
58 26
    public function fieldDefinition($field)
59
    {
60 26
        if ($field == static::PROP_VALUE) {
61 25
            if (isset(static::$types[$this->getName()])) {
62 18
                $fieldDefinition = static::$types[$this->getName()];
63 18
                if (!$fieldDefinition instanceof AttributeDefinition) {
64
                    return null;
65
                }
66 18
                $fieldType = $fieldDefinition->getType();
67 18
                if (!$fieldType instanceof AttributeType) {
68
                    return null;
69
                }
70 18
                return $fieldType->fieldTypeDefinition();
71
            }
72 7
            return null;
73
        }
74 26
        return parent::fieldDefinition($field);
75
    }
76
77
    /**
78
     * @param $attributeName
79
     * @param $value
80
     * @return mixed
81
     */
82 18
    protected function getApiType($attributeName, $value)
83
    {
84 18
        if (isset(static::$types[$attributeName])) {
85 4
            return static::$types[$attributeName];
86
        }
87
88 14
        $apiType = $this->guessApiType($value);
89 14
        $definition = AttributeDefinition::of($this->getContextCallback());
90 14
        $definition->setName($attributeName);
91 14
        $definition->setType(AttributeType::fromArray(['name' => $apiType]));
92
93 14
        if ($apiType == static::API_SET) {
94 2
            $elementType = $this->guessApiType(current($value));
95 2
            $definition->getType()->setElementType(AttributeType::fromArray(['name' => $elementType]));
0 ignored issues
show
Documentation Bug introduced by
The method setElementType does not exist on object<Commercetools\Cor...ductType\AttributeType>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
96
        }
97 14
        $this->setAttributeDefinition($definition);
98 14
        return static::$types[$attributeName];
99
    }
100
101
    /**
102
     * @param AttributeDefinition $definition
103
     * @return $this
104
     */
105 18
    public function setAttributeDefinition(AttributeDefinition $definition)
106
    {
107 18
        static::$types[$definition->getName()] = $definition;
108
109 18
        return $this;
110
    }
111
112
    /**
113
     * @param string $field
114
     */
115 19
    protected function initialize($field)
116
    {
117 19
        if ($field == static::PROP_VALUE) {
118 18
            $name = $this->getRaw(static::PROP_NAME);
119 18
            $value = $this->getRaw(static::PROP_VALUE);
120 18
            $this->getApiType($name, $value);
121
        }
122 19
        parent::initialize($field);
123 19
    }
124
125
    /**
126
     * @param $value
127
     * @return string
128
     */
129 14
    protected function guessApiType($value)
130
    {
131
        $map = [
132 14
            'guessUnknown',
133
            'guessBool',
134
            'guessTextLike',
135
            'guessNumber',
136
            'guessEnum',
137
            'guessLocalizedEnum',
138
            'guessMoney',
139
            'guessReference',
140
            'guessLocalizedText',
141
            'guessNested',
142
            'guessSet',
143
        ];
144
145 14
        foreach ($map as $function) {
146 14
            if ($type = $this->$function($value)) {
147 14
                return $type;
148
            }
149
        }
150
151
        return static::T_UNKNOWN;
152
    }
153
154
    /**
155
     * @param $value
156
     * @param string[] $keys
157
     * @return bool
158
     */
159 8
    protected function hasKeys($value, $keys)
160
    {
161 8
        if (!is_array($value)) {
162 1
            return false;
163
        }
164 8
        $intersect = array_intersect_key(array_flip($keys), $value);
165 8
        return (count($intersect) == count($keys));
166
    }
167
168 14
    protected function guessUnknown($value)
169
    {
170 14
        return (!isset($value) ? static::T_UNKNOWN : null);
171
    }
172
173 12
    protected function guessTextLike($value)
174
    {
175 12
        return is_string($value) ? static::API_TEXT : null;
176
    }
177
178 13
    protected function guessBool($value)
179
    {
180 13
        return is_bool($value) ? static::API_BOOL : null;
181
    }
182
183 10
    protected function guessNumber($value)
184
    {
185 10
        return is_numeric($value) ? static::API_NUMBER : null;
186
    }
187
188 8 View Code Duplication
    protected function guessEnum($value)
0 ignored issues
show
Duplication introduced by
This method 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...
189
    {
190 8
        if ($this->hasKeys($value, [static::PROP_KEY, static::PROP_LABEL]) && is_string($value[static::PROP_LABEL])) {
191 2
            return static::API_ENUM;
192
        }
193
194 7
        return null;
195
    }
196
197 7 View Code Duplication
    protected function guessLocalizedEnum($value)
0 ignored issues
show
Duplication introduced by
This method 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...
198
    {
199 7
        if ($this->hasKeys($value, [static::PROP_KEY, static::PROP_LABEL]) && is_array($value[static::PROP_LABEL])) {
200 1
            return static::API_LENUM;
201
        }
202
203 6
        return null;
204
    }
205
206 6 View Code Duplication
    protected function guessMoney($value)
0 ignored issues
show
Duplication introduced by
This method 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...
207
    {
208 6
        if ($this->hasKeys($value, [static::PROP_CENT_AMOUNT, static::PROP_CURRENCY_CODE])) {
209 1
            return static::API_MONEY;
210
        }
211
212 5
        return null;
213
    }
214
215 5 View Code Duplication
    protected function guessReference($value)
0 ignored issues
show
Duplication introduced by
This method 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...
216
    {
217 5
        if ($this->hasKeys($value, [static::PROP_TYPE_ID, static::PROP_ID])) {
218 1
            return static::API_REFERENCE;
219
        }
220
221 4
        return null;
222
    }
223
224 4
    protected function guessLocalizedText($value)
225
    {
226 4
        if (is_array($value) && !is_numeric(key($value))) {
227 1
            return static::API_LTEXT;
228
        }
229
230 3
        return null;
231
    }
232
233 2
    protected function guessSet($value)
234
    {
235 2
        if (is_array($value)) {
236 2
            return static::API_SET;
237
        }
238
239
        return null;
240
    }
241
242 3
    protected function guessNested($value)
243
    {
244 3
        if (is_array($value)) {
245 3
            $first = reset($value);
246 3
            if ($this->hasKeys($first, [static::PROP_NAME, static::PROP_VALUE])) {
247 1
                return static::API_NESTED;
248
            }
249
        }
250
251 2
        return null;
252
    }
253
}
254