Skip to content

Commit 7f763c5

Browse files
Merge pull request #92 from VincentLanglet/improve
Improve
2 parents 1cb05b2 + 240d5f3 commit 7f763c5

17 files changed

+172
-202
lines changed

SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
651651
);
652652

653653
if ($fix) {
654-
if (0 === $found) {
655-
$phpcsFile->fixer->addContent($index['index'] - 1, str_repeat(' ', $expected));
656-
} else {
657-
$phpcsFile->fixer->replaceToken($index['index'] - 1, str_repeat(' ', $expected));
658-
}
654+
$this->align($phpcsFile, $index['index'], $expected, $found);
659655
}
660656

661657
continue;
@@ -679,20 +675,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
679675
);
680676

681677
if ($fix) {
682-
if ('newline' === $found) {
683-
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $index['value'] - 1, null, true);
684-
$phpcsFile->fixer->beginChangeset();
685-
for ($i = $prev + 1; $i < $index['value']; $i++) {
686-
$phpcsFile->fixer->replaceToken($i, '');
687-
}
688-
689-
$phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected));
690-
$phpcsFile->fixer->endChangeset();
691-
} elseif (0 === $found) {
692-
$phpcsFile->fixer->addContent($index['arrow'] - 1, str_repeat(' ', $expected));
693-
} else {
694-
$phpcsFile->fixer->replaceToken($index['arrow'] - 1, str_repeat(' ', $expected));
695-
}
678+
$this->align($phpcsFile, $index['arrow'], $expected, $found);
696679
}
697680

698681
continue;
@@ -709,28 +692,40 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
709692

710693
$fix = $phpcsFile->addFixableError(
711694
'Array value not aligned correctly; expected %s space(s) but found %s',
712-
$index['arrow'],
695+
$index['value'],
713696
'ValueNotAligned',
714697
[$expected, $found]
715698
);
716699

717700
if ($fix) {
718-
if ('newline' === $found) {
719-
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $index['value'] - 1, null, true);
720-
$phpcsFile->fixer->beginChangeset();
721-
for ($i = $prev + 1; $i < $index['value']; $i++) {
722-
$phpcsFile->fixer->replaceToken($i, '');
723-
}
724-
725-
$phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected));
726-
$phpcsFile->fixer->endChangeset();
727-
} elseif (0 === $found) {
728-
$phpcsFile->fixer->addContent($index['value'] - 1, str_repeat(' ', $expected));
729-
} else {
730-
$phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected));
731-
}
701+
$this->align($phpcsFile, $index['value'], $expected, $found);
732702
}
733703
}
734704
}
735705
}
706+
707+
/**
708+
* @param File $phpcsFile
709+
* @param int $elementIndex
710+
* @param int $expected
711+
* @param int|string $found
712+
*/
713+
private function align(File $phpcsFile, int $elementIndex, int $expected, $found): void
714+
{
715+
if ('newline' === $found) {
716+
$phpcsFile->fixer->beginChangeset();
717+
718+
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $elementIndex - 1, null, true);
719+
for ($i = $prev + 1; $i < $elementIndex; $i++) {
720+
$phpcsFile->fixer->replaceToken($i, '');
721+
}
722+
723+
$phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected));
724+
$phpcsFile->fixer->endChangeset();
725+
} elseif (0 === $found) {
726+
$phpcsFile->fixer->addContent($elementIndex - 1, str_repeat(' ', $expected));
727+
} else {
728+
$phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected));
729+
}
730+
}
736731
}

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
}

0 commit comments

Comments
 (0)