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

ExtendedPDO   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B queryWithParam() 0 25 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