Skip to content

Commit c628974

Browse files
committed
Improved indentation processing
1 parent 3d81857 commit c628974

File tree

152 files changed

+859
-737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+859
-737
lines changed

SlevomatCodingStandard/Helpers/FixerHelper.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,25 @@
1212
class FixerHelper
1313
{
1414

15+
public static function add(File $phpcsFile, int $pointer, string $content): void
16+
{
17+
$phpcsFile->fixer->addContent($pointer, IndentationHelper::convertSpacesToTabs($phpcsFile, $content));
18+
}
19+
20+
public static function addBefore(File $phpcsFile, int $pointer, string $content): void
21+
{
22+
$phpcsFile->fixer->addContentBefore($pointer, IndentationHelper::convertSpacesToTabs($phpcsFile, $content));
23+
}
24+
25+
public static function replace(File $phpcsFile, int $pointer, string $content): void
26+
{
27+
$phpcsFile->fixer->replaceToken($pointer, IndentationHelper::convertSpacesToTabs($phpcsFile, $content));
28+
}
29+
1530
public static function change(File $phpcsFile, int $startPointer, int $endPointer, string $content): void
1631
{
1732
self::removeBetweenIncluding($phpcsFile, $startPointer, $endPointer);
18-
$phpcsFile->fixer->replaceToken($startPointer, $content);
33+
self::replace($phpcsFile, $startPointer, $content);
1934
}
2035

2136
public static function removeBetween(File $phpcsFile, int $startPointer, int $endPointer): void
@@ -48,7 +63,7 @@ public static function removeWhitespaceAfter(File $phpcsFile, int $pointer): voi
4863
break;
4964
}
5065

51-
$phpcsFile->fixer->replaceToken($i, '');
66+
self::replace($phpcsFile, $i, '');
5267
}
5368
}
5469

SlevomatCodingStandard/Helpers/IndentationHelper.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SlevomatCodingStandard\Helpers;
44

55
use PHP_CodeSniffer\Files\File;
6+
use function floor;
67
use function in_array;
78
use function ltrim;
89
use function preg_replace_callback;
@@ -23,34 +24,27 @@ class IndentationHelper
2324

2425
public const DEFAULT_INDENTATION_WIDTH = 4;
2526

26-
public const TAB_INDENT = "\t";
27-
public const SPACES_INDENT = ' ';
28-
2927
public static function getIndentation(File $phpcsFile, int $pointer): string
3028
{
3129
$firstPointerOnLine = TokenHelper::findFirstTokenOnLine($phpcsFile, $pointer);
3230

3331
return TokenHelper::getContent($phpcsFile, $firstPointerOnLine, $pointer - 1);
3432
}
3533

36-
public static function addIndentation(string $indentation, int $level = 1): string
34+
public static function addIndentation(File $phpcsFile, string $indentation, int $level = 1): string
3735
{
38-
$whitespace = self::getOneIndentationLevel($indentation);
39-
40-
return $indentation . str_repeat($whitespace, $level);
36+
return $indentation . str_repeat(self::getOneIndentationLevel($phpcsFile), $level);
4137
}
4238

43-
public static function getOneIndentationLevel(string $indentation): string
39+
public static function getOneIndentationLevel(File $phpcsFile): string
4440
{
45-
return $indentation === ''
46-
? self::TAB_INDENT
47-
: ($indentation[0] === self::TAB_INDENT ? self::TAB_INDENT : self::SPACES_INDENT);
41+
return str_repeat(' ', $phpcsFile->config->tabWidth !== 0 ? $phpcsFile->config->tabWidth : self::DEFAULT_INDENTATION_WIDTH);
4842
}
4943

