Skip to content

Commit 37ba7f6

Browse files
author
Vincent Langlet
committed
🐛 Handle multiple class in doc comment string
1 parent 955e929 commit 37ba7f6

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

Symfony3Custom/Sniffs/Namespaces/UnusedUseSniff.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,38 @@ public function process(File $phpcsFile, $stackPtr)
132132
);
133133
}
134134

135-
// Check for doc comment tags
135+
// More checks
136136
foreach ($tokens as $token) {
137+
// Check for doc params @...
137138
if ('T_DOC_COMMENT_TAG' === $token['type']) {
138139
// Handle comment tag as @Route(..) or @ORM\Id
139140
if (preg_match('/^@'.$lowerClassName.'(?![a-zA-Z])/i', $token['content']) === 1) {
140141
return;
141142
};
142143
}
143-
}
144144

145-
$warning = 'Unused use statement';
146-
$fix = $phpcsFile->addFixableError($warning, $stackPtr, 'UnusedUse');
145+
// Check for @param Truc or @return Machin
146+
if ('T_DOC_COMMENT_STRING' === $token['type']) {
147+
// Handle @return Machin|Machine
148+
if (preg_match('/^'.$lowerClassName.'\|/i', $token['content']) === 1
149+
|| preg_match('/\|'.$lowerClassName.'\|/i', $token['content']) === 1
150+
|| preg_match('/\|'.$lowerClassName.'$/i', $token['content']) === 1) {
151+
$beforeUsage = $phpcsFile->findPrevious(
152+
Tokens::$emptyTokens,
153+
($classUsed - 1),
154+
null,
155+
true
156+
);
157+
158+
// If a backslash is used before the class name then this is some other use statement.
159+
if (T_USE !== $tokens[$beforeUsage]['code'] && T_NS_SEPARATOR !== $tokens[$beforeUsage]['code']) {
160+
return;
161+
}
162+
}
163+
}
164+
}
147165

166+
$fix = $phpcsFile->addFixableError('Unused use statement', $stackPtr, 'UnusedUse');
148167
if (true === $fix) {
149168
// Remove the whole use statement line.
150169
$phpcsFile->fixer->beginChangeset();

Symfony3Custom/Tests/Namespaces/UnusedUseUnitTest.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Route;
88
use ORM;
99
use Truc;
1010
use Machin;
11+
use Machine;
1112
use Unused;
1213

1314
class Container
@@ -19,7 +20,7 @@ class Container
1920
*
2021
* @param Toto
2122
* @var Truc
22-
* @return Machin
23+
* @return Machin|Machine
2324
*/
2425
function test (Bar $bar)
2526
{

Symfony3Custom/Tests/Namespaces/UnusedUseUnitTest.inc.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Route;
88
use ORM;
99
use Truc;
1010
use Machin;
11+
use Machine;
1112

1213
class Container
1314
{
@@ -18,7 +19,7 @@ class Container
1819
*
1920
* @param Toto
2021
* @var Truc
21-
* @return Machin
22+
* @return Machin|Machine
2223
*/
2324
function test (Bar $bar)
2425
{

Symfony3Custom/Tests/Namespaces/UnusedUseUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class UnusedUseUnitTest extends AbstractSniffUnitTest
2222
public function getErrorList()
2323
{
2424
return array(
25-
11 => 1,
25+
12 => 1,
2626
);
2727
}
2828

0 commit comments

Comments
 (0)