Passed
Push — master ( a1b2ba...44576f )
by Sebastian
02:32
created

ExtendedPDO::queryWithParam()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
crap 4
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Storage;
13
14
use PDO;
15
use PDOStatement;
16
use InvalidArgumentException;
17
18
/**
19
 * Extended PDO
20
 */
21
class ExtendedPDO extends PDO
22
{
23
    /**
24
     * Executes an SQL statement with parameters,
25
     * returning a result set as a PDOStatement object
26
     *
27
     * @param string $query SQL statement
28
     * @param array $param Parameter as array as PDOStatement::bindParam
29
     *
30
     * @return PDOStatement
31
     */
32 6
    public function queryWithParam(string $query, array $param) : PDOStatement
33
    {
34 6
        $statment = $this->prepare($query);
35
               
36 6
        foreach ($param as $value) {
37 5
            if (count($value) < 2) {
38 1
                throw new InvalidArgumentException(__METHOD__.': Parameters array must contain at least two elements with this form: [\':name\', \'value\']');
39
            }
40
41 4
            if (strpos($value[0], ':') !== 0) {
42 1
                throw new InvalidArgumentException(__METHOD__.': Parameter name will be in the form :name');
43
            }
44
45
            //reassign as reference
46
            //because bindParam need it as reference
47 3
            $ref = $value;
48 3
            $ref[1] = &$value[1];
49
50 3
            call_user_func_array([$statment, "bindParam"], $ref);
51
        }
52
53 4
        $statment->execute();
54
55 3
        return $statment;
56
    }
57
}
58