Completed
Push — master ( 18bfc7...82e0b5 )
by Jens
18:00 queued 05:19
created

GeoLocation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 50
loc 50
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A fieldDefinitions() 7 7 1
A fromArray() 10 10 3
A getGeoTypeByTypeName() 5 5 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
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Common;
7
8
/**
9
 * @package Commercetools\Core\Model\Common
10
 *
11
 * @method string getType()
12
 * @method GeoLocation setType(string $type = null)
13
 * @method array getCoordinates()
14
 * @method GeoLocation setCoordinates(array $coordinates = null)
15
 */
16 View Code Duplication
class GeoLocation extends JsonObject
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...
17
{
18
    const TYPE_NAME = 'Point';
19
20
    /**
21
     * @param array $data
22
     * @param Context|callable $context
23
     */
24 3
    public function __construct(array $data = [], $context = null)
25
    {
26 3
        parent::__construct($data, $context);
27 3
        $name = static::TYPE_NAME;
28 3
        if (!empty($name)) {
29 3
            $this->setType(static::TYPE_NAME);
30
        }
31 3
    }
32
33
    /**
34
     * @return array
35
     */
36 3
    public function fieldDefinitions()
37
    {
38
        return [
39 3
            'type' => [static::TYPE => 'string'],
40 3
            'coordinates' => [static::TYPE => 'array']
41
        ];
42
    }
43
44
    /**
45
     * @param array $data
46
     * @param Context|callable $context
47
     * @return static
48
     */
49 1
    public static function fromArray(array $data, $context = null)
50
    {
51 1
        if (isset($data['type'])) {
52 1
            $className = static::getGeoTypeByTypeName($data['type']);
53 1
            if (class_exists($className)) {
54 1
                return new $className($data, $context);
55
            }
56
        }
57
        return new static($data, $context);
58
    }
59
60 1
    protected static function getGeoTypeByTypeName($apiType)
61
    {
62 1
        $className = '\Commercetools\Core\Model\Common\\Geo' . ucfirst($apiType);
63 1
        return $className;
64
    }
65
}
66