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

Query   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 59
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 59
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDbOption() 0 3 1
A where() 8 8 3
A setTable() 0 3 1
A select() 0 6 2
A insert() 0 6 2
A update() 0 6 2
A delete() 0 6 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
 * Created by IntelliJ IDEA.
4
 * User: inji
5
 * Date: 07.01.2018
6
 * Time: 16:39
7
 */
8
9
namespace Inji\Db\InjiStorage;
10
11
use \Inji\Tools;
12
13
class Query implements \Inji\Db\DriverQuery {
14
    /**
15
     * @var \Inji\Db\InjiStorage
16
     */
17
    public $connection;
18
    public $whereArray = [];
19
    public $table = [];
20
    public $dbOptions = [];
21
22
    public function __construct($connection) {
23
        $this->connection = $connection;
24
    }
25
26
    public function setDbOption($name, $value) {
27
        $this->dbOptions[$name] = $value;
28
    }
29
30 View Code Duplication
    public function where($col, $value = true, $comparision = '=', $concatenation = 'AND') {
31
        if (is_array($col) && !Tools::isAssoc($col)) {
32
            $this->whereArray[] = $col;
33
        } else {
34
            $this->whereArray[] = [$col, $value, $comparision, $concatenation];
35
        }
36
        return $this;
37
    }
38
39
    public function setTable($tableName) {
40
        $this->table = $tableName;
41
    }
42
43
    public function select($tableName = null) {
44
        if (!$tableName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tableName of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
45
            $tableName = $this->table;
46
        }
47
        return new Result($this->connection->getItems($tableName, $this->whereArray, $this->dbOptions), $this);
48
    }
49
50
    public function insert(string $tableName, array $values) {
51
        if (!$tableName) {
52
            $tableName = $this->table;
53
        }
54
        return $this->connection->addItem($tableName, $values, $this->dbOptions);
55
    }
56
57
    public function update(string $tableName, array $values) {
58
        if (!$tableName) {
59
            $tableName = $this->table;
60
        }
61
        return $this->connection->updateItems($tableName, $this->whereArray, $values, $this->dbOptions);
62
    }
63
64
    public function delete(string $tableName='') {
65
        if (!$tableName) {
66
            $tableName = $this->table;
67
        }
68
        return $this->connection->deleteItems($tableName, $this->whereArray, $this->dbOptions);
69
    }
70
71
}