Completed
Push — master ( d14d6a...42d5bc )
by Dmitry
01:43
created

Spy   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 4.88 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 4
loc 82
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeRemove() 0 15 3
A reset() 0 7 1
A getKey() 4 12 3
A getChanges() 0 18 4
A hasChanges() 0 4 1
A afterCreate() 0 4 1
A afterUpdate() 0 9 2

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
declare(strict_types=1);
4
5
namespace Tarantool\Mapper\Plugin;
6
7
use Tarantool\Mapper\Entity;
8
use Tarantool\Mapper\Plugin;
9
use Tarantool\Mapper\Space;
10
11
class Spy extends Plugin
12
{
13
    private $created = [];
14
    private $updated = [];
15
    private $removed = [];
16
17
    public function afterCreate(Entity $instance, Space $space) : Entity
18
    {
19
        return $this->created[$this->getKey($instance, $space)] = $instance;
20
    }
21
22
    public function afterUpdate(Entity $instance, Space $space) : Entity
23
    {
24
        $key = $this->getKey($instance, $space);
25
        if (!array_key_exists($key, $this->created)) {
26
            $this->updated[$key] = $instance;
27
        }
28
29
        return $instance;
30
    }
31
32
    public function beforeRemove(Entity $instance, Space $space) : Entity
33
    {
34
        $key = $this->getKey($instance, $space);
35
36
        if (array_key_exists($key, $this->created)) {
37
            unset($this->created[$key]);
38
            return $instance;
39
        }
40
41
        if (array_key_exists($key, $this->updated)) {
42
            unset($this->updated[$key]);
43
        }
44
45
        return $this->removed[$key] = $instance;
46
    }
47
48
    public function reset() : self
49
    {
50
        $this->created = [];
51
        $this->updated = [];
52
        $this->removed = [];
53
        return $this;
54
    }
55
56
    private function getKey(Entity $instance, Space $space) : string
57
    {
58
        $key = [$space->getName()];
59
60
        $format = $space->getFormat();
61 View Code Duplication
        foreach ($space->getPrimaryIndex()['parts'] as $part) {
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...
62
            $field = array_key_exists(0, $part) ? $part[0] : $part['field'];
63
            $key[] = $instance->{$format[$field]['name']};
64
        }
65
66
        return implode(':', $key);
67
    }
68
69
    public function getChanges()
70
    {
71
        $result = (object) [];
72
73
        foreach (['created', 'updated', 'removed'] as $action) {
74
            $data = [];
75
            foreach ($this->$action as $key => $row) {
76
                list($space) = explode(':', $key);
77
                if (!array_key_exists($space, $data)) {
78
                    $data[$space] = [];
79
                }
80
                $data[$space][] = $row;
81
            }
82
            $result->$action = $data;
83
        }
84
85
        return $result;
86
    }
87
88
    public function hasChanges() : bool
89
    {
90
        return count($this->created) + count($this->updated) + count($this->removed) > 0;
91
    }
92
}
93