-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathVariableInDoubleQuotedStringSniff.php
156 lines (135 loc) · 4.37 KB
/
VariableInDoubleQuotedStringSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* This file is part of the mo4-coding-standard (phpcs standard)
*
* @author Xaver Loppenstedt <[email protected]>
*
* @license http://spdx.org/licenses/MIT MIT License
*
* @link https://github.com/mayflower/mo4-coding-standard
*/
declare(strict_types=1);
namespace MO4\Sniffs\Strings;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Variable in Double Quoted String sniff.
*
* Variables in double quoted strings must be surrounded by { }
*
* @author Xaver Loppenstedt <[email protected]>
*
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
*
* @license http://spdx.org/licenses/MIT MIT License
*
* @link https://github.com/mayflower/mo4-coding-standard
*
* @psalm-api
*/
class VariableInDoubleQuotedStringSniff implements Sniff
{
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array<int, string>
*
* @see Tokens.php
*/
public function register(): array
{
return [T_DOUBLE_QUOTED_STRING];
}
/**
* Called when one of the token types that this sniff is listening for
* is found.
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
*
* @param File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
$varRegExp = '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/';
$tokens = $phpcsFile->getTokens();
$content = $tokens[$stackPtr]['content'];
$matches = [];
\preg_match_all($varRegExp, $content, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches as $match) {
foreach ($match as [$var, $pos]) {
if (1 !== $pos && '{' === $content[($pos - 1)]) {
continue;
}
if (\strpos(\substr($content, 0, $pos), '{') > 0
&& !\str_contains(\substr($content, 0, $pos), '}')
) {
continue;
}
$lastOpeningBrace = \strrpos(\substr($content, 0, $pos), '{');
if (false !== $lastOpeningBrace
&& '$' === $content[($lastOpeningBrace + 1)]
) {
$lastClosingBrace = \strrpos(\substr($content, 0, $pos), '}');
if (false !== $lastClosingBrace
&& $lastClosingBrace < $lastOpeningBrace
) {
continue;
}
}
$fix = $phpcsFile->addFixableError(
\sprintf(
'must surround variable %s with { }',
$var
),
$stackPtr,
'NotSurroundedWithBraces'
);
if (true !== $fix) {
continue;
}
$correctVariable = $this->surroundVariableWithBraces(
$content,
$pos,
$var
);
$this->fixPhpCsFile($stackPtr, $correctVariable, $phpcsFile);
}
}
}
/**
* Surrounds a variable with curly brackets
*
* @param string $content content
* @param int $pos position
* @param string $var variable
*
* @return string
*/
private function surroundVariableWithBraces(string $content, int $pos, string $var): string
{
$before = \substr($content, 0, $pos);
$after = \substr($content, ($pos + \strlen($var)));
return $before.'{'.$var.'}'.$after;
}
/**
* Fixes the file
*
* @param int $stackPtr stack pointer
* @param string $correctVariable correct variable
* @param File $phpCsFile PHP_CodeSniffer File object
*
* @return void
*/
private function fixPhpCsFile(int $stackPtr, string $correctVariable, File $phpCsFile): void
{
$phpCsFile->fixer->beginChangeset();
$phpCsFile->fixer->replaceToken($stackPtr, $correctVariable);
$phpCsFile->fixer->endChangeset();
}
}