Completed
Branch master (eb6d5f)
by Dmitry
02:18
created

Entity   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 103
Duplicated Lines 13.59 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 79.25%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 21
c 6
b 0
f 3
lcom 1
cbo 0
dl 14
loc 103
ccs 42
cts 53
cp 0.7925
rs 10

9 Methods

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