Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Builder   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 106
Duplicated Lines 7.55 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 8
loc 106
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 4

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A connection() 0 4 1
A getConnection() 0 3 2
A model() 0 4 1
A where() 8 8 3
A createQuery() 0 14 3
A get() 0 8 3
A getList() 0 8 3
A insert() 0 4 1
A update() 0 4 1
A delete() 0 4 1
A setDbOption() 0 4 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 Inji\Model;
4
5
use Inji\App;
6
use Inji\Tools;
7
8
class Builder {
9
    public $connection = 'default';
10
    public $modelName = '';
11
    public $whereArray = [];
12
    public $dbOptions = [];
13
    /**
14
     * @var \Inji\App
15
     */
16
    public $app;
17
18
    public function __construct(string $modelName, App $app = null) {
19
        $this->modelName = $modelName;
20
        if (is_null($app)) {
21
            $this->app = App::$primary;
22
        } else {
23
            $this->app = $app;
24
        }
25
    }
26
27
    public function connection($connection = 'default') {
28
        $this->connection = $connection;
29
        return $this;
30
    }
31
32
    public function getConnection() {
33
        return is_string($this->connection) ? $this->app->db($this->connection) : $this->connection;
34
    }
35
36
    public function model(string $modelName) {
37
        $this->modelName = $modelName;
38
        return $this;
39
    }
40
41 View Code Duplication
    public function where($col, $value = true, $comparision = '=', $concatenation = 'AND') {
42
        if (is_array($col) && !Tools::isAssoc($col)) {
43
            $this->whereArray[] = $col;
44
        } else {
45
            $this->whereArray[] = [$col, $value, $comparision, $concatenation];
46
        }
47
        return $this;
48
    }
49
50
    /**
51
     * @return \Inji\Db\DriverQuery
52
     */
53
    public function createQuery() {
54
        /**
55
         * @var \Inji\Db\DriverQuery
56
         */
57
        $query = $this->getConnection()->newQuery();
58
        foreach ($this->dbOptions as $dbOption => $value) {
59
            $query->setDbOption($dbOption, $value);
60
        }
61
        if ($this->whereArray) {
62
            $query->where($this->whereArray);
63
        }
64
        $query->setTable($this->modelName::table());
0 ignored issues
show
Bug introduced by
The method table cannot be called on $this->modelName (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
65
        return $query;
66
    }
67
68
    /**
69
     * @param array $options
70
     * @return bool||\Inji\Model
0 ignored issues
show
Documentation introduced by
The doc-type bool||\Inji\Model could not be parsed: Unknown type name "|" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
71
     */
72
    public function get($options = []) {
73
        $query = $this->createQuery();
74
        $result = $query->select();
75
        if (!$result) {
76
            return false;
77
        }
78
        return $result->fetch(empty($options['array']) ? $this->modelName : '');
79
    }
80
81
    /**
82
     * @param array $options
83
     * @return bool||\Inji\Model
0 ignored issues
show
Documentation introduced by
The doc-type bool||\Inji\Model could not be parsed: Unknown type name "|" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
84
     */
85
    public function getList($options = []) {
86
        $query = $this->createQuery();
87
        $result = $query->select();
88
        if (!$result) {
89
            return false;
90
        }
91
        return $result->fetchAll(empty($options['array']) ? $this->modelName : '', $this->modelName::index());
0 ignored issues
show
Bug introduced by
The method index cannot be called on $this->modelName (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
92
    }
93
94
    public function insert($values) {
95
        $query = $this->createQuery();
96
        return $query->insert('', $values);
97
    }
98
99
    public function update($values) {
100
        $query = $this->createQuery();
101
        return $query->update('', $values);
102
    }
103
104
    public function delete() {
105
        $query = $this->createQuery();
106
        return $query->delete('');
107
    }
108
109
    public function setDbOption($optionName, $value) {
110
        $this->dbOptions[$optionName] = $value;
111
        return $this;
112
    }
113
}