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

ExecResult::lastInsertId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBoot\DB\impls;
4
use PhpBoot\DB\DB;
5
use PhpBoot\DB\NestedStringCut;
6
use PhpBoot\DB\Raw;
7
use PhpBoot\DB\rules\basic\BasicRule;
8
use PhpBoot\DB\Context;
9
10
class ExecResult{
11 22
    public function __construct($success, $pdo, $st){
12 22
        $this->pdo = $pdo;
13 22
        $this->st = $st;
14 22
        $this->success = $success;
15 22
        $this->rows = $this->st->rowCount();
16 22
    }
17 1
    public function lastInsertId($name=null){
18 1
        return $this->pdo->lastInsertId($name);
19
    }
20
    /**
21
     * @var bool
22
     * true on success or false on failure.
23
     */
24
    public $success;
25
    /**
26
     * @var int
27
     * the number of rows.
28
     */
29
    public $rows;
30
    /**
31
     *
32
     * @var \PDO
33
     */
34
    public $pdo;
35
36
    /**
37
     * @var \PDOStatement
38
     */
39
    public $st;
40
}
41
42
class SelectImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
43
{
44 20
    static  public function select($context, $columns){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
45 20
        $context->appendSql("SELECT $columns");
46 20
    }
47
}
48
49
class FromImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
50
{
51 19
    static public function from($context, $tables,$as=null){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
52 19
        if($tables instanceof BasicRule){
53
            $context->appendSql("FROM (".$tables->context->sql.')');
54
            $context->params = array_merge($context->params,$tables->context->params);
55
        }else {
56 19
            $context->appendSql("FROM ".DB::wrap($tables));
57
        }
58 19
        if($as){
59
            $context->appendSql("as ".DB::wrap($as));
60
        }
61 19
    }
62
}
63
64
class DeleteImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
65
{
66 7
    static public function deleteFrom($context, $from)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
67
    {
68 7
        $context->appendSql("DELETE FROM ".DB::wrap($from));
69 7
    }
70
}
71
72
class JoinImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
73
{
74 5
    static public function join($context, $type, $table) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
75 5
        $table = DB::wrap($table);
76 5
        if($type){
77 3
            $context->appendSql("$type JOIN $table");
78 3
        }else{
79 2
            $context->appendSql("JOIN $table");
80
        }
81 5
    }
82
}
83
84
class JoinOnImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
85
{
86 5
    static public function on($context, $condition) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
87 5
        $context->appendSql("ON $condition");
88 5
    }
89
}
90
91
class ForUpdateImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
92
{
93 2
    static public function forUpdate($context){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
94 2
        $context->appendSql("FOR UPDATE");
95 2
    }
96
}
97
98
class ForUpdateOfImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
99
{
100 1
    static public function of($context, $column){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
101 1
        $column = DB::wrap($column);
102 1
        $context->appendSql("OF $column");
103 1
    }
104
}
105
106
class InsertImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
107
{
108 5
    static public function insertInto($context, $table) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
109 5
        $table = DB::wrap($table);
110 5
        $context->appendSql("INSERT INTO $table");
111 5
    }
112
}
113
class ReplaceImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
114
{
115 2
    static public function replaceInto($context, $table) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
116 2
        $table = DB::wrap($table);
117 2
        $context->appendSql("REPLACE INTO $table");
118 2
    }
119
}
120
class ValuesImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
121
{
122 7
    static public function values($context, $values){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
123 7
        $params = [];
124 7
        $stubs = [];
125 7
        foreach ($values as $v){
126 7
            if(is_a($v, Raw::class)){//直接拼接sql,不需要转义
127 4
                $stubs[]=$v->get();
128 4
            }else{
129 7
                $stubs[]='?';
130 7
                $params[] = $v;
131
            }
132 7
        }
133 7
        $stubs = implode(',', $stubs);
134
135 7
        if(array_keys($values) === range(0, count($values) - 1)){
136
            //VALUES(val0, val1, val2)
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137 4
            $context->appendSql("VALUES($stubs)");
138
139 4
        }else{
140
            //(col0, col1, col2) VALUES(val0, val1, val2)
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
141
            $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values)));
142 3
            $context->appendSql("($columns) VALUES($stubs)",false);
143
        }
144 7
        $context->appendParams($params);
145 7
    }
146
    private $sql = null;
0 ignored issues
show
Unused Code introduced by
The property $sql is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
147
}
148
149
class UpdateImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
150
{
151 8
    static public function update($context, $table){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
152 8
        $table = DB::wrap($table);
153 8
        $context->appendSql("UPDATE $table");
154 8
    }
155
}
156
157
class UpdateSetImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
158
{
159 8
    public function set(Context $context, $expr, $args){
160 8
        if(is_string($expr)){
161
            return $this->setExpr($context, $expr, $args);
162
        }else{
163 8
            return $this->setArgs($context, $expr);
164
        }
165
    }
166
167
    public function setExpr(Context $context, $expr, $args){
168
        if($this->first){
169
            $this->first = false;
170
            $prefix = 'SET ';
171
        }else{
172
            $prefix = ',';
173
        }
174
175
        $context->appendSql("$prefix$expr",$prefix == 'SET ');
176
        $context->appendParams($args);
177
178
    }
179 8 View Code Duplication
    public function setArgs(Context $context, $values){
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...
180 8
        $set = [];
181 8
        $params = [];
182 8
        foreach ($values as $k=>$v){
183 8
            $k = DB::wrap($k);
184 8
            if(is_a($v, Raw::class)){//直接拼接sql,不需要转义
185 1
                $set[]= "$k=".$v->get();
186 1
            }else{
187 8
                $set[]= "$k=?";
188 8
                $params[]=$v;
189
            }
190 8
        }
191 8
        if($this->first){
192 8
            $this->first = false;
193 8
            $context->appendSql('SET '.implode(',', $set));
194 8
            $context->appendParams($params);
195 8
        }else{
196 1
            $context->appendSql(','.implode(',', $set),false);
197 1
            $context->appendParams($params);
198
        }
199 8
    }
200
    private $first=true;
201
}
202
class OrderByImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
203
{
204 7
    public function orderByArgs(Context $context, $orders){
205 7
        if(empty($orders)){
206
            return $this;
207
        }
208 7
        $params = array();
209 7
        foreach ($orders as $k=>$v){
210 7
            if(is_integer($k)){
211 6
                $params[] = DB::wrap($v);
212 6
            }else{
213 2
                $k = DB::wrap($k);
214
215 2
                $v = strtoupper($v);
216 2
                ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")"));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
217
218 2
                $params[] = "$k $v";
219
            }
220 7
        }
221 7
        if($this->first){
222 7
            $this->first = false;
223 7
            $context->appendSql('ORDER BY '.implode(',', $params));
224 7
        }else{
225 1
            $context->appendSql(','.implode(',', $params),false);
226
        }
227 7
        return $this;
228
    }
229 7
    public function orderBy(Context $context, $column, $order=null){
230 7
        if(is_string($column)){
231 7
            if($order === null){
232 6
                $column = [$column];
233 6
            }else{
234 2
                $column = [$column=>$order];
235
            }
236 7
        }
237 7
        return $this->orderByArgs($context, $column);
238
239
240
    }
241
    private $first=true;
242
}
243
244
class LimitImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
245
{
246 3
    static public function limit(Context $context, $size){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
247 3
        $intSize = intval($size);
248 3
        strval($intSize) == $size or \PhpBoot\abort(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
249
            new \InvalidArgumentException("invalid params for limit($size)"));
250 3
        $context->appendSql("LIMIT $size");
251 3
    }
252 1
    static public function limitWithOffset(Context $context, $start, $size){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
253 1
        $intStart = intval($start);
254 1
        $intSize = intval($size);
255 1
        strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
256
            new \InvalidArgumentException("invalid params for limit($start, $size)"));
257 1
        $context->appendSql("LIMIT $start,$size");
258 1
    }
259
}
260
261
class WhereImpl{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
262
263 1
    static private function  findQ($str,$offset = 0,$no=0){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
264 1
        $found = strpos($str, '?', $offset);
265 1
        if($no == 0 || $found === false){
266 1
            return $found;
267
        }
268 1
        return self::findQ($str, $found+1, $no-1);
269
    }
270 2 View Code Duplication
    static public function having(Context $context, $expr, $args){
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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
271 2
        if(is_string($expr)){
272 2
            self::condition($context, 'HAVING', $expr, $args);
273 2
        }else{
274
            self::conditionArgs($context, 'HAVING', $expr);
275
        }
276
        //TODO 支持 OR 、 闭包
277
278 2
    }
279 19 View Code Duplication
    static public function where(Context $context, $expr, $args){
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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
280 19
        if(empty($expr)){
281 1
            return;
282
        }
283 18
        if (is_string($expr)){
284 13
            self::condition($context, 'WHERE', $expr, $args);
285 13
        }else{
286 6
            self::conditionArgs($context, 'WHERE', $expr);
287
        }
288
        //TODO 支持 OR 、 闭包
289 18
    }
290
291
    /**
292
     * find like Mongodb query glossary
293
     * whereArray(
294
     *      [
295
     *          'id'=>['>'=>1],
296
     *          'name'=>'cym',
297
     *      ]
298
     * )
299
     * 支持的操作符有
300
     * =    'id'=>['=' => 1]
301
     * >    'id'=>['>' => 1]
302
     * <    'id'=>['<' => 1]
303
     * <>   'id'=>['<>' => 1]
304
     * >=   'id'=>['>=' => 1]
305
     * <=   'id'=>['<=' => 1]
306
     * BETWEEN  'id'=>['BETWEEN' => [1 ,2]]
307
     * LIKE     'id'=>['LIKE' => '1%']
308
     * IN   'id'=>['IN' => [1,2,3]]
309
     * NOT IN   'id'=>['NOT IN' => [1,2,3]]
310
     * @return void
311
     */
312 6
    static public function conditionArgs(Context $context, $prefix, $args=[]){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
313 6
        if($args ===null){
314
            return ;
315
        }
316 6
        $exprs = array();
317 6
        $params = array();
318 6
        foreach ($args as $k => $v){
319 6
            $k = DB::wrap($k);
320 6
            if(is_array($v)){
321 1
                $ops = ['=', '>', '<', '<>', '>=', '<=', 'IN', 'NOT IN', 'BETWEEN', 'LIKE'];
322 1
                $op = array_keys($v)[0];
323 1
                $op = strtoupper($op);
324
325 1
                false !== array_search($op, $ops) or \PhpBoot\abort(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
326
                    new \InvalidArgumentException("invalid param $op for whereArgs"));
327
328 1
                $var = array_values($v)[0];
329 1
                if($op == 'IN' || $op == 'NOT IN'){
330 1
                    $stubs = [];
331 1 View Code Duplication
                    foreach ($var as $i){
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...
332 1
                        if(is_a($i, Raw::class)){
333 1
                            $stubs[]=strval($i);
334 1
                        }else{
335 1
                            $stubs[]='?';
336 1
                            $params[] = $i;
337
                        }
338 1
                    }
339 1
                    $stubs = implode(',', $stubs);
340 1
                    $exprs[] = "$k $op ($stubs)";
341 1
                }else if($op == 'BETWEEN'){
342 1
                    $cond = "$k BETWEEN";
343 1 View Code Duplication
                    if(is_a($var[0], Raw::class)){
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...
344
                        $cond = "$cond ".strval($var[0]);
345
                    }else{
346 1
                        $cond = "$cond ?";
347 1
                        $params[] = $var[0];
348
                    }
349 1 View Code Duplication
                    if(is_a($var[1], Raw::class)){
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...
350 1
                        $cond = "$cond AND ".strval($var[1]);
351 1
                    }else{
352
                        $cond = "$cond AND ?";
353
                        $params[] = $var[1];
354
                    }
355 1
                    $exprs[] = $cond;
356 1
                }else{
357 1
                    if(is_a($var, Raw::class)){
358
                        $exprs[] = "$k $op ".strval($var);
359
                    }else{
360 1
                        $exprs[] = "$k $op ?";
361 1
                        $params[] = $var;
362
                    }
363
                }
364 1
            }else{
365 6
                if(is_a($v, Raw::class)){
366 1
                    $exprs[] = "$k = ".strval($v);
367
368 1
                }else{
369 6
                    $exprs[] = "$k = ?";
370 6
                    $params[] = $v;
371
                }
372
            }
373 6
        }
374
375 6
        self::condition($context, $prefix, implode(' AND ', $exprs), $params);
376 6
    }
377 19
    static public function condition(Context $context, $prefix, $expr, $args){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
378 19
        if(!empty($expr)){
379 19
            if($args){
380
                //因为PDO不支持绑定数组变量, 这里需要手动展开数组
381
                //也就是说把 where("id IN(?)", [1,2])  展开成 where("id IN(?,?)", 1,2)
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
382 18
                $cutted = null;
383 18
                $cut = null;
384 18
                $toReplace = array();
385
386 18
                $newArgs=array();
387
                //找到所有数组对应的?符位置
388 18
                foreach ($args as $k =>$arg){
389 18
                    if(is_array($arg) || is_a($arg, Raw::class)){
390 1
                        if(!$cutted){
0 ignored issues
show
Bug Best Practice introduced by
The expression $cutted of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
391 1
                            $cut = new NestedStringCut($expr);
392 1
                            $cutted = $cut->getText();
393 1
                        }
394
                        //找到第$k个?符
395 1
                        $pos = self::findQ($cutted, 0, $k);
396 1
                        $pos = $cut->mapPos($pos);
397 1
                        $pos !== false or \PhpBoot\abort(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
398
                            new \InvalidArgumentException("unmatched params and ? @ $expr"));
399
400 1
                        if(is_array($arg)){
401 1
                            $stubs = [];
402 1 View Code Duplication
                            foreach ($arg as $i){
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...
403 1
                                if(is_a($i, Raw::class)){
404 1
                                    $stubs[] = strval($i);
405 1
                                }else{
406 1
                                    $stubs[] = '?';
407 1
                                    $newArgs[] = $i;
408
                                }
409 1
                            }
410 1
                            $stubs = implode(',', $stubs);
411 1
                        }else{
412 1
                            $stubs = strval($arg);
413
                        }
414 1
                        $toReplace[] = [$pos, $stubs];
415
416 1
                    }else{
417 18
                        $newArgs[]=$arg;
418
                    }
419 18
                }
420
421 18
                if(count($toReplace)){
422 1
                    $toReplace = array_reverse($toReplace);
423 1
                    foreach ($toReplace as $i){
424 1
                        list($pos, $v) = $i;
425 1
                        $expr = substr($expr, 0, $pos).$v. substr($expr, $pos+1);
426 1
                    }
427 1
                    $args = $newArgs;
428 1
                }
429 18
            }
430 19
            $context->appendSql($prefix.' '.$expr);
431 19
            if($args){
432 18
                $context->appendParams($args);
433 18
            }
434 19
        }
435 19
    }
436
}
437
438
class GroupByImpl{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
439 3
    static public function groupBy(Context $context, $column){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
440 3
        $column = DB::wrap($column);
441 3
        $context->appendSql("GROUP BY $column");
442 3
    }
443
}
444
445
class ExecImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
446
{
447
    /**
448
     *
449
     * @param Context $context
450
     * @param $exceOnError boolean whether throw exceptions
451
     * @return ExecResult
452
     */
453 22
    static public function exec($context) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
454 22
        $st = $context->connection->prepare($context->sql);
455 22
        $success = $st->execute($context->params);
456 22
        return new ExecResult($success, $context->connection, $st);
457
    }
458
    /**
459
     *
460
     * @param Context $context
461
     * @param string|false $asDict return  as dict or array
0 ignored issues
show
Bug introduced by
There is no parameter named $asDict. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
462
     * @return false|array
463
     */
464 20
    static public function get($context, $dictAs=false){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
465
466 20
        $st = $context->connection->prepare($context->sql);
467 20
        if($st->execute($context->params)){
468
            $res = $st->fetchAll(\PDO::FETCH_ASSOC);
469
            if ($dictAs){
470
                $dict= [];
471
                foreach ($res as $i){
472
                    $dict[$i[$dictAs]]=$i;
473
                }
474
                return $context->handleResult($dict);
475
            }
476
            return $context->handleResult($res);
477
        }else{
478 20
            return false;
479
        }
480
    }
481
482
    /**
483
     * @param Context $context
484
     * @return int|false
485
     */
486
    static public function count($context){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
487
488
        $found = [];
489
        if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) ||
490
            count($found)==0){
491
            \PhpBoot\abort(new \PDOException("can not use count(*) without select"));
492
        }
493
        list($chars, $columnBegin) = $found[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $chars is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
494
        $columnBegin = $columnBegin + strlen('select')+1;
495
496
        $columnEnd = 0;
497
        $found = [];
498
        if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) ||