5044
/**
5145
* @param list<int> $codePointers
5246
*/
53-
public static function fixIndentation(File $phpcsFile, array $codePointers, string $defaultIndentation): string
47+
public static function removeIndentation(File $phpcsFile, array $codePointers, string $defaultIndentation): string
5448
{
5549
$tokens = $phpcsFile->getTokens();
5650

@@ -59,6 +53,9 @@ public static function fixIndentation(File $phpcsFile, array $codePointers, stri
5953
$code = '';
6054
$inHeredoc = false;
6155

56+
$indentation = self::getOneIndentationLevel($phpcsFile);
57+
$indentationLength = strlen($indentation);
58+
6259
foreach ($codePointers as $no => $codePointer) {
6360
$content = $tokens[$codePointer]['content'];
6461

@@ -71,10 +68,8 @@ public static function fixIndentation(File $phpcsFile, array $codePointers, stri
7168
) {
7269
if ($content === $phpcsFile->eolChar) {
7370
// Nothing
74-
} elseif ($content[0] === self::TAB_INDENT) {
75-
$content = substr($content, 1);
76-
} elseif (substr($content, 0, self::DEFAULT_INDENTATION_WIDTH) === self::SPACES_INDENT) {
77-
$content = substr($content, self::DEFAULT_INDENTATION_WIDTH);
71+
} elseif (substr($content, 0, $indentationLength) === $indentation) {
72+
$content = substr($content, $indentationLength);
7873
} else {
7974
$content = $defaultIndentation . ltrim($content);
8075
}
@@ -92,14 +87,19 @@ public static function fixIndentation(File $phpcsFile, array $codePointers, stri
9287
return rtrim($code);
9388
}
9489

95-
public static function convertTabsToSpaces(File $phpcsFile, string $code): string
90+
public static function convertSpacesToTabs(File $phpcsFile, string $code): string
9691
{
97-
return preg_replace_callback('~^(\t+)~', static function (array $matches) use ($phpcsFile): string {
98-
$indentation = str_repeat(
99-
' ',
100-
$phpcsFile->config->tabWidth !== 0 ? $phpcsFile->config->tabWidth : self::DEFAULT_INDENTATION_WIDTH,
101-
);
102-
return str_repeat($indentation, strlen($matches[1]));
92+
// @codeCoverageIgnoreStart
93+
if ($phpcsFile->config->tabWidth === 0) {
94+
return $code;
95+
}
96+
// @codeCoverageIgnoreEnd
97+
98+
return preg_replace_callback('~^([ ]+)~m', static function (array $matches) use ($phpcsFile): string {
99+
$tabsCount = (int) floor(strlen($matches[1]) / $phpcsFile->config->tabWidth);
100+
$spacesCountToRemove = $tabsCount * $phpcsFile->config->tabWidth;
101+
102+
return str_repeat("\t", $tabsCount) . substr($matches[1], $spacesCountToRemove);
103103
}, $code);
104104
}
105105

SlevomatCodingStandard/Sniffs/Arrays/AlphabeticallySortedByKeysSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private function fix(File $phpcsFile, array $keyValues): void
6666
$pointerStart = $keyValues[0]->getPointerStart();
6767
$pointerEnd = $keyValues[count($keyValues) - 1]->getPointerEnd();
6868

69-
// determine indent to use
69+
// Determine indent to use
7070
$indent = ArrayHelper::getIndentation($keyValues);
7171

7272
usort($keyValues, static fn ($a1, $a2) => strnatcasecmp((string) $a1->getKey(), (string) $a2->getKey()));

SlevomatCodingStandard/Sniffs/Arrays/ArrayAccessSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\FixerHelper;
78
use SlevomatCodingStandard\Helpers\TokenHelper;
89
use const T_CLOSE_SQUARE_BRACKET;
910
use const T_OPEN_SQUARE_BRACKET;
@@ -69,7 +70,7 @@ private function addError(File $phpcsFile, int $stackPointer, string $error, str
6970
}
7071

7172
$phpcsFile->fixer->beginChangeset();
72-
$phpcsFile->fixer->replaceToken($stackPointer - 1, '');
73+
FixerHelper::replace($phpcsFile, $stackPointer - 1, '');
7374
$phpcsFile->fixer->endChangeset();
7475
}
7576

SlevomatCodingStandard/Sniffs/Arrays/MultiLineArrayEndBracketPlacementSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\ArrayHelper;
8+
use SlevomatCodingStandard\Helpers\FixerHelper;
89
use SlevomatCodingStandard\Helpers\TokenHelper;
910
use function in_array;
1011

@@ -54,7 +55,7 @@ public function process(File $phpcsFile, $stackPointer): void
5455
return;
5556
}
5657

57-
$phpcsFile->fixer->addContent($arrayOpenerPointer, $phpcsFile->eolChar);
58+
FixerHelper::add($phpcsFile, $arrayOpenerPointer, $phpcsFile->eolChar);
5859
}
5960

6061
}

SlevomatCodingStandard/Sniffs/Arrays/SingleLineArrayWhitespaceSniff.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\ArrayHelper;
8+
use SlevomatCodingStandard\Helpers\FixerHelper;
89
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
910
use SlevomatCodingStandard\Helpers\TokenHelper;
1011
use function in_array;
@@ -111,7 +112,7 @@ private function checkWhitespaceInEmptyArray(File $phpcsFile, int $arrayStart, i
111112
return;
112113
}
113114

114-
$phpcsFile->fixer->replaceToken($arrayStart + 1, '');
115+
FixerHelper::replace($phpcsFile, $arrayStart + 1, '');
115116
}
116117

117118
private function checkWhitespaceAfterOpeningBracket(File $phpcsFile, int $arrayStart): void
@@ -136,9 +137,13 @@ private function checkWhitespaceAfterOpeningBracket(File $phpcsFile, int $arrayS
136137
}
137138

138139
if ($spaceLength === 0) {
139-
$phpcsFile->fixer->addContent($arrayStart, str_repeat(' ', $this->spacesAroundBrackets));
140+
FixerHelper::add($phpcsFile, $arrayStart, str_repeat(' ', $this->spacesAroundBrackets));
140141
} else {
141-
$phpcsFile->fixer->replaceToken($whitespacePointer, str_repeat(' ', $this->spacesAroundBrackets));
142+
FixerHelper::replace(
143+
$phpcsFile,
144+
$whitespacePointer,
145+
str_repeat(' ', $this->spacesAroundBrackets),
146+
);
142147
}
143148
}
144149

