Completed
Push — develop ( 991012...b60d88 )
by Jens
08:17
created

Message   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 94.12%
Metric Value
wmc 6
lcom 0
cbo 1
dl 8
loc 48
ccs 16
cts 17
cp 0.9412
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A fieldDefinitions() 0 14 1
A fromArray() 0 10 3

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\Message;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
use Commercetools\Core\Model\Common\Reference;
11
use Commercetools\Core\Model\Common\DateTimeDecorator;
12
13
/**
14
 * @package Commercetools\Core\Model\Message
15
 * @link https://dev.commercetools.com/http-api-projects-messages.html#message
16
 * @method string getId()
17
 * @method Message setId(string $id = null)
18
 * @method DateTimeDecorator getCreatedAt()
19
 * @method Message setCreatedAt(\DateTime $createdAt = null)
20
 * @method int getSequenceNumber()
21
 * @method Message setSequenceNumber(int $sequenceNumber = null)
22
 * @method Reference getResource()
23
 * @method Message setResource(Reference $resource = null)
24
 * @method int getResourceVersion()
25
 * @method Message setResourceVersion(int $resourceVersion = null)
26
 * @method string getType()
27
 * @method Message setType(string $type = null)
28
 */
29
class Message extends JsonObject
30
{
31
    const MESSAGE_TYPE = '';
32
33
    /**
34
     * @param array $data
35
     * @param Context|callable $context
36
     */
37 3 View Code Duplication
    public function __construct(array $data = [], $context = null)
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...
38
    {
39 3
        parent::__construct($data, $context);
40 3
        $type = static::MESSAGE_TYPE;
41 3
        if (!empty($type)) {
42
            $this->setType(static::MESSAGE_TYPE);
43
        }
44 3
    }
45
46 2
    public function fieldDefinitions()
47
    {
48
        return [
49 2
            'id' => [static::TYPE => 'string'],
50
            'createdAt' => [
51 2
                static::TYPE => '\DateTime',
52 2
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
53
            ],
54 2
            'sequenceNumber' => [static::TYPE => 'int'],
55 2
            'resource' => [static::TYPE => '\Commercetools\Core\Model\Common\Reference'],
56 2
            'resourceVersion' => [static::TYPE => 'int'],
57 2
            'type' => [static::TYPE => 'string'],
58
        ];
59
    }
60
61
    /**
62
     * @param array $data
63
     * @param Context|callable $context
64
     * @return static
65
     */
66 1
    public static function fromArray(array $data, $context = null)
67
    {
68 1
        if (isset($data['type'])) {
69
            $className = '\Commercetools\Core\Model\Message\\' . ucfirst($data['type']) . 'Message';
70
            if (class_exists($className)) {
71
                return new $className($data, $context);
72
            }
73
        }
74 1
        return new static($data, $context);
75
    }
76
}
77