[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); 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} * {@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))); $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) $ok = $this->check($simple_class_name, $class_name_full, $class_name_short, $simple_statement) ? true : $ok;
{
$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;
}
} }
} }
@ -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))); $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) $ok = $this->check($paamayim_nekudotayim_class_name, $class_name_full, $class_name_short, $paamayim_nekudotayim) ? true : $ok;
{
$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;
}
} }
// Checks in implements // 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))); $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) $ok = $this->check($implements_class_name, $class_name_full, $class_name_short, $implements) ? true : $ok;
{
$error = 'Either use statement or full name must be used.';
$phpcsFile->addError($error, $implements, 'FullName');
}
if ($implements_class_name === $class_name_short)
{
$ok = true;
}
} }
} }
@ -153,7 +143,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
T_FUNCTION, T_FUNCTION,
T_OPEN_TAG, T_OPEN_TAG,
); );
$comment_end = $phpcsFile->findPrevious($find, ($function_declaration - 1)); $comment_end = $phpcsFile->findPrevious($find, ($function_declaration - 1));
if ($comment_end !== false) if ($comment_end !== false)
{ {
@ -167,36 +157,18 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
$comment_parser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $comment_parser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile);
$comment_parser->parse(); $comment_parser->parse();
// Check @param
foreach ($comment_parser->getParams() as $param) { foreach ($comment_parser->getParams() as $param) {
$type = $param->getType(); $type = $param->getType();
$ok = $this->check($type, $class_name_full, $class_name_short, $param->getLine() + $comment_start) ? true : $ok;
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;
}
} }
// Check @return
$return = $comment_parser->getReturn(); $return = $comment_parser->getReturn();
if ($return !== null) if ($return !== null)
{ {
$type = $return->getValue(); $type = $return->getValue();
$ok = $this->check($type, $class_name_full, $class_name_short, $return->getLine() + $comment_start) ? true : $ok;
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;
}
} }
} }
catch (PHP_CodeSniffer_CommentParser_ParserException $e) 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)); // Check type hint
$old_argument = $function_declaration; $params = $phpcsFile->getMethodParameters($function_declaration);
while (($argument = $phpcsFile->findNext(T_VARIABLE, ($old_argument + 1), $end_function)) !== false) foreach ($params as $param)
{ {
$old_argument = $argument; $ok = $this->check($param['type_hint'], $class_name_full, $class_name_short, $function_declaration) ? true : $ok;
$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;
}
}
} }
} }