aboutsummaryrefslogtreecommitdiffstats
path: root/build/code_sniffer/phpbb/Sniffs
diff options
context:
space:
mode:
authorTristan Darricau <github@nicofuma.fr>2014-11-29 18:58:51 +0100
committerTristan Darricau <github@nicofuma.fr>2014-11-30 19:18:18 +0100
commit13d43948447ab3a29105e6a2756c34831b1d5c9d (patch)
tree78843c6f40997302d1ac82815e3de5b47d11c248 /build/code_sniffer/phpbb/Sniffs
parentc5227ab2a5d173dba881a275077e434afbe405a9 (diff)
downloadforums-13d43948447ab3a29105e6a2756c34831b1d5c9d.tar
forums-13d43948447ab3a29105e6a2756c34831b1d5c9d.tar.gz
forums-13d43948447ab3a29105e6a2756c34831b1d5c9d.tar.bz2
forums-13d43948447ab3a29105e6a2756c34831b1d5c9d.tar.xz
forums-13d43948447ab3a29105e6a2756c34831b1d5c9d.zip
[ticket/13402] Refactor unused use Sniff
PHPBB3-13402
Diffstat (limited to 'build/code_sniffer/phpbb/Sniffs')
-rw-r--r--build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php108
1 files changed, 29 insertions, 79 deletions
diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php
index 1f961e102f..0dea58dd49 100644
--- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php
+++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php
@@ -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;
}
}
@@ -153,7 +143,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff
T_FUNCTION,
T_OPEN_TAG,
);
-
+
$comment_end = $phpcsFile->findPrevious($find, ($function_declaration - 1));
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->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;
}
}