Completed
Push — master ( 64123e...952eff )
by Rougin
01:47
created

Wildfire::setQueryAndTable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
use Rougin\Wildfire\Helpers\TableHelper;
6
7
/**
8
 * Wildfire
9
 *
10
 * Yet another wrapper for CodeIgniter's Query Builder Class.
11
 *
12
 * @package Wildfire
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class Wildfire extends \CI_Model
16
{
17
    use Traits\DatabaseTrait, Traits\ResultTrait;
18
19
    /**
20
     * Initializes the Wildfire instance.
21
     *
22
     * @param \CI_DB_query_builder|null $database
23
     * @param \CI_DB_result|null        $query
24
     */
25 39
    public function __construct($database = null, $query = null)
26
    {
27 39
        empty($database) || $this->database($database);
28
29 39
        $this->query = $query;
30 39
    }
31
32
    /**
33
     * Finds the row from the specified ID or with the
34
     * list of delimiters from the specified table.
35
     *
36
     * @param  string         $table
37
     * @param  array|integer  $delimiters
38
     * @return object|boolean
39
     */
40 24
    public function find($table, $delimiters = [])
41
    {
42 24
        if (is_integer($delimiters) === true) {
43 15
            $primary = $this->describe->primary($table);
44
45 15
            $delimiters = array($primary => $delimiters);
46 10
        }
47
48 24
        $this->db->where($delimiters);
49
50 24
        $query = $this->db->get($table);
51
52 24
        if ($query->num_rows() > 0 && $query->row()) {
53 18
            return $this->make($table, $query->row());
0 ignored issues
show
Documentation introduced by
$table is of type string, but the function expects a object<Rougin\Wildfire\CodeigniterModel\string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
        }
55
56 6
        return false;
57
    }
58
59
    /**
60
     * Returns all rows from the specified table.
61
     *
62
     * @param  mixed $table
63
     * @return self
64
     */
65 27
    public function get($table = '')
66
    {
67
        // Guess the specified table from the query
68 27
        if (empty($table) === true) {
69 6
            $pattern = '/\bfrom\b\s*(\w+)/i';
70
71 6
            $query = $this->db->last_query();
72
73 6
            preg_match($pattern, $query, $matches);
74
75 6
            $this->table = (string) $matches[1];
76
77 6
            return $this;
78
        }
79
80 21
        return $this->prepare($table);
81
    }
82
83
    /**
84
     * Returns the model class of the said table.
85
     *
86
     * @param  string|object $table
87
     * @return array
88
     */
89 39
    protected function model($table)
90
    {
91 39
        if (is_string($table) === true) {
92 36
            $model = TableHelper::model($table);
93
94 36
            $model = new $model;
95
96 36
            $name = TableHelper::name($model);
97
98 36
            return array((string) $name, $model);
99
        }
100
101 9
        $name = TableHelper::name($table);
102
103 9
        return array((string) $name, $table);
104
    }
105
106
    /**
107
     * Prepares the query and table properties.
108
     *
109
     * @param  string|object $table
110
     * @return self
111
     */
112 21
    protected function prepare($table)
113
    {
114 21
        list($name, $model) = (array) $this->model($table);
115
116 21
        $this->query || $this->query = $this->db->get($name);
117
118 21
        $this->table = $model === $table ? $model : $name;
119
120 21
        return $this;
121
    }
122
123
    /**
124
     * Calls methods from this class in underscore case.
125
     * NOTE: To be removed in v1.0.0. Methods are now only one words.
126
     *
127
     * @param  string $method
128
     * @param  mixed  $parameters
129
     * @return mixed
130
     */
131 9
    public function __call($method, $parameters)
132
    {
133 9
        $method = (string) camelize($method);
134
135 9
        $result = $this;
136
137 9
        if (method_exists($this, $method) === true) {
138 9
            $instance = array($this, (string) $method);
139
140 9
            $result = call_user_func_array($instance, $parameters);
141 6
        }
142
143 9
        return $result;
144
    }
145
}
146