Skip to content

Commit 43369e6

Browse files
committed
Added more tests and fixes
Added cases with names with namespace separator and some cases with comments
1 parent 110dcb4 commit 43369e6

File tree

4 files changed

+119
-17
lines changed

4 files changed

+119
-17
lines changed

src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,32 @@ public function processOpen(File $phpcsFile, $stackPtr)
168168
$fix = $phpcsFile->addFixableError($error, $keyword, ucfirst($keywordType).'Line', $data);
169169
if ($fix === true) {
170170
$phpcsFile->fixer->beginChangeset();
171-
for ($i = ($stackPtr + 1); $i < $keyword; $i++) {
172-
if ($tokens[$i]['line'] !== $tokens[($i + 1)]['line']) {
173-
$phpcsFile->fixer->substrToken($i, 0, (strlen($phpcsFile->eolChar) * -1));
171+
$comments = [];
172+
173+
for ($i = ($stackPtr + 1); $i < $keyword; ++$i) {
174+
if ($tokens[$i]['code'] === T_COMMENT) {
175+
$comments[] = trim($tokens[$i]['content']);
176+
}
177+
178+
if ($tokens[$i]['code'] === T_WHITESPACE
179+
|| $tokens[$i]['code'] === T_COMMENT
180+
) {
181+
$phpcsFile->fixer->replaceToken($i, ' ');
182+
}
183+
}
184+
185+
$phpcsFile->fixer->addContent($stackPtr, ' ');
186+
if (empty($comments) === false) {
187+
$i = $keyword;
188+
while ($tokens[($i + 1)]['line'] === $tokens[$keyword]['line']) {
189+
++$i;
174190
}
191+
192+
$phpcsFile->fixer->addContentBefore($i, ' '.implode(' ', $comments));
175193
}
176194

177-
$phpcsFile->fixer->addContentBefore($keyword, ' ');
178195
$phpcsFile->fixer->endChangeset();
179-
}
196+
}//end if
180197
} else {
181198
// Check the whitespace before. Whitespace after is checked
182199
// later by looking at the whitespace before the first class name
@@ -228,19 +245,28 @@ public function processOpen(File $phpcsFile, $stackPtr)
228245
$nextClass = $phpcsFile->findNext($find, ($nextClass + 1), ($openingBrace - 1));
229246
}
230247

248+
$skip = array_merge(
249+
Tokens::$emptyTokens,
250+
[
251+
T_STRING,
252+
T_NS_SEPARATOR,
253+
]
254+
);
255+
231256
$classCount = count($classNames);
232257
$checkingImplements = false;
233258
$implementsToken = null;
234-
$numberOfInterface = 0;
259+
$oneInterface = false;
235260
foreach ($classNames as $n => $className) {
236261
if ($tokens[$className]['code'] === $keywordTokenType) {
237262
$checkingImplements = true;
238263
$implementsToken = $className;
239-
continue;
240-
}
241264

242-
if ($checkingImplements === true) {
243-
$numberOfInterface += 1;
265+
if (false === $phpcsFile->findNext($skip, ($implementsToken + 1), ($openingBrace - 1), true)) {
266+
$oneInterface = true;
267+
}
268+
269+
continue;
244270
}
245271

246272
if ($checkingImplements === true
@@ -302,15 +328,39 @@ public function processOpen(File $phpcsFile, $stackPtr)
302328
$phpcsFile->fixer->addNewline($prev);
303329
$phpcsFile->fixer->endChangeset();
304330
}
305-
} else if ($n === ($classCount - 1) && $numberOfInterface === 1) {
306-
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($className - 1), $implements, true);
307-
if ($tokens[$prev]['line'] !== $tokens[$className]['line']) {
331+
} else if ($oneInterface === true) {
332+
if ($tokens[$implementsToken]['line'] !== $tokens[$className]['line']) {
308333
$error = 'Interface name should be in the same line as '.$keywordType.' keyword';
309334
$fix = $phpcsFile->addFixableError($error, $className, 'InterfaceWrongLine');
310335
if ($fix === true) {
311-
$phpcsFile->fixer->replaceToken(($prev + 1), ' ');
312-
}
313-
}
336+
$phpcsFile->fixer->beginChangeset();
337+
$comments = [];
338+
339+
for ($i = ($implementsToken + 1); $i < $className; ++$i) {
340+
if ($tokens[$i]['code'] === T_COMMENT) {
341+
$comments[] = trim($tokens[$i]['content']);
342+
}
343+
344+
if ($tokens[$i]['code'] === T_WHITESPACE
345+
|| $tokens[$i]['code'] === T_COMMENT
346+
) {
347+
$phpcsFile->fixer->replaceToken($i, '');
348+
}
349+
}
350+
351+
$phpcsFile->fixer->addContent($implementsToken, ' ');
352+
if (empty($comments) === false) {
353+
$i = $className;
354+
while ($tokens[($i + 1)]['line'] === $tokens[$className]['line']) {
355+
++$i;
356+
}
357+
358+
$phpcsFile->fixer->addContentBefore($i, ' '.implode(' ', $comments));
359+
}
360+
361+
$phpcsFile->fixer->endChangeset();
362+
}//end if
363+
}//end if
314364
} else {
315365
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($className - 1), $implements);
316366
if ($tokens[$prev]['line'] !== $tokens[$className]['line']) {
@@ -321,7 +371,7 @@ public function processOpen(File $phpcsFile, $stackPtr)
321371

322372
$expected = ($classIndent + $this->indent);
323373
if ($found !== $expected) {
324-
$error = 'Expected %s spaces before interface name; %s found';
374+
$error = 'Expected %s spaces before interface name; %s found - '.$n.' :: '.$classCount;
325375
$data = [
326376
$expected,
327377
$found,

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,29 @@ class C5 extends Foo implements
205205
Baz
206206
{
207207
}
208+
209+
class C6 extends \Foo\Bar implements
210+
\Baz\Bar
211+
{
212+
}
213+
214+
interface I4 extends
215+
\Baz
216+
\Bar
217+
{
218+
}
219+
220+
interface I5 extends /* comment */
221+
\Foo\Bar
222+
{
223+
}
224+
225+
interface I6 extends // comment
226+
\Foo\Bar
227+
{
228+
}
229+
230+
class C7 extends // comment
231+
\Foo\Bar implements \Baz\Bar
232+
{
233+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,23 @@ class C5 extends Foo implements
197197
Baz
198198
{
199199
}
200+
201+
class C6 extends \Foo\Bar implements \Baz\Bar
202+
{
203+
}
204+
205+
interface I4 extends \Baz\Bar
206+
{
207+
}
208+
209+
interface I5 extends \Foo\Bar /* comment */
210+
{
211+
}
212+
213+
interface I6 extends \Foo\Bar // comment
214+
{
215+
}
216+
217+
class C7 extends \Foo\Bar implements \Baz\Bar // comment
218+
{
219+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public function getErrorList()
6060
199 => 1,
6161
204 => 1,
6262
205 => 1,
63+
210 => 1,
64+
215 => 2,
65+
216 => 1,
66+
221 => 1,
67+
226 => 1,
68+
231 => 2,
6369
];
6470

6571
}//end getErrorList()

0 commit comments

Comments
 (0)