Completed
Push — master ( 391062...ff47c5 )
by Rasmus
02:37
created

Statement::getParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace mindplay\sql\framework;
4
5
use UnexpectedValueException;
6
7
/**
8
 * This class represents an as-of-yet unprepared SQL statement.
9
 */
10
class Statement implements Executable
11
{
12
    /**
13
     * @var string
14
     */
15
    private $sql;
16
17
    /**
18
     * @var array map where placeholder name maps to a scalar value, or array of scalar values
19
     */
20
    private $params = [];
21
22
    /**
23
     * @param string $sql SQL statement (with placeholders)
24
     */
25 1
    public function __construct($sql)
26
    {
27 1
        $this->sql = $sql;
28 1
    }
29
30
    /**
31
     * Bind an individual placeholder name to a given value.
32
     *
33
     * Acceptable value types are scalar types (string, int, float, bool, null) and arrays of scalar values.
34
     *
35
     * @param string                     $name  placeholder name
36
     * @param array|string|int|bool|null $value value to bind
37
     *
38
     * @return void
39
     */
40 1
    public function bind($name, $value)
41
    {
42 1
        static $SCALAR_TYPES = [
43
            'integer' => true,
44
            'double'  => true,
45
            'string'  => true,
46
            'boolean' => true,
47
            'NULL'    => true,
48
        ];
49
50 1
        $value_type = gettype($value);
51
52 1
        if ($value_type === 'array') {
53 1
            foreach ($value as $item) {
0 ignored issues
show
Bug introduced by
The expression $value of type array|string|integer|boolean|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
54 1
                $item_type = gettype($item);
55
56 1
                if (! isset($SCALAR_TYPES[$item_type])) {
57 1
                    throw new UnexpectedValueException("unexpected item type in array: {$item_type}");
58
                }
59
            }
60
        } else {
61 1
            if (! isset($SCALAR_TYPES[$value_type])) {
62 1
                throw new UnexpectedValueException("unexpected value type: {$value_type}");
63
            }
64
        }
65
66 1
        $this->params[$name] = $value;
67 1
    }
68
69
    /**
70
     * Applies a set of placeholder name/value pairs and binds them to individual placeholders.
71
     *
72
     * @param array $params placeholder name/value pairs
73
     *
74
     * @return void
75
     */
76 1
    public function apply(array $params)
77
    {
78 1
        foreach ($params as $name => $value) {
79 1
            $this->bind($name, $value);
80
        }
81 1
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 1
    public function getSQL()
87
    {
88 1
        return $this->sql;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    public function getParams()
95
    {
96 1
        return $this->params;
97
    }
98
}
99