Completed
Push — master ( 27649e...ef3cb7 )
by Dmitry
02:55
created

Mapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 5.83 %

Test Coverage

Coverage 87.69%

Importance

Changes 0
Metric Value
wmc 11
dl 7
loc 120
ccs 57
cts 65
cp 0.8769
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hp$0 ➔ __construct() 0 6 1
A hp$0 ➔ save() 0 11 3
A hp$1 ➔ hasChanges() 0 4 1
A create() 0 28 4
A find() 4 17 5
B findOne() 0 14 7
A findOrFail() 0 8 2
A getPlugin() 0 11 2
A getRepository() 0 7 2
A remove() 3 15 6

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 Basis\Test;
4
5
use Basis\Test;
6
use Exception;
7
use Tarantool\Mapper\Plugin\Spy;
8
9
class Mapper
10
{
11
    protected $test;
12
    public $serviceName;
13
14 1
    public function __construct(Test $test, $service)
15
    {
16 1
        $this->test = $test;
17 1
        $this->serviceName = $service;
18 1
    }
19
20 1
    public function create($space, $params)
21
    {
22 1
        $key = $this->serviceName.'.'.$space;
23 1
        if (array_key_exists($key, $this->test->data)) {
24
            return new class($params, $this->test, $key) {
25
                private $data;
26
                private $test;
27
                private $key;
28 1
                public function __construct($data, $test, $key)
29
                {
30 1
                    $this->data = $data;
31 1
                    $this->test = $test;
32 1
                    $this->key = $key;
33 1
                }
34 1
                public function save()
35
                {
36 1
                    if (!array_key_exists('id', $this->data)) {
37 1
                        $this->data['id'] = 1;
38 1
                        foreach ($this->test->data[$this->key] as $candidate) {
39 1
                            $this->data['id'] = max($this->data['id'], $candidate['id'] + 1);
40
                        }
41
                    }
42 1
                    $this->test->data[$this->key][] = $this->data;
43 1
                    return (object) $this->data;
44
                }
45
            };
46
        }
47
    }
48
49 2
    public function find(string $space, $params = [])
50
    {
51 2
        $key = $this->serviceName.'.'.$space;
52 2
        if (array_key_exists($key, $this->test->data)) {
53 2
            $data = $this->test->data[$key];
54 2
            foreach ($data as $i => $v) {
55 2 View Code Duplication
                if (count($params) && array_intersect_assoc($params, $v) != $params) {
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...
56 2
                    unset($data[$i]);
57 2
                    continue;
58
                }
59 2
                $data[$i] = (object) $v;
60
            }
61 2
            $data = array_values($data);
62 2
            return $data;
63
        }
64
        return [];
65
    }
66
67 2
    public function findOne(string $space, $params = [])
68
    {
69 2
        $key = $this->serviceName.'.'.$space;
70 2
        if (is_numeric($params) || is_string($params)) {
71 1
            $params = [ 'id' => $params ];
72
        }
73 2
        if (array_key_exists($key, $this->test->data)) {
74 2
            foreach ($this->test->data[$key] as $candidate) {
75 2
                if (!count($params) || array_intersect_assoc($params, $candidate) == $params) {
76 2
                    return (object) $candidate;
77
                }
78
            }
79
        }
80 2
    }
81
82 2
    public function findOrFail(string $space, $params = [])
83
    {
84 2
        $result = $this->findOne($space, $params);
85 2
        if (!$result) {
86 1
            throw new Exception("No ".$space.' found using '.json_encode($params));
87
        }
88 2
        return $result;
89
    }
90
91
    public function getPlugin($class)
92
    {
93
        if ($class == Spy::class) {
94
            return new class {
95
                public function hasChanges()
96
                {
97
                    return false;
98
                }
99
            };
100
        }
101
    }
102
103
    protected $repositores = [];
104
105 1
    public function getRepository($space)
106
    {
107 1
        if (!array_key_exists($space, $this->repositores)) {
108
            $this->repositores[$space] = new Repository($this, $space);
109
        }
110 1
        return $this->repositores[$space];
111
    }
112
113 1
    public function remove($space, $params)
114
    {
115 1
        if (is_object($params)) {
116 1
            $params = get_object_vars($params);
117
        }
118 1
        $key = $this->serviceName.'.'.$space;
119 1
        if (array_key_exists($key, $this->test->data)) {
120 1
            foreach ($this->test->data[$key] as $i => $v) {
121 1 View Code Duplication
                if (count($params) && array_intersect_assoc($params, $v) == $params) {
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...
122 1
                    unset($this->test->data[$key][$i]);
123
                }
124
            }
125 1
            $this->test->data[$key] = array_values($this->test->data[$key]);
126
        }
127
    }
128
}