Completed
Push — master ( 14c836...906b18 )
by Björn
06:09 queued 03:52
created

CodeWarning   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 123
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer;
6
7
use Exception;
8
9
/**
10
 * The warning of a code does not conform to the style guide.
11
 *
12
 * We use the exception code as a string like the PDOException does, not as an integer.
13
 *
14
 * @author blange <[email protected]>
15
 * @package BestIt\CodeSniffer
16
 */
17
class CodeWarning extends Exception
18
{
19
    /**
20
     * Is this topic fixable?
21
     *
22
     * @var bool
23
     */
24
    private bool $isFixable = false;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
26
    /**
27
     * The payload for displaying the error message.
28
     *
29
     * @var array
30
     */
31
    private array $payload = [];
32
33
    /**
34
     * The position of the error.
35
     *
36
     * @var int
37
     */
38
    private int $stackPosition;
39
40
    /**
41
     * The erroneous token.
42
     *
43
     * @var array|void
44
     */
45
    private $token;
46
47
    /**
48
     * CodeWarning constructor.
49
     *
50
     * @param string $code The code for the sniffer.
51
     * @param string $message The message for the sniffer.
52
     * @param int $stackPosition Where is the token in the stack?
53
     */
54
    public function __construct(string $code, string $message, int $stackPosition)
55
    {
56
        parent::__construct($message);
57
58
        // The docs allow, that the code is not numeric (like in the PDOException) but the type hint is a number,
59
        // so we just use the property.
60
        $this->code = $code;
61
        $this->stackPosition = $stackPosition;
62
    }
63
64
    /**
65
     * Returns the payload for displaying the error message.
66
     *
67
     * @return array The possible payload for the error message.
68
     */
69
    public function getPayload(): array
70
    {
71
        return $this->payload;
72
    }
73
74
    /**
75
     * Returns the position of the error.
76
     *
77
     * @return int Where is the token in the stack?
78
     */
79
    public function getStackPosition(): int
80
    {
81
        return $this->stackPosition;
82
    }
83
84
    /**
85
     * Returns the erroneous token.
86
     *
87
     * @return array The "broken" token.
88
     */
89
    public function getToken(): array
90
    {
91
        return $this->token;
92
    }
93
94
    /**
95
     * Is this topic fixable?
96
     *
97
     * @param bool|null $newStatus If given the new status.
98
     *
99
     * @return bool Returns the "old" status if this is fixable.
100
     */
101
    public function isFixable(?bool $newStatus = null): bool
102
    {
103
        $oldStatus = $this->isFixable;
104
105
        if ($newStatus !== null) {
106
            $this->isFixable = $newStatus;
107
        }
108
109
        return $oldStatus;
110
    }
111
112
    /**
113
     * Sets the payload for displaying the error message.
114
     *
115
     * @param array $payload The possible payload for the error message.
116
     *
117
     * @return $this Fluent-Interface.
118
     */
119
    public function setPayload(array $payload): self
120
    {
121
        $this->payload = $payload;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Sets the erroneous token.
128
     *
129
     * @param array $token The "broken" token.
130
     *
131
     * @return $this Fluent Interface.
132
     */
133
    public function setToken(array $token): self
134
    {
135
        $this->token = $token;
136
137
        return $this;
138
    }
139
}
140