Completed
Push — master ( 8004d0...4e5693 )
by Rougin
02:29
created

Wildfire::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
use Rougin\Wildfire\Helpers\ModelHelper;
6
use Rougin\Wildfire\Helpers\DescribeHelper;
7
8
/**
9
 * Wildfire
10
 *
11
 * Yet another wrapper for CodeIgniter's Query Builder Class.
12
 *
13
 * @package Wildfire
14
 * @author  Rougin Royce Gutib <[email protected]>
15
 */
16
class Wildfire extends \CI_Model
17
{
18
    use Traits\DatabaseTrait, Traits\ObjectTrait, Traits\ResultTrait;
19
20
    /**
21
     * @var \Rougin\Describe\Describe
22
     */
23
    protected $describe;
24
25
    /**
26
     * @param \CI_DB|null        $database
27
     * @param \CI_DB_result|null $query
28
     */
29 39
    public function __construct($database = null, $query = null)
30
    {
31 39
        $this->setDatabase($database);
32
33 39
        $this->describe = DescribeHelper::createInstance($this->db);
34 39
        $this->query    = $query;
35 39
    }
36
37
    /**
38
     * Finds the row from the specified ID or with the list of delimiters from
39
     * the specified table.
40
     *
41
     * @param  string         $tableName
42
     * @param  array|integer  $delimiters
43
     * @return object|boolean
44
     */
45 18
    public function find($tableName, $delimiters = [])
46
    {
47 18
        list($tableName, $model) = ModelHelper::createInstance($tableName);
0 ignored issues
show
Unused Code introduced by
The assignment to $model is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
48
49 18
        if (is_integer($delimiters)) {
50 12
            $primaryKey = $this->describe->getPrimaryKey($tableName);
51
52 12
            $delimiters = [ $primaryKey => $delimiters ];
53 12
        }
54
55 18
        $this->db->where($delimiters);
56
57 18
        $query = $this->db->get($tableName);
58
59 18
        if ($query->num_rows() > 0) {
60 12
            return $this->createObject($tableName, $query->row());
61
        }
62
63 6
        return false;
64
    }
65
66
    /**
67
     * Returns all rows from the specified table.
68
     *
69
     * @param  mixed $table
70
     * @return self
71
     */
72 24
    public function get($table = '')
73
    {
74
        // Guess the specified table from the query
75 24
        if (empty($table)) {
76 6
            $query = $this->db->last_query();
77
78 6
            preg_match('/\bfrom\b\s*(\w+)/i', $query, $matches);
79
80 6
            $this->table = $table = $matches[1];
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
82 6
            return $this;
83
        }
84
85 18
        list($tableName, $model) = ModelHelper::createInstance($table);
86
87 18
        $this->table = $tableName;
88
89 18
        if ($this->query == null) {
90 18
            $this->query = $this->db->get($tableName);
91 18
        }
92
93 18
        if ($model == $table) {
94 6
            $this->table = $model;
95 6
        }
96
97 18
        return $this;
98
    }
99
100
    /**
101
     * Calls methods from this class in underscore case.
102
     *
103
     * @param  string $method
104
     * @param  mixed  $parameters
105
     * @return mixed
106
     */
107 9
    public function __call($method, $parameters)
108
    {
109 9
        $method = camelize($method);
110 9
        $result = $this;
111
112 9
        if (method_exists($this, $method)) {
113 9
            $class = [$this, $method];
114
            
115 9
            $result = call_user_func_array($class, $parameters);
116 9
        }
117
118 9
        return $result;
119
    }
120
}
121