|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs\Formatting; |
|
6
|
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\CodeError; |
|
8
|
|
|
use BestIt\CodeSniffer\CodeWarning; |
|
9
|
|
|
use BestIt\Sniffs\AbstractSniff; |
|
10
|
|
|
use const T_STRING_CONCAT; |
|
11
|
|
|
use const T_WHITESPACE; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Checks for the space around concat dots and fixes them if they are missing. |
|
15
|
|
|
* |
|
16
|
|
|
* @author blange <[email protected]> |
|
17
|
|
|
* @package BestIt\Sniffs\Formatting |
|
18
|
|
|
*/ |
|
19
|
|
|
class SpaceAroundConcatSniff extends AbstractSniff |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* You MUST wrap your concat-dot with a whitespace char. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public const CODE_MISSING_SPACE_AROUND_CONCAT = 'MissingSpaceAroundConcat'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The message to the user for this error. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private const MESSAGE_MISSING_SPACE_AROUND_CONCAT = 'Please wrap your concatinations with a whitespace char.'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Is the next token whitespace? |
|
37
|
|
|
* |
|
38
|
|
|
* @var bool|void |
|
39
|
|
|
*/ |
|
40
|
|
|
private $nextIsWhitespace; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Is the prev token whitespace? |
|
44
|
|
|
* |
|
45
|
|
|
* @var void|bool |
|
46
|
|
|
*/ |
|
47
|
|
|
private $prevIsWhitespace; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Adds whitespace around the concats. |
|
51
|
|
|
* |
|
52
|
|
|
* @param CodeWarning $warning |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function fixDefaultProblem(CodeWarning $warning): void |
|
57
|
|
|
{ |
|
58
|
|
|
$newContent = ''; |
|
59
|
|
|
|
|
60
|
|
|
if (!$this->prevIsWhitespace) { |
|
|
|
|
|
|
61
|
|
|
$newContent = ' '; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$newContent .= '.'; |
|
65
|
|
|
|
|
66
|
|
|
if (!$this->nextIsWhitespace) { |
|
|
|
|
|
|
67
|
|
|
$newContent .= ' '; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$fixer = $this->getFile()->fixer; |
|
71
|
|
|
|
|
72
|
|
|
$fixer->beginChangeset(); |
|
73
|
|
|
$fixer->replaceToken($this->stackPos, $newContent); |
|
74
|
|
|
$fixer->endChangeset(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Processes the token. |
|
79
|
|
|
* |
|
80
|
|
|
* @throws CodeError |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
* |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function processToken(): void |
|
86
|
|
|
{ |
|
87
|
|
|
if (!(($this->nextIsWhitespace) && ($this->prevIsWhitespace))) { |
|
|
|
|
|
|
88
|
|
|
$error = new CodeError( |
|
89
|
|
|
static::CODE_MISSING_SPACE_AROUND_CONCAT, |
|
90
|
|
|
self::MESSAGE_MISSING_SPACE_AROUND_CONCAT, |
|
91
|
|
|
$this->stackPos |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$error->isFixable(true); |
|
95
|
|
|
|
|
96
|
|
|
throw $error; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Registers the tokens that this sniff wants to listen for. |
|
102
|
|
|
* |
|
103
|
|
|
* An example return value for a sniff that wants to listen for whitespace |
|
104
|
|
|
* and any comments would be: |
|
105
|
|
|
* |
|
106
|
|
|
* <code> |
|
107
|
|
|
* return array( |
|
108
|
|
|
* T_WHITESPACE, |
|
109
|
|
|
* T_DOC_COMMENT, |
|
110
|
|
|
* T_COMMENT, |
|
111
|
|
|
* ); |
|
112
|
|
|
* </code> |
|
113
|
|
|
* |
|
114
|
|
|
* @return int[] |
|
115
|
|
|
*/ |
|
116
|
|
|
public function register(): array |
|
117
|
|
|
{ |
|
118
|
|
|
return [T_STRING_CONCAT]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Sets up the sniff. |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function setUp(): void |
|
127
|
|
|
{ |
|
128
|
|
|
parent::setUp(); |
|
129
|
|
|
|
|
130
|
|
|
$nextToken = $this->tokens[$this->stackPos + 1]; |
|
131
|
|
|
$prevToken = $this->tokens[$this->stackPos - 1]; |
|
132
|
|
|
|
|
133
|
|
|
$this->nextIsWhitespace = $nextToken['code'] === T_WHITESPACE; |
|
134
|
|
|
$this->prevIsWhitespace = $prevToken['code'] === T_WHITESPACE; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.