@@ -164,9 +169,13 @@ private function checkWhitespaceBeforeClosingBracket(File $phpcsFile, int $array
164169
}
165170

166171
if ($spaceLength === 0) {
167-
$phpcsFile->fixer->addContentBefore($arrayEnd, str_repeat(' ', $this->spacesAroundBrackets));
172+
FixerHelper::addBefore($phpcsFile, $arrayEnd, str_repeat(' ', $this->spacesAroundBrackets));
168173
} else {
169-
$phpcsFile->fixer->replaceToken($whitespacePointer, str_repeat(' ', $this->spacesAroundBrackets));
174+
FixerHelper::replace(
175+
$phpcsFile,
176+
$whitespacePointer,
177+
str_repeat(' ', $this->spacesAroundBrackets),
178+
);
170179
}
171180
}
172181

@@ -192,7 +201,7 @@ private function checkWhitespaceBeforeComma(File $phpcsFile, int $comma): void
192201
return;
193202
}
194203

195-
$phpcsFile->fixer->replaceToken($comma - 1, '');
204+
FixerHelper::replace($phpcsFile, $comma - 1, '');
196205
}
197206

198207
private function checkWhitespaceAfterComma(File $phpcsFile, int $comma): void
@@ -203,7 +212,7 @@ private function checkWhitespaceAfterComma(File $phpcsFile, int $comma): void
203212
$error = sprintf('Expected 1 space between comma and "%s", 0 found.', $tokens[$comma + 1]['content']);
204213
$fix = $phpcsFile->addFixableError($error, $comma, self::CODE_SPACE_AFTER_COMMA);
205214
if ($fix) {
206-
$phpcsFile->fixer->addContent($comma, ' ');
215+
FixerHelper::add($phpcsFile, $comma, ' ');
207216
}
208217

209218
return;
@@ -220,7 +229,7 @@ private function checkWhitespaceAfterComma(File $phpcsFile, int $comma): void
220229
return;
221230
}
222231

223-
$phpcsFile->fixer->replaceToken($comma + 1, ' ');
232+
FixerHelper::replace($phpcsFile, $comma + 1, ' ');
224233
}
225234

226235
}

SlevomatCodingStandard/Sniffs/Arrays/TrailingArrayCommaSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\ArrayHelper;
8+
use SlevomatCodingStandard\Helpers\FixerHelper;
89
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
910
use SlevomatCodingStandard\Helpers\TokenHelper;
1011
use function in_array;
@@ -72,7 +73,7 @@ public function process(File $phpcsFile, $stackPointer): void
7273
}
7374

7475
$phpcsFile->fixer->beginChangeset();
75-
$phpcsFile->fixer->addContent($pointerPreviousToClose, ',');
76+
FixerHelper::add($phpcsFile, $pointerPreviousToClose, ',');
7677
$phpcsFile->fixer->endChangeset();
7778
}
7879

