Skip to content

Commit 371d67b

Browse files
committed
style: auto formatting
1 parent 479d914 commit 371d67b

File tree

5 files changed

+64
-74
lines changed

5 files changed

+64
-74
lines changed

yCodeTech/Sniffs/Commenting/DocblockFormatSniff.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public function register()
4141
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
4242
* @param int $stackPtr The position of the current token in the
4343
* stack passed in $tokens.
44-
*
45-
* @return void
4644
*/
4745
public function process(File $phpcsFile, $stackPtr)
4846
{
@@ -63,13 +61,11 @@ public function process(File $phpcsFile, $stackPtr)
6361
*
6462
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
6563
* @param int $stackPtr The position of the @return token.
66-
*
67-
* @return void
6864
*/
6965
private function checkReturnEmptyLine(File $phpcsFile, $stackPtr)
7066
{
7167
$tokens = $phpcsFile->getTokens();
72-
68+
7369
// First, check if there are any other @ tags before this @return
7470
$hasOtherTags = false;
7571
$docBlockStart = $phpcsFile->findPrevious([T_DOC_COMMENT_OPEN_TAG], $stackPtr);
@@ -81,12 +77,12 @@ private function checkReturnEmptyLine(File $phpcsFile, $stackPtr)
8177
}
8278
}
8379
}
84-
80+
8581
// If there are no other tags before @return, don't require empty line
8682
if (!$hasOtherTags) {
8783
return;
8884
}
89-
85+
9086
// Find the previous non-whitespace token
9187
$prev = $phpcsFile->findPrevious([T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR], ($stackPtr - 1), null, true);
9288
if ($prev === false) {
@@ -134,7 +130,7 @@ private function checkReturnEmptyLine(File $phpcsFile, $stackPtr)
134130
break;
135131
}
136132
}
137-
133+
138134
$starIndent = str_repeat(' ', $starColumn - 1);
139135
$newContent = $phpcsFile->eolChar . $starIndent . '*' . $phpcsFile->eolChar . $starIndent . '* ';
140136
$phpcsFile->fixer->addContent($prev, $newContent);
@@ -148,8 +144,6 @@ private function checkReturnEmptyLine(File $phpcsFile, $stackPtr)
148144
*
149145
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
150146
* @param int $stackPtr The position of the @ tag token.
151-
*
152-
* @return void
153147
*/
154148
private function checkTagSpacing(File $phpcsFile, $stackPtr)
155149
{
@@ -198,10 +192,10 @@ private function checkTagSpacing(File $phpcsFile, $stackPtr)
198192

199193
// Replace all spaces that are 2 or more consecutive spaces, with a single space
200194
$normalizedContent = preg_replace('/( ){2,}/', ' ', $content);
201-
195+
202196
// Fix missing space between type and variable
203197
$normalizedContent = preg_replace('/^([^\s]+)(\$\w+)/', '$1 $2', $normalizedContent);
204-
198+
205199
// Check if content needs normalization
206200
if ($content !== $normalizedContent) {
207201
$needsFixing = true;
@@ -212,15 +206,15 @@ private function checkTagSpacing(File $phpcsFile, $stackPtr)
212206
$fix = $phpcsFile->addFixableError($error, $contentToken, 'TagSpacing');
213207
if ($fix === true) {
214208
$phpcsFile->fixer->beginChangeset();
215-
209+
216210
// If there was a whitespace token, replace it and normalize content
217211
if ($tokens[$next]['code'] === T_DOC_COMMENT_WHITESPACE) {
218212
$phpcsFile->fixer->replaceToken($next, ' ');
219213
$phpcsFile->fixer->replaceToken($contentToken, $normalizedContent);
220214
}
221-
215+
222216
$phpcsFile->fixer->endChangeset();
223217
}
224218
}
225219
}
226-
}
220+
}

yCodeTech/Sniffs/Types/DisallowTypeLongNamesSniff.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class DisallowTypeLongNamesSniff implements Sniff
4848

