Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUseSprintfInExceptionsSniff.php
More file actions
Latest commit
74 lines (56 loc) · 1.93 KB
/
Copy pathUseSprintfInExceptionsSniff.php
File metadata and controls
74 lines (56 loc) · 1.93 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespaceContao\EasyCodingStandard\Sniffs;
usePHP_CodeSniffer\Files\File;
usePHP_CodeSniffer\Sniffs\Sniff;
useSlevomatCodingStandard\Helpers\TokenHelper;
finalclass UseSprintfInExceptionsSniff implements Sniff
{
publicfunctionregister(): array
{
return [T_THROW];
}
publicfunctionprocess(File$phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
if (T_THROW !== $tokens[$stackPtr]['code']) {
return;
}
$next = $this->getNextNonWhitespaceToken($tokens, $stackPtr);
// We are not dealing with "throw new"
if (T_NEW !== $tokens[$next]['code']) {
return;
}
$next = TokenHelper::findNext($phpcsFile, [T_STRING, T_NAME_FULLY_QUALIFIED], $next);
// We are not dealing with an exception class
if (!str_ends_with((string) $tokens[$next]['content'], 'Exception')) {
return;
}
$next = $this->getNextNonWhitespaceToken($tokens, $next);
// There is no opening parenthesis after the class name
if (T_OPEN_PARENTHESIS !== $tokens[$next]['code']) {
return;
}
$next = $this->getNextNonWhitespaceToken($tokens, $next);
// A non-interpolated string will have the T_CONSTANT_ENCAPSED_STRING code, so it
// is enough to check for T_DOUBLE_QUOTED_STRING here
if (T_DOUBLE_QUOTED_STRING !== $tokens[$next]['code']) {
return;
}
$phpcsFile->addError('Using string interpolation in exception messages is not allowed. Use sprintf() instead.', $stackPtr, self::class);
}
privatefunctiongetNextNonWhitespaceToken(array$tokens, int$index): int
{
do {
++$index;
} while (T_WHITESPACE === $tokens[$index]['code']);
return$index;
}
}