Skip to content

Commit c1bcf0c

Browse files
authored
minor: Split BracesFixer (#4884)
1 parent 26204b5 commit c1bcf0c

21 files changed

+125
-716
lines changed

.github/workflows/sca.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ jobs:
113113
if: ${{ env.CHANGED_PHP_FILES }}
114114
run: |
115115
if [ '${{ github.event_name }}' == 'pull_request' ]; then
116-
./dev-tools/vendor/bin/phpmd `echo "$CHANGED_PHP_FILES" | xargs | sed 's/ /,/g'` github phpmd.xml --exclude tests/Fixtures/
116+
./dev-tools/vendor/bin/phpmd `echo "$CHANGED_PHP_FILES" | grep -v /Fixtures/ | xargs | sed 's/ /,/g'` github phpmd.xml
117117
else
118-
./dev-tools/vendor/bin/phpmd `echo "$CHANGED_PHP_FILES" | xargs | sed 's/ /,/g'` ansi phpmd.xml --exclude tests/Fixtures/
118+
./dev-tools/vendor/bin/phpmd `echo "$CHANGED_PHP_FILES" | grep -v /Fixtures/ | xargs | sed 's/ /,/g'` ansi phpmd.xml
119119
fi
120120
121121
- name: Check - ensure test files are not present in the archive

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ parameters:
2323
-
2424
message: '#^.+no value type specified in iterable type.+\.$#'
2525
path: *
26-
count: 540
26+
count: 538
2727
tipsOfTheDay: false
2828
tmpDir: dev-tools/phpstan/cache

src/Fixer/Basic/BracesFixer.php

+24-621
Large diffs are not rendered by default.

src/Fixer/Basic/NoMultipleStatementsPerLineFixer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function getDefinition(): FixerDefinitionInterface
4444
/**
4545
* {@inheritdoc}
4646
*
47-
* Must run after NoEmptyStatementFixer.
47+
* Must run after ControlStructureBracesFixer, NoEmptyStatementFixer.
4848
*/
4949
public function getPriority(): int
5050
{
51-
return 39;
51+
return -1;
5252
}
5353

5454
/**

src/Fixer/ControlStructure/ControlStructureBracesFixer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function isCandidate(Tokens $tokens): bool
4646
/**
4747
* {@inheritdoc}
4848
*
49-
* Must run before ControlStructureContinuationPositionFixer, CurlyBracesPositionFixer.
49+
* Must run before ControlStructureContinuationPositionFixer, CurlyBracesPositionFixer, NoMultipleStatementsPerLineFixer.
5050
*/
5151
public function getPriority(): int
5252
{

src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function configure(array $configuration): void
122122
* {@inheritdoc}
123123
*
124124
* Must run before ArrayIndentationFixer.
125-
* Must run after CombineNestedDirnameFixer, FunctionDeclarationFixer, ImplodeCallFixer, LambdaNotUsedImportFixer, MethodChainingIndentationFixer, NoMultilineWhitespaceAroundDoubleArrowFixer, NoUselessSprintfFixer, PowToExponentiationFixer, StrictParamFixer.
125+
* Must run after CombineNestedDirnameFixer, FunctionDeclarationFixer, ImplodeCallFixer, LambdaNotUsedImportFixer, NoMultilineWhitespaceAroundDoubleArrowFixer, NoUselessSprintfFixer, PowToExponentiationFixer, StrictParamFixer.
126126
*/
127127
public function getPriority(): int
128128
{

src/Fixer/Whitespace/ArrayIndentationFixer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function isCandidate(Tokens $tokens): bool
5454
* {@inheritdoc}
5555
*
5656
* Must run before AlignMultilineCommentFixer, BinaryOperatorSpacesFixer.
57-
* Must run after MethodArgumentSpaceFixer, MethodChainingIndentationFixer.
57+
* Must run after MethodArgumentSpaceFixer.
5858
*/
5959
public function getPriority(): int
6060
{

src/Fixer/Whitespace/LineEndingFixer.php

-10
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ public function getDefinition(): FixerDefinitionInterface
5454
);
5555
}
5656

57-
/**
58-
* {@inheritdoc}
59-
*
60-
* Must run before BracesFixer.
61-
*/
62-
public function getPriority(): int
63-
{
64-
return 40;
65-
}
66-
6757
/**
6858
* {@inheritdoc}
6959
*/

src/Fixer/Whitespace/MethodChainingIndentationFixer.php

+27-11
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ public function getDefinition(): FixerDefinitionInterface
4040
);
4141
}
4242

