Completed
Pull Request — master (#2)
by Rougin
02:28
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
/**
6
 * Wildfire
7
 *
8
 * Yet another wrapper for CodeIgniter's Query Builder Class.
9
 *
10
 * @package Wildfire
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class Wildfire extends \CI_Model
14
{
15
    use Traits\DatabaseTrait, Traits\DescribeTrait, Traits\ObjectTrait, Traits\ResultTrait;
16
17
    /**
18
     * @param \CI_DB|null        $database
19
     * @param \CI_DB_result|null $query
20
     */
21 39
    public function __construct($database = null, $query = null)
22
    {
23 39
        $this->setDatabase($database);
24
25 39
        $this->describe = $this->getDescribe($this->db);
26 39
        $this->query    = $query;
27 39
    }
28
29
    /**
30
     * Finds the row from the specified ID or with the list of delimiters from
31
     * the specified table.
32
     *
33
     * @param  string         $table
34
     * @param  array|integer  $delimiters
35
     * @param  boolean        $isForeignKey
36
     * @return object|boolean
37
     */
38 18
    public function find($table, $delimiters = [], $isForeignKey = false)
39
    {
40 18
        list($table) = $this->getModel($table, $isForeignKey);
41
42 18
        if (! is_array($delimiters)) {
43 18
            $primaryKey = $this->describe->getPrimaryKey($table);
44
45 18
            $delimiters = [ $primaryKey => $delimiters ];
46 15
        }
47
48 18
        $this->db->where($delimiters);
49
50 18
        $query = $this->db->get($table);
51
52 18
        if ($query->num_rows() > 0) {
53 12
            return $this->createObject($table, $query->row(), $isForeignKey);
54
        }
55
56 6
        return false;
57
    }
58
59
    /**
60
     * Returns all rows from the specified table.
61
     *
62
     * @param  string|object $table
63
     * @return self
64
     */
65 24
    public function get($table = '')
66
    {
67 24
        list($tableName, $model) = $this->getModel($table);
0 ignored issues
show
Bug introduced by
It seems like $table can also be of type object; however, Rougin\Wildfire\Traits\ObjectTrait::getModel() does only seem to accept string|object<CI_Model>|null, 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...
68
69 24
        if ($this->query == null) {
70 18
            $this->query = $this->db->get($tableName);
71 18
        }
72
73 24
        if ($model == $table) {
74 12
            $this->table = $model;
75 12
        } else {
76 12
            $this->table = $tableName;
77
        }
78
79
        // Guess the specified table from the query
80 24
        if (empty($table)) {
81 6
            $query = $this->db->last_query();
82
83 6
            preg_match('/\bfrom\b\s*(\w+)/i', $query, $matches);
84
85 6
            $this->table = $matches[1];
86 6
        }
87
88 24
        return $this;
89
    }
90
91
    /**
92
     * Calls methods from this class in underscore case.
93
     *
94
     * @param  string $method
95
     * @param  mixed  $parameters
96
     * @return mixed
97
     */
98 9
    public function __call($method, $parameters)
99
    {
100 9
        $method = camelize($method);
101 9
        $result = $this;
102
103 9
        if (method_exists($this, $method)) {
104 9
            $class = [$this, $method];
105
            
106 9
            $result = call_user_func_array($class, $parameters);
107 9
        }
108
109 9
        return $result;
110
    }
111
}
112