forked from symplify/coding-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodChainingNewlineFixer.php
More file actions
175 lines (149 loc) · 5.11 KB
/
MethodChainingNewlineFixer.php
File metadata and controls
175 lines (149 loc) · 5.11 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?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 PhpCsFixer\WhitespacesFixerConfig;
use SplFileInfo;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\TokenAnalyzer\ChainMethodCallAnalyzer;
use Symplify\CodingStandard\TokenRunner\Analyzer\FixerAnalyzer\BlockFinder;
use Symplify\CodingStandard\TokenRunner\ValueObject\BlockInfo;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\CodingStandard\Tests\Fixer\Spacing\MethodChainingNewlineFixer\MethodChainingNewlineFixerTest
*/
final class MethodChainingNewlineFixer extends AbstractSymplifyFixer implements DocumentedRuleInterface
{
/**
* @var string
*/
private const ERROR_MESSAGE = 'Each chain method call must be on own line';
public function __construct(
private readonly WhitespacesFixerConfig $whitespacesFixerConfig,
private readonly BlockFinder $blockFinder,
private readonly ChainMethodCallAnalyzer $chainMethodCallAnalyzer
) {
}
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(self::ERROR_MESSAGE, []);
}
/**
* Must run before
*
* @see \PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer::getPriority()
*/
public function getPriority(): int
{
return 39;
}
/**
* @param Tokens<Token> $tokens
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_OBJECT_OPERATOR]);
}
/**
* @param Tokens<Token> $tokens
*/
public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
{
// function arguments, function call parameters, lambda use()
for ($index = 1, $count = count($tokens); $index < $count; ++$index) {
$currentToken = $tokens[$index];
if (! $currentToken->isGivenKind(T_OBJECT_OPERATOR)) {
continue;
}
if (! $this->shouldPrefixNewline($tokens, $index)) {
continue;
}
$tokens->ensureWhitespaceAtIndex($index, 0, $this->whitespacesFixerConfig->getLineEnding());
++$index;
}
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
$someClass->firstCall()->secondCall();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$someClass->firstCall()
->secondCall();
CODE_SAMPLE
),
]);
}
/**
* @param Tokens<Token> $tokens
*/
private function shouldPrefixNewline(Tokens $tokens, int $objectOperatorIndex): bool
{
for ($i = $objectOperatorIndex; $i >= 0; --$i) {
/** @var Token $currentToken */
$currentToken = $tokens[$i];
if ($currentToken->equals(')')) {
return $this->shouldBracketPrefix($tokens, $i, $objectOperatorIndex);
}
if ($currentToken->isGivenKind([T_NEW, T_VARIABLE])) {
return false;
}
if ($currentToken->getContent() === '(') {
return false;
}
}
return false;
}
/**
* @param Tokens<Token> $tokens
*/
private function isDoubleBracket(Tokens $tokens, int $position): bool
{
/** @var int $nextTokenPosition */
$nextTokenPosition = $tokens->getNextNonWhitespace($position);
/** @var Token $nextToken */
$nextToken = $tokens[$nextTokenPosition];
return $nextToken->getContent() === ')';
}
/**
* Matches e.g.: - app([ ])->some()
*
* @param Tokens<Token> $tokens
*/
private function isPreceededByOpenedCallInAnotherBracket(Tokens $tokens, int $position): bool
{
$blockInfo = $this->blockFinder->findInTokensByEdge($tokens, $position);
if (! $blockInfo instanceof BlockInfo) {
return false;
}
return $tokens->isPartialCodeMultiline($blockInfo->getStart(), $blockInfo->getEnd());
}
/**
* @param Tokens<Token> $tokens
*/
private function shouldBracketPrefix(Tokens $tokens, int $position, int $objectOperatorIndex): bool
{
if ($this->isDoubleBracket($tokens, $position)) {
return false;
}
if ($this->chainMethodCallAnalyzer->isPartOfMethodCallOrArray($tokens, $position)) {
return false;
}
if ($this->chainMethodCallAnalyzer->isPreceededByFuncCall($tokens, $position)) {
return false;
}
if ($this->isPreceededByOpenedCallInAnotherBracket($tokens, $position)) {
return false;
}
// all good, there is a newline
return ! $tokens->isPartialCodeMultiline($position, $objectOperatorIndex);
}
}