[ticket/13402] Refactor unused use Sniff

PHPBB3-13402
This commit is contained in:
Tristan Darricau 2014-11-29 18:58:51 +01:00
parent c5227ab2a5
commit 13d4394844

View file

@ -24,6 +24,23 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
return array(T_USE);
}
protected function check($found_name, $full_name, $short_name, $line)
{
if ($found_name === $full_name)
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $line, 'FullName');
}
if ($found_name === $short_name)
{
return true;
}
return false;
}
/**
* {@inheritdoc}
*/
@ -74,16 +91,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
$simple_class_name = trim($phpcsFile->getTokensAsString($simple_class_name_start, ($simple_class_name_end - $simple_class_name_start)));
if ($simple_class_name === $class_name_full)
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $simple_statement, 'FullName');
}
if ($simple_class_name === $class_name_short)
{
$ok = true;
}
$ok = $this->check($simple_class_name, $class_name_full, $class_name_short, $simple_statement) ? true : $ok;
}
}
@ -98,16 +106,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
$paamayim_nekudotayim_class_name = trim($phpcsFile->getTokensAsString($paamayim_nekudotayim_class_name_start + 1, ($paamayim_nekudotayim_class_name_end - $paamayim_nekudotayim_class_name_start)));
if ($paamayim_nekudotayim_class_name === $class_name_full)
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $paamayim_nekudotayim, 'FullName');
}
if ($paamayim_nekudotayim_class_name === $class_name_short)
{
$ok = true;
}
$ok = $this->check($paamayim_nekudotayim_class_name, $class_name_full, $class_name_short, $paamayim_nekudotayim) ? true : $ok;
}
// Checks in implements
@ -126,16 +125,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
$implements_class_name = trim($phpcsFile->getTokensAsString($implements_class_name_start, ($implements_class_name_end - $implements_class_name_start)));
if ($implements_class_name === $class_name_full)
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $implements, 'FullName');
}
if ($implements_class_name === $class_name_short)
{
$ok = true;
}
$ok = $this->check($implements_class_name, $class_name_full, $class_name_short, $implements) ? true : $ok;
}
}
@ -167,36 +157,18 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
$comment_parser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile);
$comment_parser->parse();
// Check @param
foreach ($comment_parser->getParams() as $param) {
$type = $param->getType();
if ($type === $class_name_full)
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $param->getLine() + $comment_start, 'FullName');
}
if ($type === $class_name_short)
{
$ok = true;
}
$ok = $this->check($type, $class_name_full, $class_name_short, $param->getLine() + $comment_start) ? true : $ok;
}
// Check @return
$return = $comment_parser->getReturn();
if ($return !== null)
{
$type = $return->getValue();
if ($type === $class_name_full)
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $return->getLine() + $comment_start, 'FullName');
}
if ($type === $class_name_short)
{
$ok = true;
}
$ok = $this->check($type, $class_name_full, $class_name_short, $return->getLine() + $comment_start) ? true : $ok;
}
}
catch (PHP_CodeSniffer_CommentParser_ParserException $e)
@ -207,33 +179,11 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
}
}
$end_function = $phpcsFile->findNext(array(T_CLOSE_PARENTHESIS), ($function_declaration + 1));
$old_argument = $function_declaration;
while (($argument = $phpcsFile->findNext(T_VARIABLE, ($old_argument + 1), $end_function)) !== false)
// Check type hint
$params = $phpcsFile->getMethodParameters($function_declaration);
foreach ($params as $param)
{
$old_argument = $argument;
$start_argument = $phpcsFile->findPrevious(array(T_OPEN_PARENTHESIS, T_COMMA), $argument);
$argument_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($start_argument + 1), $argument);
// Skip the parameter if no type is defined.
if ($argument_class_name_start !== false)
{
$argument_class_name_end = $phpcsFile->findNext($find, ($argument_class_name_start + 1), null, true);
$argument_class_name = $phpcsFile->getTokensAsString($argument_class_name_start, ($argument_class_name_end - $argument_class_name_start - 1));
if ($argument_class_name === $class_name_full)
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $function_declaration, 'FullName');
}
if ($argument_class_name === $class_name_short)
{
$ok = true;
}
}
$ok = $this->check($param['type_hint'], $class_name_full, $class_name_short, $function_declaration) ? true : $ok;
}
}