forked from symplify/coding-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceAfterCommaHereNowDocFixer.php
More file actions
95 lines (83 loc) · 2.56 KB
/
SpaceAfterCommaHereNowDocFixer.php
File metadata and controls
95 lines (83 loc) · 2.56 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace Symplify\CodingStandard\Fixer\Spacing;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\CodingStandard\Tests\Fixer\Spacing\SpaceAfterCommaHereNowDocFixer\SpaceAfterCommaHereNowDocFixerTest
* @see https://3v4l.org/KPZXU
*/
final class SpaceAfterCommaHereNowDocFixer extends AbstractSymplifyFixer implements DocumentedRuleInterface
{
/**
* @var string
*/
private const ERROR_MESSAGE = 'Add space after nowdoc and heredoc keyword, to prevent bugs on PHP 7.2 and lower, see https://laravel-news.com/flexible-heredoc-and-nowdoc-coming-to-php-7-3';
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(self::ERROR_MESSAGE, []);
}
/**
* @param Tokens<Token> $tokens
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_START_HEREDOC, T_START_NOWDOC]);
}
/**
* @param Tokens<Token> $tokens
*/
public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
{
// function arguments, function call parameters, lambda use()
for ($position = count($tokens) - 1; $position >= 0; --$position) {
/** @var Token $token */
$token = $tokens[$position];
if (! $token->isGivenKind(T_END_HEREDOC)) {
continue;
}
// nothing
if (! isset($tokens[$position + 1])) {
continue;
}
/** @var Token $nextToken */
$nextToken = $tokens[$position + 1];
if (! in_array($nextToken->getContent(), [',', ']'], true)) {
continue;
}
$tokens->ensureWhitespaceAtIndex($position + 1, 0, PHP_EOL);
}
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
$values = [
<<<RECTIFY
Some content
RECTIFY,
1000
];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$values = [
<<<RECTIFY
Some content
RECTIFY
,
1000
];
CODE_SAMPLE
),
]);
}
}