43-
/**
44-
* {@inheritdoc}
45-
*
46-
* Must run before ArrayIndentationFixer, MethodArgumentSpaceFixer.
47-
* Must run after BracesFixer.
48-
*/
49-
public function getPriority(): int
50-
{
51-
return 34;
52-
}
53-
5443
/**
5544
* {@inheritdoc}
5645
*/
@@ -90,6 +79,33 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
9079
if ($currentIndent !== $expectedIndent) {
9180
$tokens[$index - 1] = new Token([T_WHITESPACE, $lineEnding.$expectedIndent]);
9281
}
82+
83+
$endParenthesisIndex = $tokens->findBlockEnd(
84+
Tokens::BLOCK_TYPE_PARENTHESIS_BRACE,
85+
$tokens->getNextTokenOfKind($index, ['('])
86+
);
87+
88+
for ($searchIndex = $index + 1; $searchIndex < $endParenthesisIndex; ++$searchIndex) {
89+
$searchToken = $tokens[$searchIndex];
90+
91+
if (!$searchToken->isWhitespace()) {
92+
continue;
93+
}
94+
95+
$content = $searchToken->getContent();
96+
97+
if (!Preg::match('/\R/', $content)) {
98+
continue;
99+
}
100+
101+
$content = Preg::replace(
102+
'/(\R)'.$currentIndent.'(\h*)$/D',
103+
'$1'.$expectedIndent.'$2',
104+
$content
105+
);
106+
107+
$tokens[$searchIndex] = new Token([$searchToken->getId(), $content]);
108+
}
93109
}
94110
}
95111

src/Fixer/Whitespace/NoExtraBlankLinesFixer.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,7 @@ private function removeEmptyLinesAfterLineWithTokenAt(int $index): void
441441
continue;
442442
}
443443

444-
$pos = strrpos($content, "\n");
445-
446-
if ($pos + 2 <= \strlen($content)) { // preserve indenting where possible
447-
$newContent = $ending.substr($content, $pos + 1);
448-
} else {
449-
$newContent = $ending;
450-
}
444+
$newContent = Preg::replace('/^.*\R(\h*)$/s', $ending.'$1', $content);
451445

452446
$this->tokens[$i] = new Token([T_WHITESPACE, $newContent]);
453447
}

