ChildishException   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A formatMessage() 0 4 1
A getSql() 0 4 1
A getBindings() 0 4 1
1
<?php
2
namespace Childish;
3
4
use Childish\support\Tools;
5
6
/**
7
 * ChildishException
8
 *
9
 * @author    Pu ShaoWei <[email protected]>
10
 * @date      2017/12/7
11
 * @package   Childish
12
 * @version   1.0
13
 */
14
class ChildishException extends \PDOException
15
{
16
    /**
17
     * The SQL for the query.
18
     *
19
     * @var string
20
     */
21
    protected $sql;
22
23
    /**
24
     * The bindings for the query.
25
     *
26
     * @var array
27
     */
28
    protected $bindings;
29
30
    /**
31
     * Create a new query exception instance.
32
     *
33
     * @param  string  $sql
34
     * @param  array  $bindings
35
     * @param  \Exception $previous
36
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    public function __construct($sql, array $bindings, $previous)
39
    {
40
        parent::__construct('', 0, $previous);
41
42
        $this->sql = $sql;
43
        $this->bindings = $bindings;
44
        $this->code = $previous->getCode();
45
        $this->message = $this->formatMessage($sql, $bindings, $previous);
46
47
        if ($previous instanceof \PDOException) {
48
            $this->errorInfo = $previous->errorInfo;
49
        }
50
    }
51
52
    /**
53
     * Format the SQL error message.
54
     *
55
     * @param  string  $sql
56
     * @param  array  $bindings
57
     * @param  \Exception $previous
58
     * @return string
59
     */
60
    protected function formatMessage($sql, $bindings, $previous)
61
    {
62
        return $previous->getMessage().' (SQL: '.Tools::replaceArray('?', $bindings, $sql).')';
63
    }
64
65
    /**
66
     * Get the SQL for the query.
67
     *
68
     * @return string
69
     */
70
    public function getSql()
71
    {
72
        return $this->sql;
73
    }
74
75
    /**
76
     * Get the bindings for the query.
77
     *
78
     * @return array
79
     */
80
    public function getBindings()
81
    {
82
        return $this->bindings;
83
    }
84
}