Skip to content

Commit 0e4df34

Browse files
committed
Integer settings in normalized always on process() method start
1 parent c3324df commit 0e4df34

28 files changed

+274
-210
lines changed

SlevomatCodingStandard/Helpers/SniffSettingsHelper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public static function normalizeInteger($settings): int
2525
return (int) trim((string) $settings);
2626
}
2727

28+
/**
29+
* @param string|int|null $settings
30+
*/
31+
public static function normalizeNullableInteger($settings): ?int
32+
{
33+
return $settings !== null ? (int) trim((string) $settings) : null;
34+
}
35+
2836
/**
2937
* @param string[] $settings
3038
* @return string[]

SlevomatCodingStandard/Sniffs/Arrays/SingleLineArrayWhitespaceSniff.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function register(): array
4242
*/
4343
public function process(File $phpcsFile, $stackPointer): int
4444
{
45+
$this->spacesAroundBrackets = SniffSettingsHelper::normalizeInteger($this->spacesAroundBrackets);
46+
4547
$tokens = $phpcsFile->getTokens();
4648

4749
$arrayStart = $stackPointer;
@@ -99,11 +101,6 @@ public function process(File $phpcsFile, $stackPointer): int
99101
return $arrayStart + 1;
100102
}
101103

102-
protected function getSpacesAroundBrackets(): int
103-
{
104-
return SniffSettingsHelper::normalizeInteger($this->spacesAroundBrackets);
105-
}
106-
107104
private function checkWhitespaceInEmptyArray(File $phpcsFile, int $arrayStart, int $arrayEnd): void
108105
{
109106
if ($arrayEnd - $arrayStart === 1) {
@@ -130,21 +127,20 @@ private function checkWhitespaceAfterOpeningBracket(File $phpcsFile, int $arrayS
130127
$spaceLength = $tokens[$whitespacePointer]['length'];
131128
}
132129

133-
$spacesAroundBrackets = $this->getSpacesAroundBrackets();
134-
if ($spaceLength === $spacesAroundBrackets) {
130+
if ($spaceLength === $this->spacesAroundBrackets) {
135131
return;
136132
}
137133

138-
$error = sprintf('Expected %d spaces after array opening bracket, %d found.', $spacesAroundBrackets, $spaceLength);
134+
$error = sprintf('Expected %d spaces after array opening bracket, %d found.', $this->spacesAroundBrackets, $spaceLength);
139135
$fix = $phpcsFile->addFixableError($error, $arrayStart, self::CODE_SPACE_AFTER_ARRAY_OPEN);
140136
if (!$fix) {
141137
return;
142138
}
143139

144140
if ($spaceLength === 0) {
145-
$phpcsFile->fixer->addContent($arrayStart, str_repeat(' ', $spacesAroundBrackets));
141+
$phpcsFile->fixer->addContent($arrayStart, str_repeat(' ', $this->spacesAroundBrackets));
146142
} else {
147-
$phpcsFile->fixer->replaceToken($whitespacePointer, str_repeat(' ', $spacesAroundBrackets));
143+
$phpcsFile->fixer->replaceToken($whitespacePointer, str_repeat(' ', $this->spacesAroundBrackets));
148144
}
149145
}
150146

@@ -159,21 +155,20 @@ private function checkWhitespaceBeforeClosingBracket(File $phpcsFile, int $array
159155
$spaceLength = $tokens[$whitespacePointer]['length'];
160156
}
161157

162-
$spacesAroundBrackets = $this->getSpacesAroundBrackets();
163-
if ($spaceLength === $spacesAroundBrackets) {
158+
if ($spaceLength === $this->spacesAroundBrackets) {
164159
return;
165160
}
166161

167-
$error = sprintf('Expected %d spaces before array closing bracket, %d found.', $spacesAroundBrackets, $spaceLength);
162+
$error = sprintf('Expected %d spaces before array closing bracket, %d found.', $this->spacesAroundBrackets, $spaceLength);
168163
$fix = $phpcsFile->addFixableError($error, $arrayEnd, self::CODE_SPACE_BEFORE_ARRAY_CLOSE);
169164
if (!$fix) {
170165
return;
171166
}
172167

173168
if ($spaceLength === 0) {
174-
$phpcsFile->fixer->addContentBefore($arrayEnd, str_repeat(' ', $spacesAroundBrackets));
169+
$phpcsFile->fixer->addContentBefore($arrayEnd, str_repeat(' ', $this->spacesAroundBrackets));
175170
} else {
176-
$phpcsFile->fixer->replaceToken($whitespacePointer, str_repeat(' ', $spacesAroundBrackets));
171+
$phpcsFile->fixer->replaceToken($whitespacePointer, str_repeat(' ', $this->spacesAroundBrackets));
177172
}
178173
}
179174

SlevomatCodingStandard/Sniffs/Classes/AbstractPropertyAndConstantSpacing.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ abstract protected function addError(File $phpcsFile, int $pointer, int $min, in
5353
*/
5454
public function process(File $phpcsFile, $pointer): int
5555
{
56+
$this->minLinesCountBeforeWithComment = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithComment);
57+
$this->maxLinesCountBeforeWithComment = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeWithComment);
58+
$this->minLinesCountBeforeWithoutComment = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithoutComment);
59+
$this->maxLinesCountBeforeWithoutComment = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeWithoutComment);
60+
5661
$tokens = $phpcsFile->getTokens();
5762

