-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathArrayItemNewliner.php
More file actions
57 lines (47 loc) · 1.75 KB
/
ArrayItemNewliner.php
File metadata and controls
57 lines (47 loc) · 1.75 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
<?php
declare(strict_types=1);
namespace Symplify\CodingStandard\TokenRunner\Arrays;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\WhitespacesFixerConfig;
use Symplify\CodingStandard\TokenRunner\Analyzer\FixerAnalyzer\ArrayAnalyzer;
use Symplify\CodingStandard\TokenRunner\ValueObject\BlockInfo;
final readonly class ArrayItemNewliner
{
public function __construct(
private ArrayAnalyzer $arrayAnalyzer,
private WhitespacesFixerConfig $whitespacesFixerConfig
) {
}
/**
* @param Tokens<Token> $tokens
*/
public function fixArrayOpener(Tokens $tokens, BlockInfo $blockInfo): void
{
$this->arrayAnalyzer->traverseArrayWithoutNesting(
$tokens,
$blockInfo,
function (Token $token, int $position, Tokens $tokens): void {
if ($token->getContent() !== ',') {
return;
}
$nextTokenPosition = $position + 1;
$nextToken = $tokens[$nextTokenPosition] ?? null;
if (! $nextToken instanceof Token) {
return;
}
if (\str_contains($nextToken->getContent(), "\n")) {
return;
}
$lookaheadPosition = $tokens->getNonWhitespaceSibling($position, 1, " \t\r\0\x0B");
if ($lookaheadPosition !== null && $tokens[$lookaheadPosition]->isGivenKind(T_COMMENT)) {
return;
}
if ($nextToken->getContent() === '{') {
return;
}
$tokens->ensureWhitespaceAtIndex($nextTokenPosition, 0, $this->whitespacesFixerConfig->getLineEnding());
}
);
}
}