Completed
Pull Request — master (#2)
by Rougin
02:28
created

DatabaseTrait::getTableName()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10.3696

Importance

Changes 0
Metric Value
dl 0
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|\CI_Model $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
            if (method_exists($table, 'getTableName')) {
40
                $tableName = $table->getTableName();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CI_Model as the method getTableName() does only exist in the following sub-classes of CI_Model: Rougin\Wildfire\CodeigniterModel, Rougin\Wildfire\Wildfire. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
41
            } elseif (property_exists($table, 'table')) {
42
                // NOTE: To be removed in v1.0.0
43
                $tableName = $table->table;
0 ignored issues
show
Bug introduced by
The property table does not seem to exist in CI_Model.

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...
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