5863
$classPointer = ClassHelper::getClassPointer($phpcsFile, $pointer);
@@ -81,11 +86,11 @@ public function process(File $phpcsFile, $pointer): int
8186

8287
$linesBetween = $tokens[$nextPointer]['line'] - $tokens[$semicolonPointer]['line'] - 1;
8388
if (in_array($tokens[$nextPointer]['code'], [T_DOC_COMMENT_OPEN_TAG, T_COMMENT, T_ATTRIBUTE], true)) {
84-
$minExpectedLines = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithComment);
85-
$maxExpectedLines = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeWithComment);
89+
$minExpectedLines = $this->minLinesCountBeforeWithComment;
90+
$maxExpectedLines = $this->maxLinesCountBeforeWithComment;
8691
} else {
87-
$minExpectedLines = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithoutComment);
88-
$maxExpectedLines = SniffSettingsHelper::normalizeInteger($this->maxLinesCountBeforeWithoutComment);
92+
$minExpectedLines = $this->minLinesCountBeforeWithoutComment;
93+
$maxExpectedLines = $this->maxLinesCountBeforeWithoutComment;
8994
}
9095

9196
if ($linesBetween >= $minExpectedLines && $linesBetween <= $maxExpectedLines) {

SlevomatCodingStandard/Sniffs/Classes/ClassMemberSpacingSniff.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public function register(): array
6262
*/
6363
public function process(File $phpcsFile, $classPointer): void
6464
{
65+
$this->linesCountBetweenMembers = SniffSettingsHelper::normalizeInteger($this->linesCountBetweenMembers);
66+
6567
$tokens = $phpcsFile->getTokens();
6668

6769
$memberPointer = null;
@@ -110,26 +112,28 @@ public function process(File $phpcsFile, $classPointer): void
110112
$memberStartPointer = $this->getMemberStartPointer($phpcsFile, $memberPointer);
111113

112114
$actualLinesCount = $tokens[$memberStartPointer]['line'] - $tokens[$previousMemberEndPointer]['line'] - 1;
113-
$requiredLinesCount = SniffSettingsHelper::normalizeInteger($this->linesCountBetweenMembers);
114115

115-
if ($actualLinesCount === $requiredLinesCount) {
116+
if ($actualLinesCount === $this->linesCountBetweenMembers) {
116117
continue;
117118
}
118119

119-
$errorMessage = $requiredLinesCount === 1
120+
$errorMessage = $this->linesCountBetweenMembers === 1
120121
? 'Expected 1 blank line between class members, found %2$d.'
121122
: 'Expected %1$d blank lines between class members, found %2$d.';
122123

123124
$fix = $phpcsFile->addFixableError(
124-
sprintf($errorMessage, $requiredLinesCount, $actualLinesCount),
125+
sprintf($errorMessage, $this->linesCountBetweenMembers, $actualLinesCount),
125126
$memberPointer,
126127
self::CODE_INCORRECT_COUNT_OF_BLANK_LINES_BETWEEN_MEMBERS
127128
);
128129
if (!$fix) {
129130
continue;
130131
}
131132

132-
$newLines = str_repeat($phpcsFile->eolChar, $requiredLinesCount + ($hasCommentWithNewLineAfterPreviousMember ? 0 : 1));
133+
$newLines = str_repeat(
134+
$phpcsFile->eolChar,
135+
$this->linesCountBetweenMembers + ($hasCommentWithNewLineAfterPreviousMember ? 0 : 1)
136+
);
133137

134138
$phpcsFile->fixer->beginChangeset();
135139

SlevomatCodingStandard/Sniffs/Classes/EmptyLinesAroundClassBracesSniff.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function register(): array
4646
*/
4747
public function process(File $phpcsFile, $stackPointer): void
4848
{
49+
$this->linesCountAfterOpeningBrace = SniffSettingsHelper::normalizeInteger($this->linesCountAfterOpeningBrace);
50+
$this->linesCountBeforeClosingBrace = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeClosingBrace);
51+
4952
$this->processOpeningBrace($phpcsFile, $stackPointer);
5053
$this->processClosingBrace($phpcsFile, $stackPointer);
5154
}
@@ -58,14 +61,13 @@ private function processOpeningBrace(File $phpcsFile, int $stackPointer): void
5861
$openerToken = $tokens[$openerPointer];
5962
$nextPointerAfterOpeningBrace = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $openerPointer + 1);
6063
$nextTokenAfterOpeningBrace = $tokens[$nextPointerAfterOpeningBrace];
61-
$linesCountAfterOpeningBrace = SniffSettingsHelper::normalizeInteger($this->linesCountAfterOpeningBrace);
6264
$lines = $nextTokenAfterOpeningBrace['line'] - $openerToken['line'] - 1;
6365

64-
if ($lines === $linesCountAfterOpeningBrace) {
66+
if ($lines === $this->linesCountAfterOpeningBrace) {
6567
return;
6668
}
6769

68-
if ($linesCountAfterOpeningBrace === 1) {
70+
if ($this->linesCountAfterOpeningBrace === 1) {
6971
$fix = $phpcsFile->addFixableError(
7072
sprintf('There must be one empty line after %s opening brace.', $typeToken['content']),
7173
$openerPointer,
@@ -76,7 +78,7 @@ private function processOpeningBrace(File $phpcsFile, int $stackPointer): void
7678
} else {
7779
$fix = $phpcsFile->addFixableError(sprintf(
7880
'There must be exactly %d empty lines after %s opening brace.',
79-
$linesCountAfterOpeningBrace,
81+
$this->linesCountAfterOpeningBrace,
8082
$typeToken['content']
8183
), $openerPointer, self::CODE_INCORRECT_EMPTY_LINES_AFTER_OPENING_BRACE);
8284
}
@@ -87,12 +89,12 @@ private function processOpeningBrace(File $phpcsFile, int $stackPointer): void
8789

8890
$phpcsFile->fixer->beginChangeset();
8991

90-
if ($lines < $linesCountAfterOpeningBrace) {
91-
for ($i = $lines; $i < $linesCountAfterOpeningBrace; $i++) {
92+
if ($lines < $this->linesCountAfterOpeningBrace) {
93+
for ($i = $lines; $i < $this->linesCountAfterOpeningBrace; $i++) {
9294
$phpcsFile->fixer->addNewline($openerPointer);
9395
}
9496
} else {
95-
for ($i = $openerPointer + $linesCountAfterOpeningBrace + 2; $i < $nextPointerAfterOpeningBrace; $i++) {
97+
for ($i = $openerPointer + $this->linesCountAfterOpeningBrace + 2; $i < $nextPointerAfterOpeningBrace; $i++) {
9698
if ($tokens[$i]['content'] !== $phpcsFile->eolChar) {
9799
break;
98100
}
@@ -111,14 +113,13 @@ private function processClosingBrace(File $phpcsFile, int $stackPointer): void
111113
$closerToken = $tokens[$closerPointer];
112114
$previousPointerBeforeClosingBrace = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $closerPointer - 1);
113115
$previousTokenBeforeClosingBrace = $tokens[$previousPointerBeforeClosingBrace];
114-
$linesCountBeforeClosingBrace = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeClosingBrace);
115116
$lines = $closerToken['line'] - $previousTokenBeforeClosingBrace['line'] - 1;
116117

117-
if ($lines === $linesCountBeforeClosingBrace) {
118+
if ($lines === $this->linesCountBeforeClosingBrace) {
118119
return;
119120
}
120121

121-
if ($linesCountBeforeClosingBrace === 1) {
122+
if ($this->linesCountBeforeClosingBrace === 1) {
122123
$fix = $phpcsFile->addFixableError(
123124
sprintf('There must be one empty line before %s closing brace.', $typeToken['content']),
124125
$closerPointer,
@@ -129,7 +130,7 @@ private function processClosingBrace(File $phpcsFile, int $stackPointer): void
129130
} else {
130131
$fix = $phpcsFile->addFixableError(sprintf(
131132
'There must be exactly %d empty lines before %s closing brace.',
132-
$linesCountBeforeClosingBrace,
133+
$this->linesCountBeforeClosingBrace,
133134
$typeToken['content']
134135
), $closerPointer, self::CODE_INCORRECT_EMPTY_LINES_BEFORE_CLOSING_BRACE);
135136
}
@@ -140,12 +141,12 @@ private function processClosingBrace(File $phpcsFile, int $stackPointer): void
140141

141142
$phpcsFile->fixer->beginChangeset();
142143

143-
if ($lines < $linesCountBeforeClosingBrace) {
144-
for ($i = $lines; $i < $linesCountBeforeClosingBrace; $i++) {
144+
if ($lines < $this->linesCountBeforeClosingBrace) {
145+
for ($i = $lines; $i < $this->linesCountBeforeClosingBrace; $i++) {
145146
$phpcsFile->fixer->addNewlineBefore($closerPointer);
146147
}
147148
} else {
148-
for ($i = $previousPointerBeforeClosingBrace + $linesCountBeforeClosingBrace + 2; $i < $closerPointer; $i++) {
149+
for ($i = $previousPointerBeforeClosingBrace + $this->linesCountBeforeClosingBrace + 2; $i < $closerPointer; $i++) {
149150
$phpcsFile->fixer->replaceToken($i, '');
150151
}
151152
}

SlevomatCodingStandard/Sniffs/Classes/MethodSpacingSniff.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function register(): array
4242
*/
4343
public function process(File $phpcsFile, $methodPointer): void
4444
{
45+
$this->minLinesCount = SniffSettingsHelper::normalizeInteger($this->minLinesCount);
46+
$this->maxLinesCount = SniffSettingsHelper::normalizeInteger($this->maxLinesCount);
47+
4548
if (!FunctionHelper::isMethod($phpcsFile, $methodPointer)) {
4649
return;
4750
}
@@ -78,23 +81,21 @@ public function process(File $phpcsFile, $methodPointer): void
7881
$linesBetween = $tokens[$nextMethodFirstLinePointer]['line'] !== $tokens[$methodEndPointer]['line']
7982
? $tokens[$nextMethodFirstLinePointer]['line'] - $tokens[$methodEndPointer]['line'] - 1
8083
: null;
81-
$minExpectedLines = SniffSettingsHelper::normalizeInteger($this->minLinesCount);
82-
$maxExpectedLines = SniffSettingsHelper::normalizeInteger($this->maxLinesCount);
8384

84-
if ($linesBetween !== null && $linesBetween >= $minExpectedLines && $linesBetween <= $maxExpectedLines) {
85+
if ($linesBetween !== null && $linesBetween >= $this->minLinesCount && $linesBetween <= $this->maxLinesCount) {
8586
return;
8687
}
8788

88-
if ($minExpectedLines === $maxExpectedLines) {
89-
$errorMessage = $minExpectedLines === 1
89+
if ($this->minLinesCount === $this->maxLinesCount) {
90+
$errorMessage = $this->minLinesCount === 1
9091
? 'Expected 1 blank line after method, found %3$d.'
9192
: 'Expected %2$d blank lines after method, found %3$d.';
9293
} else {
9394
$errorMessage = 'Expected %1$d to %2$d blank lines after method, found %3$d.';
9495
}
9596

9697
$fix = $phpcsFile->addFixableError(
97-
sprintf($errorMessage, $minExpectedLines, $maxExpectedLines, $linesBetween ?? 0),
98+
sprintf($errorMessage, $this->minLinesCount, $this->maxLinesCount, $linesBetween ?? 0),
9899
$methodPointer,
99100
self::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS
100101
);
@@ -108,7 +109,7 @@ public function process(File $phpcsFile, $methodPointer): void
108109
if ($linesBetween === null) {
109110
$phpcsFile->fixer->addContent(
110111
$methodEndPointer,
111-
$phpcsFile->eolChar . str_repeat($phpcsFile->eolChar, $minExpectedLines) . IndentationHelper::getIndentation(
112+
$phpcsFile->eolChar . str_repeat($phpcsFile->eolChar, $this->minLinesCount) . IndentationHelper::getIndentation(
112113
$phpcsFile,
113114
TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $methodPointer)
114115
)
@@ -118,14 +119,14 @@ public function process(File $phpcsFile, $methodPointer): void
118119
$phpcsFile->fixer->replaceToken($i, '');
119120
}
120121

121-
} elseif ($linesBetween > $maxExpectedLines) {
122-
$phpcsFile->fixer->addContent($methodEndPointer, str_repeat($phpcsFile->eolChar, $maxExpectedLines + 1));
122+
} elseif ($linesBetween > $this->maxLinesCount) {
123+
$phpcsFile->fixer->addContent($methodEndPointer, str_repeat($phpcsFile->eolChar, $this->maxLinesCount + 1));
123124

124125
for ($i = $methodEndPointer + 1; $i < TokenHelper::findFirstTokenOnLine($phpcsFile, $nextMethodFirstLinePointer); $i++) {
125126
$phpcsFile->fixer->replaceToken($i, '');
126127
}
127128
} else {
128-
$phpcsFile->fixer->addContent($methodEndPointer, str_repeat($phpcsFile->eolChar, $minExpectedLines - $linesBetween));
129+
$phpcsFile->fixer->addContent($methodEndPointer, str_repeat($phpcsFile->eolChar, $this->minLinesCount - $linesBetween));
129130
}
130131

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

SlevomatCodingStandard/Sniffs/Classes/ParentCallSpacingSniff.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class ParentCallSpacingSniff extends AbstractControlStructureSpacing
4242
*/
4343
public function process(File $phpcsFile, $parentPointer): void
4444
{
45+
$this->linesCountBefore = SniffSettingsHelper::normalizeInteger($this->linesCountBefore);
46+
$this->linesCountBeforeFirst = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirst);
47+
$this->linesCountAfter = SniffSettingsHelper::normalizeInteger($this->linesCountAfter);
48+
$this->linesCountAfterLast = SniffSettingsHelper::normalizeInteger($this->linesCountAfterLast);
49+
4550
$tokens = $phpcsFile->getTokens();
4651

4752
if (array_key_exists('nested_parenthesis', $tokens[$parentPointer])) {
@@ -89,28 +94,28 @@ protected function getKeywordsToCheck(): array
8994

9095
protected function getLinesCountBefore(): int
9196
{
92-
return SniffSettingsHelper::normalizeInteger($this->linesCountBefore);
97+
return $this->linesCountBefore;
9398
}
9499

95100
/**
96101
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
97102
*/
98103
protected function getLinesCountBeforeFirst(File $phpcsFile, int $parentPointer): int
99104
{
100-
return SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirst);
105+
return $this->linesCountBeforeFirst;
101106
}
102107

103108
protected function getLinesCountAfter(): int
104109
{
105-
return SniffSettingsHelper::normalizeInteger($this->linesCountAfter);
110+
return $this->linesCountAfter;
106111
}
107112

108113
/**
109114
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
110115
*/
111116
protected function getLinesCountAfterLast(File $phpcsFile, int $parentPointer, int $parentEndPointer): int
112117
{
113-
return SniffSettingsHelper::normalizeInteger($this->linesCountAfterLast);
118+
return $this->linesCountAfterLast;
114119
}
115120

116121
}

SlevomatCodingStandard/Sniffs/Classes/RequireMultiLineMethodSignatureSniff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class RequireMultiLineMethodSignatureSniff extends AbstractMethodSignature
4141
*/
4242
public function process(File $phpcsFile, $methodPointer): void
4343
{
44+
$this->minLineLength = SniffSettingsHelper::normalizeInteger($this->minLineLength);
45+
4446
if (!FunctionHelper::isMethod($phpcsFile, $methodPointer)) {
4547
return;
4648
}
@@ -76,8 +78,7 @@ public function process(File $phpcsFile, $methodPointer): void
7678
return;
7779
}
7880

79-
$minLineLength = SniffSettingsHelper::normalizeInteger($this->minLineLength);
80-
if ($minLineLength !== 0 && strlen($signatureWithoutTabIndentation) < $minLineLength) {
81+
if ($this->minLineLength !== 0 && strlen($signatureWithoutTabIndentation) < $this->minLineLength) {
8182
return;
8283
}
8384

0 commit comments

Comments
 (0)