SlevomatCodingStandard/Sniffs/Attributes/AttributeAndTargetSpacingSniff.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function process(File $phpcsFile, $attributeOpenerPointer): void
9797
$phpcsFile->fixer->beginChangeset();
9898

9999
FixerHelper::removeWhitespaceAfter($phpcsFile, $attributeCloserPointer);
100-
$phpcsFile->fixer->addContentBefore($pointerAfter, str_repeat($phpcsFile->eolChar, $this->linesCount + 1) . $indentation);
100+
FixerHelper::addBefore($phpcsFile, $pointerAfter, str_repeat($phpcsFile->eolChar, $this->linesCount + 1) . $indentation);
101101

102102
$phpcsFile->fixer->endChangeset();
103103

@@ -109,8 +109,7 @@ public function process(File $phpcsFile, $attributeOpenerPointer): void
109109
$phpcsFile->fixer->beginChangeset();
110110

111111
FixerHelper::removeBetween($phpcsFile, $attributeCloserPointer, $firstTokenOnLine);
112-
113-
$phpcsFile->fixer->addContentBefore($firstTokenOnLine, str_repeat($phpcsFile->eolChar, $this->linesCount + 1));
112+
FixerHelper::addBefore($phpcsFile, $firstTokenOnLine, str_repeat($phpcsFile->eolChar, $this->linesCount + 1));
114113

115114
$phpcsFile->fixer->endChangeset();
116115
}

SlevomatCodingStandard/Sniffs/Attributes/AttributesOrderSniff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,16 @@ public function process(File $phpcsFile, $attributeOpenerPointer): void
160160
foreach (array_keys($expectedOrder) as $position => $attributesGroupNo) {
161161
if ($areOnSameLine) {
162162
if ($position !== 0) {
163-
$phpcsFile->fixer->addContent($attributesStartPointer, ' ');
163+
FixerHelper::add($phpcsFile, $attributesStartPointer, ' ');
164164
}
165165

166-
$phpcsFile->fixer->addContent($attributesStartPointer, $attributesGroupsContent[$attributesGroupNo]);
166+
FixerHelper::add($phpcsFile, $attributesStartPointer, $attributesGroupsContent[$attributesGroupNo]);
167167
} else {
168168
if ($position !== 0) {
169-
$phpcsFile->fixer->addContent($attributesStartPointer, $indentation);
169+
FixerHelper::add($phpcsFile, $attributesStartPointer, $indentation);
170170
}
171171

172-
$phpcsFile->fixer->addContent($attributesStartPointer, $attributesGroupsContent[$attributesGroupNo]);
172+
FixerHelper::add($phpcsFile, $attributesStartPointer, $attributesGroupsContent[$attributesGroupNo]);
173173

174174
if ($position !== count($attributesGroups) - 1) {
175175
$phpcsFile->fixer->addNewline($attributesStartPointer);

SlevomatCodingStandard/Sniffs/Attributes/DisallowAttributesJoiningSniff.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\AttributeHelper;
8+
use SlevomatCodingStandard\Helpers\FixerHelper;
89
use function count;
910
use function sprintf;
1011
use const T_ATTRIBUTE;
@@ -55,15 +56,15 @@ public function process(File $phpcsFile, $attributeOpenerPointer): void
5556
$previousAttribute = $attributes[$i - 1];
5657
$attribute = $attributes[$i];
5758

58-
$phpcsFile->fixer->addContent($previousAttribute->getEndPointer(), ']');
59+
FixerHelper::add($phpcsFile, $previousAttribute->getEndPointer(), ']');
5960

6061
for ($j = $previousAttribute->getEndPointer() + 1; $j < $attribute->getStartPointer(); $j++) {
6162
if ($phpcsFile->fixer->getTokenContent($j) === ',') {
62-
$phpcsFile->fixer->replaceToken($j, '');
63+
FixerHelper::replace($phpcsFile, $j, '');
6364
}
6465
}
6566

66-
$phpcsFile->fixer->addContentBefore($attribute->getStartPointer(), '#[');
67+
FixerHelper::addBefore($phpcsFile, $attribute->getStartPointer(), '#[');
6768
}
6869

6970
$phpcsFile->fixer->endChangeset();

0 commit comments

Comments
 (0)