Skip to content

Commit e4bec06

Browse files
✨ Multiple improvements
1 parent 1cb05b2 commit e4bec06

13 files changed

+119
-168
lines changed

SymfonyCustom/Sniffs/Commenting/ClassCommentSniff.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,57 +26,46 @@ class ClassCommentSniff extends PEARClassCommentSniff
2626
'category' => [
2727
'required' => false,
2828
'allow_multiple' => false,
29-
'order_text' => 'precedes @package',
3029
],
3130
'package' => [
3231
'required' => false,
3332
'allow_multiple' => false,
34-
'order_text' => 'follows @category',
3533
],
3634
'subpackage' => [
3735
'required' => false,
3836
'allow_multiple' => false,
39-
'order_text' => 'follows @package',
4037
],
4138
'author' => [
4239
'required' => false,
4340
'allow_multiple' => true,
44-
'order_text' => 'follows @subpackage (if used) or @package',
4541
],
4642
'copyright' => [
4743
'required' => false,
4844
'allow_multiple' => true,
49-
'order_text' => 'follows @author',
5045
],
5146
'license' => [
5247
'required' => false,
5348
'allow_multiple' => false,
54-
'order_text' => 'follows @copyright (if used) or @author',
5549
],
5650
'version' => [
5751
'required' => false,
5852
'allow_multiple' => false,
59-
'order_text' => 'follows @license',
6053
],
6154
'link' => [
6255
'required' => false,
6356
'allow_multiple' => true,
64-
'order_text' => 'follows @version',
6557
],
6658
'see' => [
6759
'required' => false,
6860
'allow_multiple' => true,
69-
'order_text' => 'follows @link',
7061
],
7162
'since' => [
7263
'required' => false,
7364
'allow_multiple' => false,
74-
'order_text' => 'follows @see (if used) or @link',
7565
],
7666
'deprecated' => [
7767
'required' => false,
7868
'allow_multiple' => false,
79-
'order_text' => 'follows @since (if used) or @see (if used) or @link',
8069
],
8170
];
8271
}

SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 57 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,114 +29,94 @@ public function process(File $phpcsFile, $stackPtr): void
2929
{
3030
$tokens = $phpcsFile->getTokens();
3131

32-
$previousType = '';
32+
$typeSeen = [];
33+
$previousTag = false;
34+
$previousIsCustom = false;
35+
3336
foreach ($tokens[$stackPtr]['comment_tags'] as $commentTag) {
3437
$currentType = $tokens[$commentTag]['content'];
35-
$commentTagLine = $tokens[$commentTag]['line'];
36-
37-
$previousString = $phpcsFile->findPrevious(
38-
T_DOC_COMMENT_STRING,
39-
$commentTag,
40-
$stackPtr
41-
);
38+
$currentIsCustom = !in_array($currentType, SniffHelper::TAGS);
39+
$isNewType = !in_array($currentType, $typeSeen);
4240

43-
$previousTag = $phpcsFile->findPrevious(
44-
T_DOC_COMMENT_TAG,
45-
$commentTag - 1,
46-
$stackPtr
47-
);
41+
$commentTagLine = $tokens[$commentTag]['line'];
4842

43+
$previousString = $phpcsFile->findPrevious(T_DOC_COMMENT_STRING, $commentTag, $stackPtr);
4944
$previousLine = -1;
45+
5046
if (false !== $previousString) {
5147
$previousLine = $tokens[$previousString]['line'];
5248
$previousElement = $previousString;
5349
}
5450

5551
if (false !== $previousTag) {
52+
$previousType = $tokens[$previousTag]['content'];
5653
$previousTagLine = $tokens[$previousTag]['line'];
5754

5855
if ($previousTagLine > $previousLine) {
5956
$previousLine = $previousTagLine;
6057
$previousElement = $previousTag;
6158
}
59+
} else {
60+
$previousType = null;
6261
}
6362

6463
if (isset($previousElement) && $previousLine >= 0) {
65-
$currentIsCustom = !in_array($currentType, SniffHelper::TAGS);
66-
$previousIsCustom = '' !== $previousType
67-
&& !in_array($previousType, SniffHelper::TAGS);
68-
69-
if (($previousType === $currentType) || ($currentIsCustom && $previousIsCustom)) {
64+
if ($previousType === $currentType) {
7065
if ($previousLine !== $commentTagLine - 1) {
71-
if ($previousType === $currentType) {
72-
$fix = $phpcsFile->addFixableError(
73-
'Expected no empty lines between annotations of the same type',
74-
$commentTag,
75-
'SameType'
76-
);
77-
} else {
78-
$fix = $phpcsFile->addFixableError(
79-
'Expected no empty lines between custom annotations',
80-
$commentTag,
81-
'CustomType'
82-
);
83-
}
66+
$fix = $phpcsFile->addFixableError(
67+
'Expected no empty lines between annotations of the same type',
68+
$commentTag,
69+
'SameType'
70+
);
8471

8572
if ($fix) {
86-
$phpcsFile->fixer->beginChangeset();
87-
$this->removeLines(
88-
$phpcsFile,
89-
$previousElement,
90-
$previousLine + 1,
91-
$commentTagLine - 1
92-
);
93-
$phpcsFile->fixer->endChangeset();
73+
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
9474
}
9575
}
96-
} else {
97-
if ($previousLine !== $commentTagLine - 2) {
76+
} elseif ($currentIsCustom && $previousIsCustom) {
77+
if ($previousLine !== $commentTagLine - 1) {
9878
$fix = $phpcsFile->addFixableError(
99-
'Expected exactly one empty line between annotations of different types',
79+
'Expected no empty lines between custom annotations',
10080
$commentTag,
101-
'DifferentType'
81+
'CustomType'
10282
);
10383

10484
if ($fix) {
105-
$phpcsFile->fixer->beginChangeset();
106-
107-
if ($previousLine === $commentTagLine - 1) {
108-
$firstOnLine = $phpcsFile->findFirstOnLine(
109-
[],
110-
$commentTag,
111-
true
112-
);
113-
$star = $phpcsFile->findNext(
114-
T_DOC_COMMENT_STAR,
115-
$firstOnLine
116-
);
117-
$content = $phpcsFile->getTokensAsString(
118-
$firstOnLine,
119-
$star - $firstOnLine + 1
120-
);
121-
$phpcsFile->fixer->addContentBefore(
122-
$firstOnLine,
123-
$content.$phpcsFile->eolChar
124-
);
125-
} else {
126-
$this->removeLines(
127-
$phpcsFile,
128-
$previousElement,
129-
$previousLine + 2,
130-
$commentTagLine - 1
131-
);
132-
}
133-
$phpcsFile->fixer->endChangeset();
85+
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
86+
}
87+
}
88+
} elseif (!$currentIsCustom && !$isNewType) {
89+
$phpcsFile->addError(
90+
'Annotation of the same type should be together',
91+
$commentTag,
92+
'GroupSameType'
93+
);
94+
} elseif ($previousLine !== $commentTagLine - 2) {
95+
$fix = $phpcsFile->addFixableError(
96+
'Expected exactly one empty line between annotations of different types',
97+
$commentTag,
98+
'DifferentType'
99+
);
100+
101+
if ($fix) {
102+
if ($previousLine === $commentTagLine - 1) {
103+
$firstOnLine = $phpcsFile->findFirstOnLine([], $commentTag, true);
104+
$star = $phpcsFile->findNext(T_DOC_COMMENT_STAR, $firstOnLine);
105+
$content = $phpcsFile->getTokensAsString($firstOnLine, $star - $firstOnLine + 1);
106+
107+
$phpcsFile->fixer->addContentBefore($firstOnLine, $content.$phpcsFile->eolChar);
108+
} else {
109+
$this->removeLines($phpcsFile, $previousElement, $previousLine + 2, $commentTagLine - 1);
134110
}
135111
}
136112
}
137113
}
138114

139-
$previousType = $currentType;
115+
$previousTag = $commentTag;
116+
$previousIsCustom = $currentIsCustom;
117+
if (!$currentIsCustom && $isNewType) {
118+
$typeSeen[] = $currentType;
119+
}
140120
}
141121
}
142122

@@ -150,6 +130,8 @@ private function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $
150130
{
151131
$tokens = $phpcsFile->getTokens();
152132

133+
$phpcsFile->fixer->beginChangeset();
134+
153135
for ($i = $fromPtr;; $i++) {
154136
if ($tokens[$i]['line'] > $toLine) {
155137
break;
@@ -159,5 +141,7 @@ private function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $
159141
$phpcsFile->fixer->replaceToken($i, '');
160142
}
161143
}
144+
145+
$phpcsFile->fixer->endChangeset();
162146
}
163147
}

SymfonyCustom/Sniffs/Errors/UserDeprecatedSniff.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,32 @@ public function process(File $phpcsFile, $stackPtr): void
3838

3939
do {
4040
$string = $phpcsFile->findNext(T_STRING, $opener, $closer);
41-
4241
if (false === $string) {
4342
break;
4443
}
4544

46-
if ('E_USER_DEPRECATED' === $tokens[$string]['content'] && '@' !== $tokens[$stackPtr - 1]['content']) {
47-
$phpcsFile->addError(
48-
'Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator',
49-
$stackPtr,
50-
'Invalid'
51-
);
45+
$opener = $string + 1;
5246

53-
break;
54-
} else {
55-
$opener = $string + 1;
47+
if ('E_USER_DEPRECATED' !== $tokens[$string]['content']) {
48+
continue;
49+
}
50+
51+
if ('@' === $tokens[$stackPtr - 1]['content']) {
52+
continue;
5653
}
54+
55+
if ('@' === $tokens[$stackPtr - 2]['content']
56+
&& 'T_NS_SEPARATOR' === $tokens[$stackPtr - 1]['type']
57+
) {
58+
continue;
59+
}
60+
61+
$phpcsFile->addError(
62+
'Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator',
63+
$stackPtr,
64+
'Invalid'
65+
);
66+
break;
5767
} while ($opener < $closer);
5868
}
5969
}

SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class UnaryOperatorSpacingSniff implements Sniff
1717
*/
1818
public function register(): array
1919
{
20-
return [T_MINUS, T_PLUS, T_BOOLEAN_NOT];
20+
return [T_MINUS, T_PLUS];
2121
}
2222

2323
/**
@@ -28,21 +28,6 @@ public function process(File $phpcsFile, $stackPtr): void
2828
{
2929
$tokens = $phpcsFile->getTokens();
3030

31-
// Check "!" operator.
32-
if (T_BOOLEAN_NOT === $tokens[$stackPtr]['code'] && T_WHITESPACE === $tokens[$stackPtr + 1]['code']) {
33-
$fix = $phpcsFile->addFixableError(
34-
'A unary operator statement must not be followed by a space',
35-
$stackPtr,
36-
'BooleanNot'
37-
);
38-
39-
if ($fix) {
40-
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
41-
}
42-
43-
return;
44-
}
45-
4631
// Find the last syntax item to determine if this is an unary operator.
4732
$lastSyntaxItem = $phpcsFile->findPrevious(
4833
T_WHITESPACE,
@@ -66,17 +51,15 @@ public function process(File $phpcsFile, $stackPtr): void
6651
);
6752

6853
// Check plus / minus value assignments or comparisons.
69-
if (T_MINUS === $tokens[$stackPtr]['code'] || T_PLUS === $tokens[$stackPtr]['code']) {
70-
if (!$operatorSuffixAllowed && T_WHITESPACE === $tokens[$stackPtr + 1]['code']) {
71-
$fix = $phpcsFile->addFixableError(
72-
'A unary operator statement must not be followed by a space',
73-
$stackPtr,
74-
'Invalid'
75-
);
54+
if (!$operatorSuffixAllowed && T_WHITESPACE === $tokens[$stackPtr + 1]['code']) {
55+
$fix = $phpcsFile->addFixableError(
56+
'A unary operator statement must not be followed by a space',
57+
$stackPtr,
58+
'Invalid'
59+
);
7660

77-
if ($fix) {
78-
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
79-
}
61+
if ($fix) {
62+
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
8063
}
8164
}
8265
}

SymfonyCustom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@
6666
* @Route("/{id}/")
6767
* @param
6868
*/
69+
70+
/**
71+
* @param string $a
72+
*
73+
* @return string
74+
*
75+
* @param string $b
76+
*/

SymfonyCustom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@
6969
*
7070
* @param
7171
*/
72+
73+
/**
74+
* @param string $a
75+
*
76+
* @return string
77+
*
78+
* @param string $b
79+
*/

SymfonyCustom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected function getErrorList(): array
3030
29 => 1,
3131
33 => 1,
3232
67 => 1,
33+
75 => 1,
3334
];
3435
}
3536

0 commit comments

Comments
 (0)