GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 083d1a...28e483 )
by cao
03:15
created

DB::wrap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 58 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace PhpBoot\DB;
3
4
use PhpBoot\Application;
5
use PhpBoot\DB\rules\select\SelectRule;
6
use PhpBoot\DB\rules\insert\InsertRule;
7
use PhpBoot\DB\rules\update\UpdateRule;
8
use PhpBoot\DB\rules\delete\DeleteRule;
9
use PhpBoot\DB\rules\replace\ReplaceIntoRule;
10
11 1
require_once __DIR__.'/rules/select.php';
12 1
require_once __DIR__.'/rules/insert.php';
13 1
require_once __DIR__.'/rules/update.php';
14 1
require_once __DIR__.'/rules/delete.php';
15 1
require_once __DIR__.'/rules/replace.php';
16
17
/**
18
 * 
19
 * How-to-use:
20
 * 
21
 * $db = new DB(...);
22
 * // 1. select
23
 * $res = $db->select('a, b')
24
 *      ->from('table')
25
 *      ->leftJoin('table1')->on('table.id=table1.id')
26
 *      ->where('a=?',1)
27
 *      ->groupBy('b')->having('sum(b)=?', 2)
28
 *      ->orderBy('c', Sql::ORDER_BY_ASC)
29
 *      ->limit(0,1)
30
 *      ->forUpdate()->of('d')
31
 *      ->get();
32
 * 
33
 * // 2. update
34
 * $rows = $db->update('table')
35
 *      ->set('a', 1)
36
 *      ->where('b=?', 2)
37
 *      ->orderBy('c', Sql::ORDER_BY_ASC)
38
 *      ->limit(1)
39
 *      ->exec($db)
40
 *      ->rows
41
 *      
42
 * // 3. insert
43
 * $newId = $db->insertInto('table')
44
 *      ->values(['a'=>1])
45
 *      ->exec($db)
46
 *      ->lastInsertId()
47
 *      
48
 * //4. delete
49
 * $rows = $db->deleteFrom('table')
50
 *      ->where('b=?', 2)
51
 *      ->orderBy('c', Sql::ORDER_BY_ASC)
52
 *      ->limit(1)
53
 *      ->exec($db)
54
 *      ->rows
55
 *      
56
 * @author caoym <[email protected]>
57
 */
58
class DB{
59
60
    /**
61
     * DB constructor.
62
     * @param Application $app
63
     * @param string $dsn @see \PDO
64
     * @param string $username @see \PDO
65
     * @param string $password @see \PDO
66
     * @param array $options @see \PDO
67
     */
68
69
    static public function connect(Application $app,
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
70
                                   $dsn,
71
                                  $username,
72
                                  $password,
73
                                  $options = [])
74
    {
75
        $options += [
76
            \PDO::ATTR_ERRMODE =>\PDO::ERRMODE_EXCEPTION,
77
            \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';",
78
            \PDO::MYSQL_ATTR_FOUND_ROWS => true
79
        ];
80
81
        $connection = new \PDO($dsn, $username, $password, $options);
82
        return new DB($app, $connection);
83
    }
84
85 42
    public function __construct(Application $app, $connection)
86
    {
87 42
        $this->app = $app;
88 42
        $this->connection = $connection;
89 42
    }
90
91
    /**
92
     * select('column0,column1') => "SELECT column0,column1"
93
     *   
94
     * select('column0', 'column1') => "SELECT column0,column1"
95
     * 
96
     * @param string $column0
97
     * @return \PhpBoot\DB\rules\select\FromRule
98
     */
99 20
    function select($column0=null, $_=null){
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100 20
        $obj = new SelectRule(new Context($this->connection));
101 20
        if($column0 == null){
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $column0 of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
102 1
            $args = ['*'];
103 20
        }elseif(is_array($column0)){
104 3
            $args = $column0;
105 3
        }else{
106 17
            $args = func_get_args();
107
        }
108 20
        foreach ($args as &$arg){
109 20
            $arg = DB::wrap($arg);
110 20
            if($arg == '*'){
111 8
                continue;
112
            }
113
114 20
        }
115 20
        return $obj->select(implode(',', $args));
116
    }
117
    /** 
118
     * insertInto('table') => "INSERT INTO table"
119
     * 
120
     * @param string $table
121
     * @return \PhpBoot\DB\rules\insert\ValuesRule
122
     */
123 5
    public function insertInto($table) {
124 5
        $obj = new InsertRule(new Context($this->connection));
125 5
        return $obj->insertInto($table);
126
    }
127
    /**
128
     * update('table') => "UPDATE table"
129
     * @param string $table
130
     * @return \PhpBoot\DB\rules\update\UpdateSetRule
131
     */
132 8
    public function update($table) {
133 8
        $obj = new UpdateRule(new Context($this->connection));
134 8
        return $obj->update($table);
135
    }
136
    
137
    /**
138
     * deleteFrom('table') => "DELETE FROM table"
139
     * @param string $table
140
     * @return \PhpBoot\DB\rules\basic\WhereRule
141
     */
142 7
    public function deleteFrom($table){
143 7
        $obj  =  new DeleteRule(new Context($this->connection));
144 7
        return $obj->deleteFrom($table);
145
    }
146
    /**
147
     * replaceInto('table') => "REPLACE INTO table"
148
     * @param string $table
149
     * @return \PhpBoot\DB\rules\replace\ValuesRule
150
     */
151 2
    public function replaceInto($table){
152 2
        $obj  =  new ReplaceIntoRule(new Context($this->connection));
153 2
        return $obj->replaceInto($table);
154
    }
155
156
    /**
157
     * @return \PDO
158
     */
159
    public function getConnection()
160
    {
161
        return $this->connection;
162
    }
163
    /**
164
	 * Splice sql use raw string(without escaping)
165
     * for example:
166
     * where('time>?', 'now()') => " WHERE time > 'now()' "
167
     * where('time>?', Sql::raw('now()')) => " WHERE time > now() "
168
     * @param string $str
169
     * @return Raw
170
     */
171 9
    static public function raw($str){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
172 9
        return new Raw($str);
173
    }
174 42
    static public function wrap($value)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
175
    {
176 42
        if ($value === '*') {
177 8
            return $value;
178
        }
179 42
        if($value instanceof Raw){
180 3
            return $value->get();
181
        }
182 41
        return '`'.str_replace('`', '``', $value).'`';
183
    }
184
185
    /**
186
     * @return Application
187
     */
188 10
    public function getApp()
189
    {
190 10
        return $this->app;
191
    }
192
    const ORDER_BY_ASC ='ASC';
193
    const ORDER_BY_DESC ='DESC';
194
195
    /**
196
     * @var \PDO
197
     */
198
    private $connection;
199
200
    /**
201
     * @var Application
202
     */
203
    private $app;
204
}
205