Mysqli::getColumns()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
rs 9.6666
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Db/Mysqli.php
9
 */
10
 
11
namespace JaegerApp\Db;
12
13
use voku\db\DB as vDb;
14
15
/**
16
 * Jaeger - MySQLi Database Object
17
 *
18
 * Wrapper for the MySQLi database interface
19
 *
20
 * @package Database
21
 * @author Eric Lamb <[email protected]>
22
 */
23
class Mysqli implements DbInterface 
24
{
25
    /**
26
     * The primary table we're working with
27
     * @var string
28
     */
29
    protected $table = null;
30
    
31
    /**
32
     * Any filtering for a WHERE SQL clause
33
     * @var mixed
34
     */
35
    protected $where = false;
36
    
37
    /**
38
     * The database connection credentials
39
     * @var array
40
     */
41
    protected $credentials = array();
42
    
43
    /**
44
     * The database object we're piggybacking on
45
     * @var \voku\db\DB
46
     */
47
    protected $db = null;
48
    
49
    /**
50
     * Changes the databse connection to use a new database
51
     * @param string $db_name
52
     */
53
    public function setDbName($db_name)
54
    {
55
        @mysqli_select_db($this->getDb()->getLink(), $db_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
56
    }
57
    
58
    /**
59
     * (non-PHPdoc)
60
     * @see \JaegerApp\Db\DbInterface::select()
61
     */
62
    public function select($table, $where = '1=1')
63
    {
64
        $this->table = $table;
65
        $this->where = $where;
66
        return $this;
67
    }
68
    
69
    /**
70
     * (non-PHPdoc)
71
     * @see \JaegerApp\Db\DbInterface::get()
72
     */
73
    public function get()
74
    {
75
        $data = $this->getDb()->select($this->table, $this->where);
76
        return $data->fetchAllArray();
77
        
78
    }
79
    
80
    /**
81
     * (non-PHPdoc)
82
     * @see \JaegerApp\Db\DbInterface::query()
83
     */
84
    public function query($sql = '', $return = false)
85
    {
86
        $data = $this->getDb()->query($sql, $return);
87
        if( $data instanceof \voku\db\Result )
88
        {
89
            return $data->fetchAllArray();
90
        }
91
    }
92
    
93
    /**
94
     * (non-PHPdoc)
95
     * @see \JaegerApp\Db\DbInterface::getTableStatus()
96
     */
97
    public function getTableStatus()
98
    {
99
        $tables = $this->query("SHOW TABLE STATUS", true);
100
        return $tables;
101
    }
102
    
103
    /**
104
     * (non-PHPdoc)
105
     * @see \JaegerApp\Db\DbInterface::getCreateTable()
106
     */
107
    public function getCreateTable($table, $if_not_exists = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
108
    {
109
        $sql = sprintf('SHOW CREATE TABLE `%s` ;', $table);
110
        $statement = $this->query($sql, true);
111
        $string = false;
112
        if (! empty($statement['0']['Create Table'])) {
113
            $string = $statement['0']['Create Table'];
114
        }
115
    
116
        if ($if_not_exists) {
117
            $replace = substr($string, 0, 12);
118
            if ($replace == 'CREATE TABLE') {
119
                $string = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS ', $string);
120
            }
121
        }
122
    
123
        return $string;
124
    }    
125
    
126
    /**
127
     * (non-PHPdoc)
128
     * @see \JaegerApp\Db\DbInterface::clear()
129
     */
130
    public function clear()
131
    {
132
        $this->table = null;
133
        $this->where = null;
134
    }
135
    
136
    /**
137
     * (non-PHPdoc)
138
     * @see \JaegerApp\Db\DbInterface::totalRows()
139
     */
140
    public function totalRows($table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
141
    {
142
        $sql = sprintf('SELECT COUNT(*) AS count FROM `%s`', $table);
143
        $statement = $this->query($sql, true);
144
        if ($statement) {
145
            if (isset($statement['0']['count'])) {
146
                return $statement['0']['count'];
147
            }
148
        }
149
        
150
        return '0';
151
    }   
152
    
153
    /**
154
     * (non-PHPdoc)
155
     * @see \JaegerApp\Db\DbInterface::getColumnns()
156
     */
157 View Code Duplication
    public function getColumns($table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
158
    {
159
        $sql = sprintf('SHOW COLUMNS FROM `%s`', $table);
160
        $statement = $this->query($sql, true);
161
        if ($statement) {
162
            return $statement;
163
        }
164
        return array();
165
    }  
166
    
167
    /**
168
     * (non-PHPdoc)
169
     * @see \JaegerApp\Db\DbInterface::escape()
170
     */
171
    public function escape($string)
172
    {
173
        return $this->getDb()->escape($string);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getDb()->escape($string); (integer|string|array|false) is incompatible with the return type declared by the interface JaegerApp\Db\DbInterface::escape of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
174
    }
175
    
176
    /**
177
     * (non-PHPdoc)
178
     * @see \JaegerApp\Db\DbInterface::getAllTables()
179
     */
180
    public function getAllTables()
181
    {
182
        return $this->getDb()->getAllTables();
183
    }    
184
    
185
    /**
186
     * (non-PHPdoc)
187
     * @see \JaegerApp\Db\DbInterface::insert()
188
     */
189
    public function insert($table, array $data = array())
190
    {
191
        return $this->getDb()->insert($table, $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getDb()->insert($table, $data); (voku\db\Result|integer|boolean) is incompatible with the return type declared by the interface JaegerApp\Db\DbInterface::insert of type false|integer|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
192
    }
193
    
194
    /**
195
     * (non-PHPdoc)
196
     * @see \JaegerApp\Db\DbInterface::update()
197
     */
198
    public function update($table, $data, $where)
199
    {
200
        return $this->getDb()->update($table, $data, $where);
201
    }
202
    
203
    /**
204
     * 
205
     * @param array $credentials
206
     * @return \JaegerApp\Db\Mysqli
207
     */
208
    public function setCredentials(array $credentials)
209
    {
210
        $this->credentials = $credentials;
211
        return $this;
212
    }
213
    
214
    /**
215
     * (non-PHPdoc)
216
     * @see \JaegerApp\Db\DbInterface::getDb()
217
     */
218
    public function getDb($force = false)
219
    {
220
        if (is_null($this->db)) {
221
            
222
            $this->db = vDb::getInstance($this->credentials['host'], $this->credentials['user'], $this->credentials['password'], $this->credentials['database']);
223
        }
224
        
225
        return $this->db;
226
    }
227
}