Completed
Push — master ( 4dbb52...f13a61 )
by Christopher
03:07
created

Manager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 13.91 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 2 Features 2
Metric Value
wmc 8
c 3
b 2
f 2
lcom 1
cbo 1
dl 16
loc 115
ccs 0
cts 53
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 9 1
A first() 0 7 1
A get() 0 8 1
A interpolate() 0 8 1
A insert() 8 8 1
A update() 8 8 1
A delete() 0 8 1

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 AsyncPHP\Icicle\Database;
4
5
use Icicle\Coroutine;
6
use Icicle\Promise\PromiseInterface;
7
8
final class Manager
9
{
10
    /**
11
     * @var Connector
12
     */
13
    private $connector;
14
15
    /**
16
     * @var Builder
17
     */
18
    private $builder;
19
20
    /**
21
     * @param Connector $connector
22
     * @param Builder $builder
23
     */
24
    public function __construct(Connector $connector, Builder $builder)
25
    {
26
        $this->connector = $connector;
27
        $this->builder = $builder;
28
    }
29
30
    /**
31
     * @param string $method
32
     * @param array $parameters
33
     *
34
     * @return mixed
35
     */
36
    public function __call($method, array $parameters = [])
37
    {
38
        $builder = call_user_func_array([$this->builder, $method], $parameters);
39
40
        $clone = clone $this;
41
        $clone->builder = $builder;
42
43
        return $clone;
44
    }
45
46
    /**
47
     * @return PromiseInterface
48
     */
49
    public function first()
50
    {
51
        return Coroutine\create(function () {
52
            $rows = (yield $this->limit(1)->get());
53
            yield reset($rows);
54
        });
55
    }
56
57
    /**
58
     * @return PromiseInterface
59
     */
60
    public function get()
61
    {
62
        return Coroutine\create(function () {
63
            yield $this->connector->query(
64
                $this->interpolate($this->builder->build())
65
            );
66
        });
67
    }
68
69
    /**
70
     * @param array $build
71
     *
72
     * @return string
73
     */
74
    private function interpolate(array $build)
75
    {
76
        list($statement, $values) = $build;
77
78
        return preg_replace_callback("/\\:([_0-9a-zA-Z]+)/", function ($matches) use ($values) {
79
            return "'" . $this->connector->escape($values[$matches[1]]) . "'";
80
        }, $statement);
81
    }
82
83
    /**
84
     * @param array $data
85
     *
86
     * @return PromiseInterface
87
     */
88 View Code Duplication
    public function insert(array $data)
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...
89
    {
90
        return Coroutine\create(function () use ($data) {
91
            yield $this->connector->query(
92
                $this->interpolate($this->builder->insert($data)->build())
93
            );
94
        });
95
    }
96
97
    /**
98
     * @param array $data
99
     *
100
     * @return PromiseInterface
101
     */
102 View Code Duplication
    public function update(array $data)
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...
103
    {
104
        return Coroutine\create(function () use ($data) {
105
            yield $this->connector->query(
106
                $this->interpolate($this->builder->update($data)->build())
107
            );
108
        });
109
    }
110
111
    /**
112
     * @return PromiseInterface
113
     */
114
    public function delete()
115
    {
116
        return Coroutine\create(function () {
117
            yield $this->connector->query(
118
                $this->interpolate($this->builder->delete()->build())
119
            );
120
        });
121
    }
122
}
123