Passed
Branch master (e0ef34)
by compolom
03:11
created

Wrapper   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 22
eloc 37
c 2
b 0
f 0
dl 0
loc 97
ccs 46
cts 46
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A bindResult() 0 3 1
A count_fields() 0 3 1
A __construct() 0 5 1
A isFetch() 0 3 1
A result() 0 11 4
A bindParam() 0 3 1
A countParams() 0 3 1
A placeholdersTypes() 0 3 1
A getMeta() 0 9 2
A query() 0 19 5
A types() 0 5 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\Mysqli;
6
7
use mysqli;
8
use mysqli_driver;
9
use mysqli_stmt;
10
11
class Wrapper
12
{
13
    public const FETCHTOARRAY = 0;
14
15
    public const FETCHTOOBJECT = 1;
16
17
    private mysqli $mysqli;
18
19
    private ?mysqli_stmt $stmt = null;
20
21 10
    public function __construct(mysqli $mysqli)
22
    {
23 10
        $this->mysqli = $mysqli;
24 10
        $driver = new mysqli_driver();
25 10
        $driver->report_mode = MYSQLI_REPORT_STRICT;
26 10
    }
27
28 8
    public function query(string $query, ?array $placeholders = null)
29
    {
30 8
        $this->stmt = $this->mysqli->prepare($query);
31
32 8
        if ($placeholders && $this->countParams() === count($placeholders)) {
33 4
            $this->bindParam($placeholders);
34
        }
35
36 8
        $this->stmt->execute();
37
38 8
        if (!$this->isFetch($query)) {
39 4
            return $this->stmt->affected_rows;
40
        }
41
42 4
        if ($placeholders) {
43 2
            $this->bindResult();
44
        }
45
46 4
        return $this;
47
    }
48
49 4
    public function result($mode = self::FETCHTOARRAY): array
50
    {
51 4
        $data = [];
52 4
        $method = $mode ? 'fetch_object' : 'fetch_assoc';
53
54 4
        $res = $this->stmt->get_result();
0 ignored issues
show
Bug introduced by
The method get_result() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        /** @scrutinizer ignore-call */ 
55
        $res = $this->stmt->get_result();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55 4
        while ($row = $res->$method()) {
56 4
            $data[] = $row;
57
        }
58
59 4
        return $this->count_fields() === 1 ? $data[0] : $data;
60
    }
61
62 2
    private function bindResult(): void
63
    {
64 2
        call_user_func_array([$this->stmt, 'bind_result'], $this->getMeta());
65 2
    }
66
67 2
    private function getMeta(): array
68
    {
69 2
        $binds = [];
70 2
        $meta = $this->stmt->result_metadata();
71 2
        while ($field = $meta->fetch_field()) {
72 2
            $binds[] = &$row[$field->name];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $row seems to be never defined.
Loading history...
73
        }
74
75 2
        return $binds;
76
    }
77
78 4
    private function bindParam(array $placeholders): void
79
    {
80 4
        @call_user_func([$this->stmt, 'bind_param'], $this->placeholdersTypes($placeholders), ...$placeholders);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for call_user_func(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

80
        /** @scrutinizer ignore-unhandled */ @call_user_func([$this->stmt, 'bind_param'], $this->placeholdersTypes($placeholders), ...$placeholders);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
81 4
    }
82
83 4
    private function placeholdersTypes(array $placeholders): string
84
    {
85 4
        return implode('', array_map([$this, 'types'], $placeholders));
86
    }
87
88 4
    private function types($value): string
89
    {
90 4
        return is_numeric($value)
91 4
            ? (is_float($value) ? 'd' : 'i') // int or float
92 4
            : (strlen((string)$value) <= 65536 ? 's' : 'b'); // string or blob
93
    }
94
95 4
    private function countParams(): int
96
    {
97 4
        return $this->stmt->param_count;
98
    }
99
100 8
    private function isFetch(string $query): bool
101
    {
102 8
        return false !== stripos($query, 'select');
103
    }
104
105 4
    private function count_fields(): int
106
    {
107 4
        return $this->stmt->field_count;
108
    }
109
}
110