Skip to content

Commit 8ad16f6

Browse files
committed
SlevomatCodingStandard.Classes.PropertyDeclaration: Improved error messages
1 parent ff4cafd commit 8ad16f6

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

SlevomatCodingStandard/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ private function checkModifiersOrder(File $phpcsFile, int $propertyPointer, int
166166
$expectedModifiersFormatted = implode(' ', $expectedModifiers);
167167

168168
$fix = $phpcsFile->addFixableError(
169-
sprintf('Incorrect order of property modifiers "%s", expected "%s".', $actualModifiersFormatted, $expectedModifiersFormatted),
169+
sprintf(
170+
'Incorrect order of modifiers "%s" of property %s, expected "%s".',
171+
$actualModifiersFormatted,
172+
$tokens[$propertyPointer]['content'],
173+
$expectedModifiersFormatted
174+
),
170175
$firstModifierPointer,
171176
self::CODE_INCORRECT_ORDER_OF_MODIFIERS
172177
);
@@ -204,8 +209,10 @@ private function checkTypeHintSpacing(File $phpcsFile, int $propertyPointer, int
204209
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $typeHintStartPointer - 1, $lastModifierPointer);
205210
$nullabilitySymbolPointer = $previousPointer !== null && $tokens[$previousPointer]['code'] === T_NULLABLE ? $previousPointer : null;
206211

212+
$propertyName = $tokens[$propertyPointer]['content'];
213+
207214
if ($tokens[$lastModifierPointer + 1]['code'] !== T_WHITESPACE) {
208-
$errorMessage = 'There must be exactly one space before type hint nullability symbol.';
215+
$errorMessage = sprintf('There must be exactly one space before type hint nullability symbol of property %s.', $propertyName);
209216
$errorCode = self::CODE_NO_SPACE_BEFORE_NULLABILITY_SYMBOL;
210217

211218
$fix = $phpcsFile->addFixableError($errorMessage, $typeHintEndPointer, $errorCode);
@@ -216,10 +223,13 @@ private function checkTypeHintSpacing(File $phpcsFile, int $propertyPointer, int
216223
}
217224
} elseif ($tokens[$lastModifierPointer + 1]['content'] !== ' ') {
218225
if ($nullabilitySymbolPointer !== null) {
219-
$errorMessage = 'There must be exactly one space before type hint nullability symbol.';
226+
$errorMessage = sprintf(
227+
'There must be exactly one space before type hint nullability symbol of property %s.',
228+
$propertyName
229+
);
220230
$errorCode = self::CODE_MULTIPLE_SPACES_BEFORE_NULLABILITY_SYMBOL;
221231
} else {
222-
$errorMessage = 'There must be exactly one space before type hint.';
232+
$errorMessage = sprintf('There must be exactly one space before type hint of property %s.', $propertyName);
223233
$errorCode = self::CODE_MULTIPLE_SPACES_BEFORE_TYPE_HINT;
224234
}
225235

@@ -233,7 +243,7 @@ private function checkTypeHintSpacing(File $phpcsFile, int $propertyPointer, int
233243

234244
if ($tokens[$typeHintEndPointer + 1]['code'] !== T_WHITESPACE) {
235245
$fix = $phpcsFile->addFixableError(
236-
'There must be exactly one space between type hint and property.',
246+
sprintf('There must be exactly one space between type hint and property %s.', $propertyName),
237247
$typeHintEndPointer,
238248
self::CODE_NO_SPACE_BETWEEN_TYPE_HINT_AND_PROPERTY
239249
);
@@ -244,7 +254,7 @@ private function checkTypeHintSpacing(File $phpcsFile, int $propertyPointer, int
244254
}
245255
} elseif ($tokens[$typeHintEndPointer + 1]['content'] !== ' ') {
246256
$fix = $phpcsFile->addFixableError(
247-
'There must be exactly one space between type hint and property.',
257+
sprintf('There must be exactly one space between type hint and property %s.', $propertyName),
248258
$typeHintEndPointer,
249259
self::CODE_MULTIPLE_SPACES_BETWEEN_TYPE_HINT_AND_PROPERTY
250260
);
@@ -264,7 +274,7 @@ private function checkTypeHintSpacing(File $phpcsFile, int $propertyPointer, int
264274
}
265275

266276
$fix = $phpcsFile->addFixableError(
267-
'There must be no whitespace between type hint nullability symbol and type hint.',
277+
sprintf('There must be no whitespace between type hint nullability symbol and type hint of property %s.', $propertyName),
268278
$typeHintStartPointer,
269279
self::CODE_WHITESPACE_AFTER_NULLABILITY_SYMBOL
270280
);

tests/Sniffs/Classes/PropertyDeclarationSniffTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,20 @@ public function testPromotedErrors(): void
5757

5858
self::assertSame(7, $report->getErrorCount());
5959

60-
self::assertSniffError($report, 6, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BEFORE_TYPE_HINT);
60+
self::assertSniffError(
61+
$report,
62+
6,
63+
PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BEFORE_TYPE_HINT,
64+
'There must be exactly one space before type hint of property $promotion1.'
65+
);
6166
self::assertSniffError($report, 6, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BETWEEN_TYPE_HINT_AND_PROPERTY);
6267
self::assertSniffError($report, 6, PropertyDeclarationSniff::CODE_INCORRECT_ORDER_OF_MODIFIERS);
68+
self::assertSniffError(
69+
$report,
70+
6,
71+
PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BEFORE_TYPE_HINT,
72+
'There must be exactly one space before type hint of property $promotion2.'
73+
);
6374
self::assertSniffError($report, 16, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BEFORE_TYPE_HINT);
6475
self::assertSniffError($report, 16, PropertyDeclarationSniff::CODE_MULTIPLE_SPACES_BETWEEN_TYPE_HINT_AND_PROPERTY);
6576
self::assertSniffError($report, 17, PropertyDeclarationSniff::CODE_INCORRECT_ORDER_OF_MODIFIERS);

0 commit comments

Comments
 (0)