From e46ef86104d457e0c98b7b1376d9cfbf5f1a8c1b Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 16 Jun 2014 22:59:46 +0200 Subject: [PATCH 1/5] [ticket/12726] PSR2.Namespaces.NamespaceDeclaration PHPBB3-12726 --- build/code_sniffer/ruleset-php-strict.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build/code_sniffer/ruleset-php-strict.xml b/build/code_sniffer/ruleset-php-strict.xml index c722f7851c..59db73dec4 100644 --- a/build/code_sniffer/ruleset-php-strict.xml +++ b/build/code_sniffer/ruleset-php-strict.xml @@ -39,4 +39,7 @@ There MUST be one blank line after the use block. --> + + + From fa5afb60aa1879311a5f909b680af7da26647c6b Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 16 Jun 2014 22:55:42 +0200 Subject: [PATCH 2/5] [ticket/12726] Ensure PHP files do not contain any unused use statements PHPBB3-12726 --- .../Sniffs/Namespaces/UnusedUseSniff.php | 206 ++++++++++++++++++ build/code_sniffer/ruleset-php-strict.xml | 3 + phpBB/phpbb/auth/provider_collection.php | 2 +- 3 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php new file mode 100644 index 0000000000..dad384d855 --- /dev/null +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -0,0 +1,206 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +/** +* Checks that each use statement is used. +*/ +class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff +{ + /** + * {@inheritdoc} + */ + public function register() + { + return array(T_USE); + } + + /** + * {@inheritdoc} + */ + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + if ($this->should_ignore_use($phpcsFile, $stackPtr) === true) { + return; + } + + $tokens = $phpcsFile->getTokens(); + + $class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($stackPtr + 1)); + + $find = array( + T_NS_SEPARATOR, + T_STRING, + T_WHITESPACE, + ); + + $class_name_end = $phpcsFile->findNext($find, ($stackPtr + 1), null, true); + + $aliasing_as_position = $phpcsFile->findNext(T_AS, $class_name_end, null, false, null, true); + if ($aliasing_as_position !== false) + { + $alias_position = $phpcsFile->findNext(T_STRING, $aliasing_as_position, null, false, null, true); + $class_name_short = $tokens[$alias_position]['content']; + $class_name_full = $phpcsFile->getTokensAsString($class_name_start, ($class_name_end - $class_name_start - 1)); + } + else + { + $class_name_full = $phpcsFile->getTokensAsString($class_name_start, ($class_name_end - $class_name_start)); + $class_name_short = $tokens[$class_name_end - 1]['content']; + } + + $ok = false; + + // Checks in simple statements (new, instanceof and extends) + foreach (array(T_INSTANCEOF, T_NEW, T_EXTENDS) as $keyword) + { + $old_simple_statement = $stackPtr; + while (($simple_statement = $phpcsFile->findNext($keyword, ($old_simple_statement + 1))) !== false) + { + $old_simple_statement = $simple_statement; + + $simple_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($simple_statement + 1)); + $simple_class_name_end = $phpcsFile->findNext($find, ($simple_statement + 1), null, true); + + $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; + } + } + } + + // Checks paamayim nekudotayim + $old_paamayim_nekudotayim = $stackPtr; + while (($paamayim_nekudotayim = $phpcsFile->findNext(T_PAAMAYIM_NEKUDOTAYIM, ($old_paamayim_nekudotayim + 1))) !== false) + { + $old_paamayim_nekudotayim = $paamayim_nekudotayim; + + $paamayim_nekudotayim_class_name_start = $phpcsFile->findPrevious($find, $paamayim_nekudotayim - 1, null, true); + $paamayim_nekudotayim_class_name_end = $paamayim_nekudotayim - 1; + + $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; + } + } + + // Checks in implements + $old_implements = $stackPtr; + while (($implements = $phpcsFile->findNext(T_IMPLEMENTS, ($old_implements + 1))) !== false) + { + $old_implements = $implements; + + $old_implemented_class = $implements; + while (($implemented_class = $phpcsFile->findNext(T_STRING, ($old_implemented_class + 1), null, false, null, true)) !== false) + { + $old_implemented_class = $implemented_class; + + $implements_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($implemented_class - 1)); + $implements_class_name_end = $phpcsFile->findNext($find, ($implemented_class - 1), null, true); + + $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; + } + } + } + + // Checks in type hinting + $old_function_declaration = $stackPtr; + while (($function_declaration = $phpcsFile->findNext(T_FUNCTION, ($old_function_declaration + 1))) !== false) + { + $old_function_declaration = $function_declaration; + + $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) + { + $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); + $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; + } + } + } + + if (!$ok) + { + $error = 'There must not be unused USE statement.'; + $phpcsFile->addError($error, $stackPtr, 'Unused'); + } + } + + /** + * Check if this use statement is part of the namespace block. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in + * the stack passed in $tokens. + * + * @return bool + */ + private function should_ignore_use(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + // Ignore USE keywords inside closures. + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); + if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { + return true; + } + + // Ignore USE keywords for traits. + if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true) { + return true; + } + + return false; + + } +} diff --git a/build/code_sniffer/ruleset-php-strict.xml b/build/code_sniffer/ruleset-php-strict.xml index 59db73dec4..9e2f0664d8 100644 --- a/build/code_sniffer/ruleset-php-strict.xml +++ b/build/code_sniffer/ruleset-php-strict.xml @@ -42,4 +42,7 @@ + + + diff --git a/phpBB/phpbb/auth/provider_collection.php b/phpBB/phpbb/auth/provider_collection.php index fe32a34e12..a74a2135dc 100644 --- a/phpBB/phpbb/auth/provider_collection.php +++ b/phpBB/phpbb/auth/provider_collection.php @@ -29,7 +29,7 @@ class provider_collection extends \phpbb\di\service_collection * @param ContainerInterface $container Container object * @param \phpbb\config\config $config phpBB config */ - public function __construct($container, \phpbb\config\config $config) + public function __construct(ContainerInterface $container, \phpbb\config\config $config) { $this->container = $container; $this->config = $config; From 2ff9064ffbde4f6add1de8fbfcdd0ef120e9b85d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 22 Jun 2014 20:54:11 +0200 Subject: [PATCH 3/5] [ticket/12726] Fix coding style PHPBB3-12726 --- .../code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index dad384d855..ba7e0b6557 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -29,7 +29,8 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { - if ($this->should_ignore_use($phpcsFile, $stackPtr) === true) { + if ($this->should_ignore_use($phpcsFile, $stackPtr) === true) + { return; } @@ -108,7 +109,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff $ok = true; } } - + // Checks in implements $old_implements = $stackPtr; while (($implements = $phpcsFile->findNext(T_IMPLEMENTS, ($old_implements + 1))) !== false) From 147ae8374ceff1d1247564e2541b9143d0da0514 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 22 Jun 2014 20:54:52 +0200 Subject: [PATCH 4/5] [ticket/12726] Update error message PHPBB3-12726 --- build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index ba7e0b6557..7e09650622 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -172,7 +172,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff if (!$ok) { - $error = 'There must not be unused USE statement.'; + $error = 'There must not be unused USE statements.'; $phpcsFile->addError($error, $stackPtr, 'Unused'); } } From 7958a97fdd2120415a38dda422b440650d4dc356 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 22 Jun 2014 23:57:39 +0200 Subject: [PATCH 5/5] [ticket/12726] Fix coding style PHPBB3-12726 --- .../code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index 7e09650622..16e3427bd9 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -192,12 +192,14 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff // Ignore USE keywords inside closures. $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { + if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) + { return true; } // Ignore USE keywords for traits. - if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true) { + if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true) + { return true; }