From: Tim Düsterhus Date: Sat, 5 Jan 2013 15:42:56 +0000 (+0100) Subject: Ensure that all classes are imported via use X-Git-Tag: 2.0.0_Beta_1~588 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=aec2b9fc13bb04a17602179637add8398893ca54;p=GitHub%2FWoltLab%2FWCF.git Ensure that all classes are imported via use --- diff --git a/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php new file mode 100644 index 0000000000..6a869d2406 --- /dev/null +++ b/CodeSniff/WCF/Sniffs/Namespaces/ClassMustBeImportedSniff.php @@ -0,0 +1,66 @@ + + * @package com.woltlab.wcf + * @category Community Framework + */ +class WCF_Sniffs_Namespaces_ClassMustBeImportedSniff implements PHP_CodeSniffer_Sniff { + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + return array( + T_NS_SEPARATOR + ); + } + + /** + * Processes this sniff, when one of its tokens is encountered. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the + * stack passed in $tokens. + */ + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + $tokens = $phpcsFile->getTokens(); + + // skip files in global namespace + if ($phpcsFile->findPrevious(T_NAMESPACE, $stackPtr) === false) { + return; + } + + // no use found + if ($phpcsFile->findPrevious(array(T_NAMESPACE, T_USE), ($stackPtr - 1), null, false, null, true) === false) { + // find previous class part and non class part + $prevClassPart = $phpcsFile->findPrevious(array(T_STRING, T_NS_SEPARATOR), $stackPtr - 1); + $prevNonClassPart = $phpcsFile->findPrevious(array(T_STRING, T_NS_SEPARATOR), $stackPtr - 1, null, true); + + // backslash as the very first character of the class is allowed (global classes) + if ($prevClassPart >= $prevNonClassPart) { + $nextNonClassPart = $phpcsFile->findNext(array(T_STRING, T_NS_SEPARATOR), $stackPtr + 1, null, true); + $lastNSSep = $phpcsFile->findPrevious(T_NS_SEPARATOR, $nextNonClassPart); + + // check whether we are the last backslash (no multiple reporting) + if ($lastNSSep === $stackPtr) { + $start = $phpcsFile->findPrevious(array(T_NS_SEPARATOR, T_STRING), $stackPtr - 1, null, true) + 1; + $end = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($start + 1), null, true); + $class = ''; + for ($i = $start; $i < $end; $i++) { + $class .= $tokens[$i]['content']; + } + + $error = 'Namespaced classes (%s) must be imported with use.'; + $data = array( + $class + ); + $phpcsFile->addError($error, $stackPtr, 'MustBeImported', $data); + } + } + } + } +} \ No newline at end of file diff --git a/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php index 0f9281a9e9..be72338a55 100644 --- a/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php +++ b/CodeSniff/WCF/Sniffs/Namespaces/SortedUseDeclarationSniff.php @@ -5,7 +5,7 @@ * See: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php * * @author Tim Duesterhus - * @license BSD Licence + * @license GNU Lesser General Public License * @package com.woltlab.wcf * @category Community Framework */ diff --git a/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php b/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php index d9ba6632ed..1d9bac295a 100644 --- a/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php +++ b/CodeSniff/WCF/Sniffs/Namespaces/UseDeclarationSniff.php @@ -5,7 +5,7 @@ * See: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php * * @author Tim Duesterhus - * @license BSD Licence + * @license GNU Lesser General Public License * @package com.woltlab.wcf * @category Community Framework */ diff --git a/CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php b/CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php index d47fa82193..e221f6ba59 100644 --- a/CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php +++ b/CodeSniff/WCF/Sniffs/PHP/DiscouragePCRESniff.php @@ -3,7 +3,7 @@ * Discourages use of raw PCRE functions. \wcf\system\Regex is a superior way to use Regex. * * @author Tim Duesterhus - * @license BSD Licence + * @license GNU Lesser General Public License * @package com.woltlab.wcf * @category Community Framework */ diff --git a/CodeSniff/WCF/ruleset.xml b/CodeSniff/WCF/ruleset.xml index 7f66d7cc19..7853276377 100644 --- a/CodeSniff/WCF/ruleset.xml +++ b/CodeSniff/WCF/ruleset.xml @@ -32,6 +32,7 @@ +