Completed
Pull Request — master (#2)
by Rougin
03:28 queued 01:06
created

DatabaseTrait::getTableName()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 26
Code Lines 14

Duplication

Lines 6
Ratio 23.08 %

Code Coverage

Tests 12
CRAP Score 10.3696

Importance

Changes 0
Metric Value
dl 6
loc 26
ccs 12
cts 18
cp 0.6667
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 14
nc 32
nop 2
crap 10.3696
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use Rougin\SparkPlug\Instance;
6
7
/**
8
 * Database Trait
9
 *
10
 * @package Wildfire
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 *
13
 * @property string $table
14
 */
15
trait DatabaseTrait
16
{
17
    /**
18
     * @var \CI_DB
19
     */
20
    protected $db;
21
22
    /**
23
     * @var \CI_DB_result
24
     */
25
    protected $query;
26
27
    /**
28
     * Parses the table name from Describe class.
29
     *
30
     * @param  string  $table
31
     * @param  boolean $isForeignKey
32
     * @return string
33
     */
34 36
    protected function getTableName($table, $isForeignKey = false)
35
    {
36 36
        $tableName = '';
37
38 36
        if ($table instanceof \CI_Model) {
39 View Code Duplication
            if (method_exists($newModel, 'getTableName')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
40
                $tableName = $newModel->getTableName();
0 ignored issues
show
Bug introduced by
The variable $newModel does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
41
            } elseif (property_exists($newModel, 'table')) {
42
                // NOTE: To be removed in v1.0.0
43
                $tableName = $newModel->table;
44
            }
45
        }
46
47 36
        if (! $isForeignKey && $this->table) {
48 24
            $tableName = $this->table;
49 24
        }
50
51 36
        if (is_string($table)) {
52 36
            $tableName = $table;
53 36
        }
54
55 36
        $tableName = ucfirst(singular($tableName));
56 36
        $array = explode('.', $tableName);
57
58 36
        return isset($array[1]) ? $array[1] : $tableName;
59
    }
60
61
    /**
62
     * Sets the database class.
63
     *
64
     * @param  \CI_DB|null $database
65
     * @return self
66
     */
67 39
    public function setDatabase($database = null)
68
    {
69 39
        $ci = Instance::create();
70
71 39
        $ci->load->helper('inflector');
0 ignored issues
show
Bug introduced by
The property load does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
72
73 39
        if (empty($database)) {
74 3
            $ci->load->database();
75
76 3
            $this->db = $ci->db;
0 ignored issues
show
Bug introduced by
The property db does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
77
78 3
            return $this;
79
        }
80
81 39
        $this->db = $database;
82
83 39
        return $this;
84
    }
85
86
    /**
87
     * Sets the query result.
88
     *
89
     * @param  \CI_DB_result $query
90
     * @return self
91
     */
92 3
    public function setQuery($query)
93
    {
94 3
        $this->query = $query;
95
96 3
        return $this;
97
    }
98
}
99