Completed
Pull Request — master (#1)
by Rougin
04:25
created

Wildfire   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 10.57 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 87.76%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 13
loc 123
ccs 43
cts 49
cp 0.8776
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A delete() 0 12 3
B find() 0 24 4
B get() 0 25 4
A __call() 13 13 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
namespace Rougin\Wildfire;
4
5
use Rougin\Wildfire\Traits\ObjectTrait;
6
use Rougin\Wildfire\Traits\ResultTrait;
7
use Rougin\Wildfire\Traits\DatabaseTrait;
8
use Rougin\Wildfire\Traits\DescribeTrait;
9
10
/**
11
 * Wildfire
12
 *
13
 * Yet another wrapper for CodeIgniter's Query Builder Class.
14
 *
15
 * @package Wildfire
16
 * @author  Rougin Royce Gutib <[email protected]>
17
 */
18
class Wildfire
19
{
20
    use DatabaseTrait, DescribeTrait, ObjectTrait, ResultTrait;
21
22
    /**
23
     * @param \CI_DB|null        $database
24
     * @param \CI_DB_result|null $query
25
     */
26 24
    public function __construct($database = null, $query = null)
27
    {
28 24
        $this->setDatabase($database);
29
30 24
        $this->describe = $this->getDescribe($this->db);
31 24
        $this->query = $query;
32 24
    }
33
34
    /**
35
     * Deletes the data from the specified table by the given delimiters.
36
     *
37
     * @param  string|object $table
38
     * @param  integer|array $delimiters
39
     * @return mixed
40
     */
41 3
    public function delete($table, $delimiters)
42
    {
43 3
        $tableName = ($table instanceof CodeigniterModel) ? $table->getTableName() : $table;
44
45 3
        if (is_integer($delimiters)) {
46
            $delimiters = [ $this->describe->getPrimaryKey($tableName) => $delimiters ];
0 ignored issues
show
Bug introduced by
It seems like $tableName defined by $table instanceof \Rougi...getTableName() : $table on line 43 can also be of type object; however, Rougin\Describe\Describe::getPrimaryKey() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
        }
48
49
        $this->db->where($delimiters);
50
51
        return $this->db->delete($tableName);
0 ignored issues
show
Bug introduced by
It seems like $tableName defined by $table instanceof \Rougi...getTableName() : $table on line 43 can also be of type object; however, CI_DB_query_builder::delete() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
    }
53
54
    /**
55
     * Finds the row from the specified ID or with the list of delimiters from
56
     * the specified table.
57
     *
58
     * @param  object|string  $table
59
     * @param  array|integer  $delimiters
60
     * @param  boolean        $isForeignKey
61
     * @return object|boolean
62
     */
63 6
    public function find($table, $delimiters = [], $isForeignKey = false)
64
    {
65 6
        list($model, $tableName) = $this->getModel($table, $isForeignKey);
66
67 6
        if (! is_array($delimiters)) {
68 6
            $primaryKey = $this->describe->getPrimaryKey($tableName);
69
70 6
            $delimiters = [ $primaryKey => $delimiters ];
71 6
        }
72
73 6
        $this->db->where($delimiters);
74
75 6
        $query = $this->db->get($tableName);
76
77 6
        if ($query->num_rows() > 0) {
78 3
            if ($table == $model) {
79
                $tableName = $model;
80
            }
81
82 3
            return $this->createObject($tableName, $query->row(), $isForeignKey);
83
        }
84
85 3
        return false;
86
    }
87
88
    /**
89
     * Returns all rows from the specified table.
90
     *
91
     * @param  object|string $table
92
     * @return self
93
     */
94 18
    public function get($table = '')
95
    {
96 18
        list($model, $tableName) = $this->getModel($table);
97
98 18
        if ($this->query == null) {
99 12
            $this->query = $this->db->get($tableName);
100 12
        }
101
102 18
        $this->table = $tableName;
103
104 18
        if ($table == $model) {
105 6
            $this->table = $model;
106 6
        }
107
108
        // Guess the specified table from the query
109 18
        if (empty($table)) {
110 6
            $query = $this->db->last_query();
111
112 6
            preg_match('/\bfrom\b\s*(\w+)/i', $query, $matches);
113
114 6
            $this->table = $matches[1];
115 6
        }
116
117 18
        return $this;
118
    }
119
120
    /**
121
     * Calls methods from this class in underscore case.
122
     *
123
     * @param  string $method
124
     * @param  mixed  $parameters
125
     * @return mixed
126
     */
127 9 View Code Duplication
    public function __call($method, $parameters)
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...
128
    {
129 9
        $method = camelize($method);
130 9
        $result = $this;
131
132 9
        if (method_exists($this, $method)) {
133 9
            $class = [$this, $method];
134
            
135 9
            $result = call_user_func_array($class, $parameters);
136 9
        }
137
138 9
        return $result;
139
    }
140
}
141