Skip to content
Snippets Groups Projects
Commit 783a88cf authored by Klaus Purer's avatar Klaus Purer
Browse files

feat(UnusedUseStatementSniff): Improve fixer to update class references in...

feat(UnusedUseStatementSniff): Improve fixer to update class references in @var doc comments (#2625124)
parent 4e113207
Branches
Tags
No related merge requests found
......@@ -156,8 +156,38 @@ class Drupal_Sniffs_Classes_UnusedUseStatementSniff implements PHP_CodeSniffer_S
$i++;
}
// Replace @var data types in doc comments with the fully qualified class
// name.
$useNamespacePtr = $phpcsFile->findNext([T_STRING], ($stackPtr + 1));
$useNamespaceEnd = $phpcsFile->findNext(
[
T_NS_SEPARATOR,
T_STRING,
],
($useNamespacePtr + 1),
null,
true
);
$fullNamespace = $phpcsFile->getTokensAsString($useNamespacePtr, ($useNamespaceEnd - $useNamespacePtr));
$tag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($stackPtr + 1));
while ($tag !== false) {
if ($tokens[$tag]['content'] === '@var'
&& isset($tokens[($tag + 1)]) === true
&& $tokens[($tag + 1)]['code'] === T_DOC_COMMENT_WHITESPACE
&& isset($tokens[($tag + 2)]) === true
&& $tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING
&& $tokens[($tag + 2)]['content'] === $tokens[$classPtr]['content']
) {
$phpcsFile->fixer->replaceToken(($tag + 2), '\\'.$fullNamespace);
}
$tag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($tag + 1));
}
$phpcsFile->fixer->endChangeset();
}
}//end if
}//end process()
......
......@@ -16,6 +16,8 @@ use Example\MyUrlHelper;
use MyNamespace\Depth\UnusedSameNamespace;
use /* I like weird comment placements */ MyNamespace\Depth\AnotherUnusedSameNamespace /* Oh yes I do */;
use MyNamespace\Depth\SomeClass as CoreSomeClass;
use Some\Data\VarName;
use Some\Data\VarName2 as AliasVarName2;
/**
* Bla.
......@@ -24,6 +26,20 @@ class Pum {
use TraitTest;
use Test\AnotherTrait;
/**
* This data type should be fixed to be fully qualified.
*
* @var VarName
*/
protected $x;
/**
* Aliased type that is otherwise unused.
*
* @var AliasVarName2
*/
protected $y;
/**
* Description.
*/
......
......@@ -16,6 +16,20 @@ class Pum {
use TraitTest;
use Test\AnotherTrait;
/**
* This data type should be fixed to be fully qualified.
*
* @var \Some\Data\VarName
*/
protected $x;
/**
* Aliased type that is otherwise unused.
*
* @var \Some\Data\VarName2
*/
protected $y;
/**
* Description.
*/
......
......@@ -39,6 +39,8 @@ class Drupal_Sniffs_Classes_UnusedUseStatementUnitTest extends CoderSniffUnitTes
14 => 1,
16 => 1,
17 => 1,
19 => 1,
20 => 1,
);
}//end getWarningList()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment