Passed
Push — main ( 5e0bcd...9ce40a )
by Peter
02:12
created

SuperExpr::__construct()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 27
rs 9.4222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Generic\Expr;
6
7
use PDO;
8
use QB\Generic\IQueryPart;
9
10
class SuperExpr extends Expr
11
{
12
    protected const PDO_TYPES = [PDO::PARAM_INT, PDO::PARAM_STR, PDO::PARAM_BOOL, PDO::PARAM_NULL];
13
14
    /**
15
     * SuperExpr constructor.
16
     *
17
     * @param string|IQueryPart $sql
18
     * @param array             $params
19
     * @param string            $super
20
     */
21
    public function __construct(string|IQueryPart $sql, array $params = [], string $super = '??')
22
    {
23
        if ($sql instanceof IQueryPart) {
24
            $params = array_merge($params, $sql->getParams());
25
            $sql    = (string)$sql;
26
        }
27
28
        $splitSql = explode($super, $sql);
29
30
        $sql = (string)array_shift($splitSql);
31
32
        $flatParams = [];
33
        foreach ($params as $i => $p) {
34
            if (!$this->isParamArray($p)) {
35
                $flatParams[] = $p;
36
                $sql        .= '?';
37
            } else {
38
                $flatParams = array_merge($flatParams, $p);
39
                $sql        .= str_repeat('?, ', count($p) - 1) . '?';
40
            }
41
42
            if (isset($splitSql[$i])) {
43
                $sql .= $splitSql[$i];
44
            }
45
        }
46
47
        parent::__construct($sql, $flatParams);
48
    }
49
50
    /**
51
     * @param null|bool|int|float|string|array $param
52
     *
53
     * @return bool
54
     */
55
    protected function isParamArray(null|bool|int|float|string|array $param): bool
0 ignored issues
show
Bug introduced by
The type QB\Generic\Expr\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
    {
57
        if (!is_array($param)) {
0 ignored issues
show
introduced by
The condition is_array($param) is always true.
Loading history...
58
            return false;
59
        }
60
61
        if (count($param) != 2) {
62
            return true;
63
        }
64
65
        if (!array_key_exists(1, $param)) {
66
            return true;
67
        }
68
69
        return !in_array($param[1], static::PDO_TYPES, true);
70
    }
71
}
72