499
            count($found)==0){
500
            $columnEnd = strlen($context->sql);
501
        }else{
502
            list($chars, $columnEnd) = $found[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $chars is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
503
        }
504
        $sql = substr($context->sql, 0, $columnBegin);
505
        $sql .= ' COUNT(*) as `count` ';
506
        $sql .= substr($context->sql, $columnEnd);
507
508
        $st = $context->connection->prepare($sql);
509
        if($st->execute($context->params)){
510
            $res = $st->fetchAll(\PDO::FETCH_ASSOC);
511
            return $res[0]['count'];
512
        }else{
513
            return false;
514
        }
515
516
    }
517
}
518
class OnDuplicateKeyUpdateImpl
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
519
{
520 1
    public function set($context, $column, $value){
521 1
        if(is_string($column)){
522 1
            $this->setExpr($context, $column, $value);
523 1
        }else{
524 1
            $this->setArgs($context, $column);
525
        }
526 1
    }
527
528 1
    public function setExpr($context, $expr, $args){
529 1
        $prefix = '';
0 ignored issues
show
Unused Code introduced by
$prefix is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
530 1
        if($this->first){
531 1
            $this->first = false;
532 1
            $prefix = 'ON DUPLICATE KEY UPDATE ';
533 1
        }else{
534
            $prefix = ',';
535
        }
536
537 1
        $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE ');
538 1
        $context->appendParams($args);
539
540 1
    }
541 1 View Code Duplication
    public function setArgs($context, $values){
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...
542 1
        $set = [];
543 1
        $params = [];
544 1
        foreach ($values as $k=>$v){
545 1
            $k = DB::wrap($k);
546 1
            if(is_a($v, Raw::class)){//直接拼接sql,不需要转义
547 1
                $set[]= "$k=".$v->get();
548 1
            }else{
549
                $set[]= "$k=?";
550
                $params[]=$v;
551
            }
552 1
        }
553 1
        if($this->first){
554 1
            $this->first = false;
555 1
            $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set));
556 1
            $context->appendParams($params);
557 1
        }else{
558
            $context->appendSql(','.implode(',', $set),false);
559
            $context->appendParams($params);
560
        }
561 1
    }
562
    private $first=true;
563
}
564