4949
/**
5050
* Returns an array of tokens this test wants to listen for.
51-
*
51+
*
5252
* Once PHP_CodeSniffer encounters one of these tokens, it calls the process method.
5353
*
5454
* @return array<int>
@@ -70,8 +70,6 @@ public function register()
7070
*
7171
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
7272
* @param int $stackPtr The position of the current token in the stack.
73-
*
74-
* @return void
7573
*/
7674
public function process(File $phpcsFile, $stackPtr)
7775
{
@@ -107,8 +105,6 @@ public function process(File $phpcsFile, $stackPtr)
107105
*
108106
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
109107
* @param int $stackPtr The position of the current token in the stack.
110-
*
111-
* @return void
112108
*/
113109
private function processDocblockTag(File $phpcsFile, $stackPtr)
114110
{
@@ -156,8 +152,6 @@ private function processDocblockTag(File $phpcsFile, $stackPtr)
156152
*
157153
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
158154
* @param int $stackPtr The position of the current token in the stack.
159-
*
160-
* @return void
161155
*/
162156
private function processFunctionTypes(File $phpcsFile, $stackPtr)
163157
{
@@ -212,8 +206,6 @@ private function processFunctionTypes(File $phpcsFile, $stackPtr)
212206
*
213207
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
214208
* @param int $stackPtr The position of the current token in the stack.
215-
*
216-
* @return void
217209
*/
218210
private function processPropertyType(File $phpcsFile, $stackPtr)
219211
{
@@ -222,7 +214,7 @@ private function processPropertyType(File $phpcsFile, $stackPtr)
222214
// Look backwards to find the visibility keyword.
223215
$visibilityTokens = [T_PUBLIC, T_PRIVATE, T_PROTECTED, T_STATIC, T_VAR];
224216
$visibilityPtr = $phpcsFile->findPrevious($visibilityTokens, $stackPtr - 1);
225-
217+
226218
if ($visibilityPtr === false) {
227219
return;
228220
}
@@ -251,8 +243,7 @@ private function processPropertyType(File $phpcsFile, $stackPtr)
251243
* @param int $stackPtr The position of the type token.
252244
* @param string $context The context (parameter, return, property).
253245
* @param array $data Data to format the error message with.
254-
*
255-
* @return void
246+
* @param string|null $docblockTagName The docblock tag name if applicable.
256247
*/
257248
private function checkTypeToken(File $phpcsFile, $stackPtr, $context, $docblockTagName = null)
258249
{
@@ -261,7 +252,7 @@ private function checkTypeToken(File $phpcsFile, $stackPtr, $context, $docblockT
261252
if (isset($this->processedTokens[$filename][$stackPtr])) {
262253
return;
263254
}
264-
255+
265256
// Mark this token as processed
266257
$this->processedTokens[$filename][$stackPtr] = true;
267258

@@ -276,14 +267,14 @@ private function checkTypeToken(File $phpcsFile, $stackPtr, $context, $docblockT
276267
foreach ($extractedTypes as $extractedType) {
277268
// Get the short type name from the mapping.
278269
$shortType = $this->typeNames[$extractedType];
279-
270+
280271
// This pattern matches for both simple types and types within generic type syntax,
281272
// using word boundaries to avoid partial matches.
282273
$pattern = '/\b' . preg_quote($extractedType, '/') . '\b/i';
283274

284275
// Replace long name type with short name in the entire content.
285276
$replacedContent = preg_replace($pattern, $shortType, $replacedContent);
286-
277+
287278
// Double-check that replacement actually happened, by comparing the original
288279
// content with the replaced content.
289280
// If replacement occurred, we will report an error.
@@ -311,8 +302,6 @@ private function checkTypeToken(File $phpcsFile, $stackPtr, $context, $docblockT
311302
* @param string $context The context (parameter, return, property, etc.).
312303
* @param string|null $docblockTagName The docblock tag name if applicable.
313304
* @param string $replacedContent The replacement content.
314-
*
315-
* @return void
316305
*/
317306
private function reportAndFixError(
318307
File $phpcsFile,
@@ -425,4 +414,4 @@ private function extractTypesFromContent($content)
425414
// Return all found long-form types
426415
return array_unique($foundTypes);
427416
}
428-
}
417+
}

yCodeTech/Tests/Commenting/DocblockFormatUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ public function getWarningList()
5353
{
5454
return [];
5555
}
56-
}
56+
}

yCodeTech/Tests/README.md

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ composer test-all
3636
### Option 2: Manual Testing
3737

3838
Test individual sniffs:
39+
3940
```bash
4041
# Test DisallowTypeLongNamesSniff
4142
vendor/bin/phpcs --standard=./yCodeTech/ruleset.xml yCodeTech/Tests/Types/DisallowTypeLongNamesUnitTest.inc
@@ -50,6 +51,7 @@ vendor/bin/phpcs --standard=./yCodeTech/ruleset.xml yCodeTech/Tests/Commenting/F
5051
### Option 3: Auto-fix Testing
5152

5253
Test that violations can be automatically fixed:
54+
5355
```bash
5456
# Copy test file and run auto-fix
5557
cp yCodeTech/Tests/Types/DisallowTypeLongNamesUnitTest.inc temp_test.php
@@ -60,54 +62,59 @@ rm temp_test.php
6062
## Test Coverage
6163

6264
### DisallowTypeLongNamesSniff Tests
63-
-`boolean``bool` conversion in @param tags
64-
-`integer``int` conversion in @param tags
65-
-`boolean``bool` conversion in @return tags
66-
-`integer``int` conversion in @return tags
67-
-`boolean``bool` conversion in @var tags
68-
-`integer``int` conversion in @var tags
69-
-`boolean``bool` conversion in @property tags
70-
-`integer``int` conversion in @property tags
71-
- ✅ Static analysis tags (@phpstan-*, @psalm-*)
72-
- ✅ Union types (boolean|string → bool|string)
73-
- ✅ Function parameter type declarations
74-
- ✅ Function return type declarations
75-
- ✅ Class property type declarations
76-
- ✅ Type casting (boolean) → (bool), (integer) → (int)
77-
- ✅ Nullable types (?boolean → ?bool)
78-
- ✅ Context-aware detection (no duplicates)
65+
66+
-`boolean``bool` conversion in @param tags
67+
-`integer``int` conversion in @param tags
68+
-`boolean``bool` conversion in @return tags
69+
-`integer``int` conversion in @return tags
70+
-`boolean``bool` conversion in @var tags
71+
-`integer``int` conversion in @var tags
72+
-`boolean``bool` conversion in @property tags
73+
-`integer``int` conversion in @property tags
74+
- ✅ Static analysis tags (@phpstan-\*, @psalm-\*)
75+
- ✅ Union types (boolean|string → bool|string)
76+
- ✅ Function parameter type declarations
77+
- ✅ Function return type declarations
78+
- ✅ Class property type declarations
79+
- ✅ Type casting (boolean) → (bool), (integer) → (int)
80+
- ✅ Nullable types (?boolean → ?bool)
81+
- ✅ Context-aware detection (no duplicates)
7982

8083
### DocblockFormatSniff Tests
81-
- ✅ Exactly 1 space between @param elements
82-
- ✅ Zero space detection and fixing
83-
- ✅ Multiple space detection and fixing
84-
- ✅ Empty line before @return tag
85-
- ✅ All @ tag types (param, return, var, throws, see, etc.)
86-
- ✅ Static analysis tag spacing
84+
85+
- ✅ Exactly 1 space between @param elements
86+
- ✅ Zero space detection and fixing
87+
- ✅ Multiple space detection and fixing
88+
- ✅ Empty line before @return tag
89+
- ✅ All @ tag types (param, return, var, throws, see, etc.)
90+
- ✅ Static analysis tag spacing
8791

8892
### FunctionCommentSniff Tests
89-
- ✅ Missing @return tag detection
90-
- ✅ Auto-insertion of @return mixed
91-
- ✅ Void function detection (explicit void)
92-
- ✅ Implicit void function detection (echo-only)
93-
- ✅ Empty return statement detection
94-
- ✅ Correct indentation for auto-inserted tags
93+
94+
- ✅ Missing @return tag detection
95+
- ✅ Auto-insertion of @return mixed
96+
- ✅ Void function detection (explicit void)
97+
- ✅ Implicit void function detection (echo-only)
98+
- ✅ Empty return statement detection
99+
- ✅ Correct indentation for auto-inserted tags
95100

96101
## Expected Results
97102

98103
Each test file contains deliberate violations that should be detected by the corresponding sniff. The test classes define the exact line numbers where errors should occur.
99104

100105
When running the tests, you should see:
101-
- **All violations detected** on the expected lines
102-
- **Auto-fixing capabilities** working correctly
103-
- **No false positives** on correct code
106+
107+
- **All violations detected** on the expected lines
108+
- **Auto-fixing capabilities** working correctly
109+
- **No false positives** on correct code
104110

105111
## Integration Testing
106112

107113
The `StandardIntegrationTest` provides comprehensive testing of all sniffs working together, ensuring:
108-
- No conflicts between sniffs
109-
- Proper violation detection across all rule types
110-
- Consistent behavior of the entire standard
114+
115+
- No conflicts between sniffs
116+
- Proper violation detection across all rule types
117+
- Consistent behaviour of the entire standard
111118

112119
## Adding New Tests
113120

yCodeTech/Tests/Types/DisallowTypeLongNamesUnitTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* Unit test class for DisallowTypeLongNamesSniff
9-
*
9+
*
1010
* The code to test are split into several .inc files to cover different scenarios.
1111
* As documented here: https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/4.x/.github/CONTRIBUTING.md#multiple-test-case-files
1212
*/
@@ -33,13 +33,13 @@ public function getErrorList($testFile = "")
3333
12 => 1, // @param boolean
3434
13 => 1, // @param integer
3535
15 => 1, // @return boolean
36-
36+
3737
// Class docblock errors
3838
25 => 1, // @property integer
3939
26 => 1, // @property-read integer
4040
27 => 1, // @property-write boolean
4141
28 => 1, // @method boolean
42-
42+
4343
// Property and method docblock errors
4444
35 => 1, // @var boolean
4545
42 => 1, // @param boolean
@@ -60,7 +60,7 @@ public function getErrorList($testFile = "")
6060
// Global type casting errors
6161
10 => 1, // (boolean) cast
6262
11 => 1, // (integer) cast
63-
63+
6464
// Function type casting errors
6565
18 => 1, // (boolean) cast
6666
19 => 1, // (integer) cast
@@ -79,7 +79,7 @@ public function getErrorList($testFile = "")
7979
13 => 1, // @param array<string, integer>
8080
14 => 2, // @param Collection<boolean, integer>
8181
16 => 2, // @return Map<integer, boolean>
82-
82+
8383
// Complex generic errors
8484
26 => 2, // @param Generator<integer, boolean>
8585
27 => 1, // @param Promise<boolean>
@@ -103,4 +103,4 @@ public function getWarningList()
103103
{
104104
return [];
105105
}
106-
}
106+
}

0 commit comments

Comments
 (0)