tests/AutoReview/FixerFactoryTest.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ private static function getFixersPriorityGraph(): array
358358
],
359359
'braces' => [
360360
'heredoc_indentation',
361-
'method_chaining_indentation',
362361
],
363362
'class_attributes_separation' => [
364363
'braces',
@@ -392,6 +391,7 @@ private static function getFixersPriorityGraph(): array
392391
'control_structure_braces' => [
393392
'control_structure_continuation_position',
394393
'curly_braces_position',
394+
'no_multiple_statements_per_line',
395395
],
396396
'declare_strict_types' => [
397397
'blank_line_after_opening_tag',
@@ -473,20 +473,13 @@ private static function getFixersPriorityGraph(): array
473473
'method_argument_space',
474474
'no_spaces_inside_parenthesis',
475475
],
476-
'line_ending' => [
477-
'braces',
478-
],
479476
'list_syntax' => [
480477
'binary_operator_spaces',
481478
'ternary_operator_spaces',
482479
],
483480
'method_argument_space' => [
484481
'array_indentation',
485482
],
486-
'method_chaining_indentation' => [
487-
'array_indentation',
488-
'method_argument_space',
489-
],
490483
'modernize_strpos' => [
491484
'binary_operator_spaces',
492485
'no_extra_blank_lines',

tests/Fixer/Basic/BracesFixerTest.php

+29-20
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ public function bar() {
817817
<?php
818818
if (true) {
819819
echo \'x\';
820-
} ?>
820+
}
821+
?>
821822
<hr />
822823
<?php
823824
}',
@@ -1146,10 +1147,6 @@ public function provideFixMissingBracesAndIndentCases(): iterable
11461147
'<?php
11471148
class ClassName
11481149
{
1149-
1150-
1151-
1152-
11531150
/**
11541151
* comment
11551152
*/
@@ -1900,10 +1897,6 @@ public function __construct(
19001897
[
19011898
'<?php
19021899
class ClassName {
1903-
1904-
1905-
1906-
19071900
/**
19081901
* comment
19091902
*/
@@ -1928,10 +1921,6 @@ class ClassName
19281921
[
19291922
'<?php
19301923
class ClassName {
1931-
1932-
1933-
1934-
19351924
/**
19361925
* comment
19371926
*/
@@ -2233,9 +2222,9 @@ function mixedComplex()
22332222
[
22342223
'<?php
22352224
if (true)
2225+
// foo
2226+
// bar
22362227
{
2237-
// foo
2238-
// bar
22392228
if (true)
22402229
{
22412230
print("foo");
@@ -4884,13 +4873,12 @@ public function providePreserveLineAfterControlBraceCases(): iterable
48844873
}',
48854874
],
48864875
[
4887-
"<?php if (true) {\r\n\r\n // CRLF newline\n}",
4876+
"<?php if (true) {\n // CRLF newline\n}",
48884877
"<?php if (true) {\r\n\r\n// CRLF newline\n}",
48894878
],
48904879
[
48914880
'<?php
48924881
if (true) {
4893-
48944882
// The blank line helps with legibility in nested control structures
48954883
if (true) {
48964884
// if body
@@ -4903,9 +4891,30 @@ public function providePreserveLineAfterControlBraceCases(): iterable
49034891
],
49044892
[
49054893
'<?php
4894+
if (true) {
4895+
// The blank line helps with legibility in nested control structures
4896+
if (true) {
4897+
// if body
4898+
}
4899+
4900+
// if body
4901+
}',
4902+
'<?php
4903+
if (true) {
4904+
4905+
// The blank line helps with legibility in nested control structures
4906+
if (true) {
4907+
// if body
4908+
}
4909+
4910+
// if body
4911+
}',
4912+
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
4913+
],
4914+
[
4915+
'<?php
49064916
if (true)
49074917
{
4908-
49094918
// The blank line helps with legibility in nested control structures
49104919
if (true)
49114920
{
@@ -4927,13 +4936,13 @@ public function providePreserveLineAfterControlBraceCases(): iterable
49274936
self::CONFIGURATION_OOP_POSITION_SAME_LINE + self::CONFIGURATION_CTRL_STRUCT_POSITION_NEXT_LINE,
49284937
],
49294938
[
4939+
"<?php if (true) {\n // CRLF newline\n}",
49304940
"<?php if (true) {\r\n\r\n // CRLF newline\n}",
4931-
null,
49324941
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
49334942
],
49344943
[
49354944
"<?php if (true)
4936-
{\r\n\r\n // CRLF newline\n}",
4945+
{\n // CRLF newline\n}",
49374946
"<?php if (true){\r\n\r\n// CRLF newline\n}",
49384947
self::CONFIGURATION_OOP_POSITION_SAME_LINE + self::CONFIGURATION_CTRL_STRUCT_POSITION_NEXT_LINE,
49394948
],

tests/Fixer/Whitespace/MethodChainingIndentationFixerTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ public function provideFixCases(): array
214214
215215
$user->setFoo(1)
216216
->setBar([
217-
1 => 1,
218-
])
217+
1 => 1,
218+
])
219219
->setBaz(true)
220220
->setX(array(
221-
2 => 2,
222-
))
221+
2 => 2,
222+
))
223223
->setY();
224224
',
225225
'<?php

tests/Fixtures/Integration/misc/method_chaining_indentation,array_indentation.test

+20
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,29 @@ Foo::bar([
99
])
1010
->call();
1111

12+
function foo($foo)
13+
{
14+
$foo
15+
->bar()
16+
->baz([
17+
'foo' => 'bar',
18+
])
19+
;
20+
}
21+
1222
--INPUT--
1323
<?php
1424
Foo::bar([
1525
'baz',
1626
])
1727
->call();
28+
29+
function foo($foo)
30+
{
31+
$foo
32+
->bar()
33+
->baz([
34+
'foo' => 'bar',
35+
])
36+
;
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Integration of fixers: control_structure_braces,no_multiple_statements_per_line.
3+
--RULESET--
4+
{"control_structure_braces": true, "no_multiple_statements_per_line": true}
5+
--EXPECT--
6+
<?php
7+
$callback = function () { if ($a) { return true; } return false; };
8+
9+
--INPUT--
10+
<?php
11+
$callback = function () { if ($a) return true; return false; };

tests/Fixtures/Integration/priority/method_chaining_indentation,array_indentation.test

-27
This file was deleted.

0 commit comments

Comments
 (0)