Completed
Push — master ( f74f27...ebdf9f )
by Dmitry
05:43 queued 03:08
created

Entity   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 14.14 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88%

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 20
c 7
b 0
f 3
lcom 1
cbo 0
dl 14
loc 99
ccs 44
cts 50
cp 0.88
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A update() 0 5 1
A getId() 0 4 1
A __set() 0 8 3
A __get() 0 6 2
A setId() 0 6 1
A getData() 6 12 3
A pullChanges() 0 14 4
A toArray() 8 14 4

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 Tarantool\Mapper;
4
5
use LogicException;
6
7
class Entity implements Contracts\Entity
8
{
9
    protected $original = [];
10
    protected $data = [];
11
12 50
    public function __construct(array $data = null)
13
    {
14 50
        $this->update($data);
15 50
    }
16
17 50
    public function update($data)
18
    {
19 50
        $this->original = $data;
20 50
        $this->data = $data;
21 50
    }
22
23 50
    public function __set($key, $value)
24
    {
25 50
        if ($key == 'id' && $this->getId()) {
26 1
            throw new LogicException('Id property is readonly');
27
        }
28
29 50
        $this->data[$key] = $value;
30 50
    }
31
32 50
    public function __get($key)
33
    {
34 50
        if (array_key_exists($key, $this->data)) {
35 50
            return $this->data[$key];
36
        }
37 50
    }
38
39
    /**
40
     * @return int
41
     */
42 50
    public function getId()
43
    {
44 50
        return $this->__get('id');
45
    }
46
47
    /**
48
     * @return Entity
49
     */
50 50
    public function setId($id)
51
    {
52 50
        $this->__set('id', $id);
53
54 50
        return $this;
55
    }
56
57
    /**
58
     * @return array
59
     */
60 49
    public function toArray($recursive = false)
61
    {
62 49
        $array = [];
63 49 View Code Duplication
        foreach (array_keys($this->data) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64 49
            $array[$key] = $this->__get($key);
65 49
            if ($array[$key] instanceof Contracts\Entity) {
66
                if ($recursive) {
67
                    $array[$key] = $array[$key]->toArray(true);
68
                }
69
            }
70 49
        }
71
72 49
        return $array;
73
    }
74
75 1
    public function getData()
76
    {
77 1
        $data = [];
78 1 View Code Duplication
        foreach (array_keys($this->data) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
79 1
            $data[$key] = $this->__get($key);
80 1
            if ($data[$key] instanceof Contracts\Entity) {
81
                $data[$key] = $data[$key]->id;
82
            }
83 1
        }
84
85 1
        return $data;
86
    }
87
88
    /**
89
     * @return array
90
     */
91 11
    public function pullChanges()
92
    {
93 11
        $changes = [];
94
95 11
        foreach ($this->data as $key => $value) {
96 11
            if (!array_key_exists($key, $this->original) || $this->original[$key] != $value) {
97 11
                $changes[$key] = $value;
98 11
            }
99 11
        }
100
101 11
        $this->original = $this->data;
102
103 11
        return $changes;
104
    }
105
}
106