CruditesError::setTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
namespace HexMakina\Crudites\Errors;
3
4
class CruditesError 
5
{
6
    protected string $message;
7
    protected string $state;
8
    protected int $code;
9
10
    protected string $table;
11
    protected array $columns = [];
12
    protected string $constraint;
13
    
14
    public function __construct($message)
15
    {
16
        $this->message = $message;
17
    }
18
19
    public function __toString(){
20
        $ret = $this->message;
21
        
22
        if(!empty($this->table))
23
            $ret .= sprintf(' on table "%s"', $this->table);
24
        if(!empty($this->columns))
25
            $ret .= sprintf(' on column(s) "%s"', implode('", "', $this->columns));
26
27
        return $ret;
28
    }
29
30
    public function import($something_PDO): self
31
    {
32
        $errorInfo = null;
33
34
        if(method_exists($something_PDO, 'errorInfo'))
35
            $errorInfo = $something_PDO->errorInfo();
36
        elseif(property_exists($something_PDO, 'errorInfo'))
37
            $errorInfo = $something_PDO->errorInfo;
38
        else
39
            throw new \InvalidArgumentException('INVALID_PDO_CLASS');
40
41
        // 0: the SQLSTATE associated with the last operation on the database handle
42
        $this->setState($errorInfo[0] ?? null);
0 ignored issues
show
Bug introduced by
It seems like $errorInfo[0] ?? null can also be of type null; however, parameter $state of HexMakina\Crudites\Error...uditesError::setState() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

42
        $this->setState(/** @scrutinizer ignore-type */ $errorInfo[0] ?? null);
Loading history...
43
44
        // 1: driver-specific error code.
45
        $this->setCode($errorInfo[1] ?? null);
0 ignored issues
show
Bug introduced by
It seems like $errorInfo[1] ?? null can also be of type null; however, parameter $code of HexMakina\Crudites\Errors\CruditesError::setCode() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        $this->setCode(/** @scrutinizer ignore-type */ $errorInfo[1] ?? null);
Loading history...
46
47
        // 2: driver-specific error message
48
        $this->setMessage($errorInfo[2] ?? null);
0 ignored issues
show
Bug introduced by
It seems like $errorInfo[2] ?? null can also be of type null; however, parameter $message of HexMakina\Crudites\Error...itesError::setMessage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        $this->setMessage(/** @scrutinizer ignore-type */ $errorInfo[2] ?? null);
Loading history...
49
50
        return $this;
51
    }
52
53
54
    public function setState(string $state): void {
55
        $this->state = $state;
56
    }
57
58
    public function getState(): string {
59
        return $this->state;
60
    }
61
62
    public function setCode(int $code): void {
63
        $this->code = $code;
64
    }
65
66
    public function getCode(): int {
67
        return $this->code;
68
    }
69
    
70
71
    public function setMessage(string $message): void {
72
        $this->message = $message;
73
    }
74
75
    public function getMessage(): string {
76
        return $this->message;
77
    }
78
79
80
    public function setTable(string $table): void {
81
        $this->table = $table;
82
    }
83
84
    public function getTable(): string {
85
        return $this->table;
86
    }
87
88
    public function setColumns(array $columns): void {
89
        $this->columns = $columns;
90
    }
91
92
    public function getColumns(): array {
93
        return $this->columns;
94
    }
95
96
    public function setConstraint(string $constraint): void {
97
        $this->constraint = $constraint;
98
    }
99
100
    public function getConstraint(): string {
101
        return $this->constraint;
102
    }
103
}
104