From cb894cee42264d239e175e6ba753cec7c23c92b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Bartus?= Date: Thu, 31 Dec 2020 14:39:11 +0100 Subject: [PATCH 1/6] [ticket/16671] Match fully qualified names correctly PHPBB3-16671 --- .../Sniffs/Namespaces/UnusedUseSniff.php | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index 0279888de1..f7f6300df5 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -24,16 +24,23 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff */ public function register() { - return array(T_USE); + return [T_USE]; } - protected function check(File $phpcsFile, $found_name, $full_name, $short_name, $line) + protected function check(File $phpcsFile, $found_name, $full_name, $short_name, $stack_pointer) { + $found_name = ltrim($found_name, '\\'); + $full_name = ltrim($full_name, '\\'); if ($found_name === $full_name) { $error = 'Either use statement or full name must be used.'; - $phpcsFile->addError($error, $line, 'FullName'); + $phpcsFile->addError($error, $stack_pointer, 'FullName'); + + if (strpos($phpcsFile->getFilename(), 'cron/manager.php') !== false) + { + print("$found_name, $full_name, $short_name}\n"); + } } if ($found_name === $short_name) @@ -99,7 +106,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $simple_class_name = trim($phpcsFile->getTokensAsString($simple_class_name_start, ($simple_class_name_end - $simple_class_name_start))); - $ok = $this->check($phpcsFile, $simple_class_name, $class_name_full, $class_name_short, $simple_statement) ? true : $ok; + $ok = $this->check($phpcsFile, $simple_class_name, $class_name_full, $class_name_short, $simple_statement) || $ok; } } @@ -114,7 +121,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $paamayim_nekudotayim_class_name = trim($phpcsFile->getTokensAsString($paamayim_nekudotayim_class_name_start + 1, ($paamayim_nekudotayim_class_name_end - $paamayim_nekudotayim_class_name_start))); - $ok = $this->check($phpcsFile, $paamayim_nekudotayim_class_name, $class_name_full, $class_name_short, $paamayim_nekudotayim) ? true : $ok; + $ok = $this->check($phpcsFile, $paamayim_nekudotayim_class_name, $class_name_full, $class_name_short, $paamayim_nekudotayim) || $ok; } // Checks in implements @@ -133,7 +140,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $implements_class_name = trim($phpcsFile->getTokensAsString($implements_class_name_start, ($implements_class_name_end - $implements_class_name_start))); - $ok = $this->check($phpcsFile, $implements_class_name, $class_name_full, $class_name_short, $implements) ? true : $ok; + $ok = $this->check($phpcsFile, $implements_class_name, $class_name_full, $class_name_short, $implements) || $ok; } } @@ -141,7 +148,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff while (($docblock = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, ($old_docblock + 1))) !== false) { $old_docblock = $docblock; - $ok = $this->checkDocblock($phpcsFile, $docblock, $tokens, $class_name_full, $class_name_short) ? true : $ok; + $ok = $this->checkDocblock($phpcsFile, $docblock, $tokens, $class_name_full, $class_name_short) || $ok; } // Checks in type hinting @@ -154,7 +161,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $params = $phpcsFile->getMethodParameters($function_declaration); foreach ($params as $param) { - $ok = $this->check($phpcsFile, $param['type_hint'], $class_name_full, $class_name_short, $function_declaration) ? true : $ok; + $ok = $this->check($phpcsFile, $param['type_hint'], $class_name_full, $class_name_short, $function_declaration) || $ok; } } @@ -169,7 +176,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $caught_class_name = trim($phpcsFile->getTokensAsString($caught_class_name_start, ($caught_class_name_end - $caught_class_name_start))); - $ok = $this->check($phpcsFile, $caught_class_name, $class_name_full, $class_name_short, $catch) ? true : $ok; + $ok = $this->check($phpcsFile, $caught_class_name, $class_name_full, $class_name_short, $catch) || $ok; } if (!$ok) @@ -247,7 +254,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $classes = explode('|', str_replace('[]', '', $classes)); foreach ($classes as $class) { - $ok = $this->check($phpcsFile, $class, $class_name_full, $class_name_short, $tokens[$tag + 2]['line']) ? true : $ok; + $ok = $this->check($phpcsFile, $class, $class_name_full, $class_name_short, $tag + 2) || $ok; } } From 8830354555e88abd461dec93e38776d22443f7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Bartus?= Date: Thu, 31 Dec 2020 14:40:14 +0100 Subject: [PATCH 2/6] [ticket/16671] Fix coding style PHPBB3-16671 --- phpBB/phpbb/console/command/update/check.php | 3 ++- phpBB/phpbb/controller/resolver.php | 4 ++-- phpBB/phpbb/cron/event/cron_runner_listener.php | 6 +++--- phpBB/phpbb/cron/manager.php | 10 +++------- phpBB/phpbb/db/extractor/mssql_extractor.php | 6 +++--- phpBB/phpbb/db/extractor/mysql_extractor.php | 6 +++--- phpBB/phpbb/db/extractor/postgres_extractor.php | 2 +- phpBB/phpbb/db/extractor/sqlite3_extractor.php | 2 +- .../di/extension/container_configuration.php | 2 +- phpBB/phpbb/di/service_collection.php | 2 +- phpBB/phpbb/files/upload.php | 2 +- .../install/console/command/install/install.php | 2 +- .../install/console/command/update/update.php | 2 +- phpBB/phpbb/install/controller/helper.php | 16 ++++++++-------- phpBB/phpbb/install/helper/container_factory.php | 8 ++++---- phpBB/phpbb/install/helper/database.php | 2 +- phpBB/phpbb/install/helper/iohandler/factory.php | 2 +- phpBB/phpbb/install/installer.php | 4 ++-- phpBB/phpbb/install/installer_configuration.php | 2 +- .../module/install_data/task/add_modules.php | 2 +- .../module/install_finish/task/notify_user.php | 2 +- .../obtain_data/task/obtain_admin_data.php | 2 +- .../obtain_data/task/obtain_board_data.php | 2 +- .../obtain_data/task/obtain_database_data.php | 2 +- .../task/obtain_file_updater_method.php | 4 ++-- .../obtain_data/task/obtain_update_ftp_data.php | 4 ++-- .../obtain_data/task/obtain_update_settings.php | 4 ++-- phpBB/phpbb/install/updater_configuration.php | 2 +- phpBB/phpbb/language/language.php | 2 +- phpBB/phpbb/module/module_manager.php | 12 ++++++------ phpBB/phpbb/notification/method/email.php | 2 +- .../phpbb/notification/method/messenger_base.php | 2 +- phpBB/phpbb/report/controller/report.php | 2 +- phpBB/phpbb/report/handler_factory.php | 2 +- phpBB/phpbb/report/report_handler.php | 2 +- phpBB/phpbb/report/report_handler_pm.php | 4 ++-- phpBB/phpbb/report/report_handler_post.php | 4 ++-- phpBB/phpbb/template/twig/twig.php | 2 +- phpBB/phpbb/textformatter/s9e/bbcode_merger.php | 2 +- phpBB/phpbb/textformatter/s9e/factory.php | 6 +++--- 40 files changed, 73 insertions(+), 76 deletions(-) diff --git a/phpBB/phpbb/console/command/update/check.php b/phpBB/phpbb/console/command/update/check.php index 4cd7d2155c..d98cb1da3e 100644 --- a/phpBB/phpbb/console/command/update/check.php +++ b/phpBB/phpbb/console/command/update/check.php @@ -26,11 +26,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class check extends \phpbb\console\command\command { - /** @var \phpbb\config\config */ + /** @var config */ protected $config; /** @var \Symfony\Component\DependencyInjection\ContainerBuilder */ protected $phpbb_container; + /** * @var language */ diff --git a/phpBB/phpbb/controller/resolver.php b/phpBB/phpbb/controller/resolver.php index f8dffc12de..ce830f7723 100644 --- a/phpBB/phpbb/controller/resolver.php +++ b/phpBB/phpbb/controller/resolver.php @@ -64,7 +64,7 @@ class resolver implements ControllerResolverInterface /** * Load a controller callable * - * @param \Symfony\Component\HttpFoundation\Request $request Symfony Request object + * @param Request $request Symfony Request object * @return bool|Callable Callable or false * @throws \phpbb\controller\exception */ @@ -119,7 +119,7 @@ class resolver implements ControllerResolverInterface * and should match the parameters of the method you are using as your * controller. * - * @param \Symfony\Component\HttpFoundation\Request $request Symfony Request object + * @param Request $request Symfony Request object * @param mixed $controller A callable (controller class, method) * @return array An array of arguments to pass to the controller * @throws \phpbb\controller\exception diff --git a/phpBB/phpbb/cron/event/cron_runner_listener.php b/phpBB/phpbb/cron/event/cron_runner_listener.php index 9e9ecf0d47..c9c13ddc06 100644 --- a/phpBB/phpbb/cron/event/cron_runner_listener.php +++ b/phpBB/phpbb/cron/event/cron_runner_listener.php @@ -26,17 +26,17 @@ use Symfony\Component\HttpKernel\Event\PostResponseEvent; class cron_runner_listener implements EventSubscriberInterface { /** - * @var \phpbb\lock\db + * @var db */ private $cron_lock; /** - * @var \phpbb\cron\manager + * @var manager */ private $cron_manager; /** - * @var \phpbb\request\request_interface + * @var request_interface */ private $request; diff --git a/phpBB/phpbb/cron/manager.php b/phpBB/phpbb/cron/manager.php index 9464b17e76..a8fdb13857 100644 --- a/phpBB/phpbb/cron/manager.php +++ b/phpBB/phpbb/cron/manager.php @@ -80,8 +80,6 @@ class manager * and puts them into $this->tasks. * * @param array|\Traversable $tasks Array of instances of \phpbb\cron\task\task - * - * @return null */ public function load_tasks($tasks) { @@ -94,8 +92,6 @@ class manager /** * Loads registered tasks from the container, wraps them * and puts them into $this->tasks. - * - * @return null */ public function load_tasks_from_container() { @@ -116,7 +112,7 @@ class manager * * If no tasks are ready, null is returned. * - * @return \phpbb\cron\task\wrapper|null + * @return wrapper|null */ public function find_one_ready_task() { @@ -161,7 +157,7 @@ class manager * Web runner uses this method to resolve names to tasks. * * @param string $name Name of the task to look up. - * @return \phpbb\cron\task\wrapper A wrapped task corresponding to the given name, or null. + * @return wrapper A wrapped task corresponding to the given name, or null. */ public function find_task($name) { @@ -193,7 +189,7 @@ class manager * Wraps a task inside an instance of \phpbb\cron\task\wrapper. * * @param \phpbb\cron\task\task $task The task. - * @return \phpbb\cron\task\wrapper The wrapped task. + * @return wrapper The wrapped task. */ public function wrap_task(\phpbb\cron\task\task $task) { diff --git a/phpBB/phpbb/db/extractor/mssql_extractor.php b/phpBB/phpbb/db/extractor/mssql_extractor.php index 4eeab4780e..64922c1124 100644 --- a/phpBB/phpbb/db/extractor/mssql_extractor.php +++ b/phpBB/phpbb/db/extractor/mssql_extractor.php @@ -21,7 +21,7 @@ class mssql_extractor extends base_extractor * Writes closing line(s) to database backup * * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ public function write_end() { @@ -195,7 +195,7 @@ class mssql_extractor extends base_extractor * * @param string $table_name name of the database table * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ protected function write_data_mssqlnative($table_name) { @@ -311,7 +311,7 @@ class mssql_extractor extends base_extractor * * @param string $table_name name of the database table * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ protected function write_data_odbc($table_name) { diff --git a/phpBB/phpbb/db/extractor/mysql_extractor.php b/phpBB/phpbb/db/extractor/mysql_extractor.php index f3cb0db457..6d230a6e35 100644 --- a/phpBB/phpbb/db/extractor/mysql_extractor.php +++ b/phpBB/phpbb/db/extractor/mysql_extractor.php @@ -87,7 +87,7 @@ class mysql_extractor extends base_extractor * * @param string $table_name name of the database table * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ protected function write_data_mysqli($table_name) { @@ -177,7 +177,7 @@ class mysql_extractor extends base_extractor * * @param string $table_name name of the database table * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ protected function new_write_table($table_name) { @@ -202,7 +202,7 @@ class mysql_extractor extends base_extractor * * @param string $table_name name of the database table * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ protected function old_write_table($table_name) { diff --git a/phpBB/phpbb/db/extractor/postgres_extractor.php b/phpBB/phpbb/db/extractor/postgres_extractor.php index 5c569abfdf..07c4d38043 100644 --- a/phpBB/phpbb/db/extractor/postgres_extractor.php +++ b/phpBB/phpbb/db/extractor/postgres_extractor.php @@ -324,7 +324,7 @@ class postgres_extractor extends base_extractor * Writes closing line(s) to database backup * * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ public function write_end() { diff --git a/phpBB/phpbb/db/extractor/sqlite3_extractor.php b/phpBB/phpbb/db/extractor/sqlite3_extractor.php index ce8da6a652..92ce9bdcc6 100644 --- a/phpBB/phpbb/db/extractor/sqlite3_extractor.php +++ b/phpBB/phpbb/db/extractor/sqlite3_extractor.php @@ -136,7 +136,7 @@ class sqlite3_extractor extends base_extractor * Writes closing line(s) to database backup * * @return null - * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor() + * @throws extractor_not_initialized_exception when calling this function before init_extractor() */ public function write_end() { diff --git a/phpBB/phpbb/di/extension/container_configuration.php b/phpBB/phpbb/di/extension/container_configuration.php index 57e7ef6ca6..e1113182b5 100644 --- a/phpBB/phpbb/di/extension/container_configuration.php +++ b/phpBB/phpbb/di/extension/container_configuration.php @@ -22,7 +22,7 @@ class container_configuration implements ConfigurationInterface /** * Generates the configuration tree builder. * - * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder + * @return TreeBuilder The tree builder */ public function getConfigTreeBuilder() { diff --git a/phpBB/phpbb/di/service_collection.php b/phpBB/phpbb/di/service_collection.php index 6298670c42..ef3f90e4bf 100644 --- a/phpBB/phpbb/di/service_collection.php +++ b/phpBB/phpbb/di/service_collection.php @@ -21,7 +21,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class service_collection extends \ArrayObject { /** - * @var \Symfony\Component\DependencyInjection\ContainerInterface + * @var ContainerInterface */ protected $container; diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 50e15c9844..a1ce4ca283 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -59,7 +59,7 @@ class upload /** @var \bantu\IniGetWrapper\IniGetWrapper ini_get() wrapper */ protected $php_ini; - /** @var \phpbb\language\language Language class */ + /** @var language Language class */ protected $language; /** @var request_interface Request class */ diff --git a/phpBB/phpbb/install/console/command/install/install.php b/phpBB/phpbb/install/console/command/install/install.php index 52a348fe44..980586cb0e 100644 --- a/phpBB/phpbb/install/console/command/install/install.php +++ b/phpBB/phpbb/install/console/command/install/install.php @@ -98,7 +98,7 @@ class install extends \phpbb\console\command\command { $this->iohandler_factory->set_environment('cli'); - /** @var \phpbb\install\helper\iohandler\cli_iohandler $iohandler */ + /** @var cli_iohandler $iohandler */ $iohandler = $this->iohandler_factory->get(); $style = new SymfonyStyle($input, $output); $iohandler->set_style($style, $output); diff --git a/phpBB/phpbb/install/console/command/update/update.php b/phpBB/phpbb/install/console/command/update/update.php index e827761d1c..d6e89b2477 100644 --- a/phpBB/phpbb/install/console/command/update/update.php +++ b/phpBB/phpbb/install/console/command/update/update.php @@ -98,7 +98,7 @@ class update extends \phpbb\console\command\command { $this->iohandler_factory->set_environment('cli'); - /** @var \phpbb\install\helper\iohandler\cli_iohandler $iohandler */ + /** @var cli_iohandler $iohandler */ $iohandler = $this->iohandler_factory->get(); $style = new SymfonyStyle($input, $output); $iohandler->set_style($style, $output); diff --git a/phpBB/phpbb/install/controller/helper.php b/phpBB/phpbb/install/controller/helper.php index ba350bea44..68c0222c18 100644 --- a/phpBB/phpbb/install/controller/helper.php +++ b/phpBB/phpbb/install/controller/helper.php @@ -40,7 +40,7 @@ class helper protected $installer_config; /** - * @var \phpbb\language\language + * @var language */ protected $language; @@ -50,37 +50,37 @@ class helper protected $language_cookie; /** - * @var \phpbb\language\language_file_helper + * @var language_file_helper */ protected $lang_helper; /** - * @var \phpbb\install\helper\navigation\navigation_provider + * @var navigation_provider */ protected $navigation_provider; /** - * @var \phpbb\template\template + * @var template */ protected $template; /** - * @var \phpbb\path_helper + * @var path_helper */ protected $path_helper; /** - * @var \phpbb\request\request + * @var request */ protected $phpbb_request; /** - * @var \phpbb\symfony_request + * @var symfony_request */ protected $request; /** - * @var \phpbb\routing\router + * @var router */ protected $router; diff --git a/phpBB/phpbb/install/helper/container_factory.php b/phpBB/phpbb/install/helper/container_factory.php index 077a4fabe5..474b70dec1 100644 --- a/phpBB/phpbb/install/helper/container_factory.php +++ b/phpBB/phpbb/install/helper/container_factory.php @@ -35,7 +35,7 @@ class container_factory protected $php_ext; /** - * @var \phpbb\request\request + * @var request */ protected $request; @@ -78,7 +78,7 @@ class container_factory * @return \Symfony\Component\DependencyInjection\ContainerInterface|Object phpBB's dependency injection container * or the service specified in $service_name * - * @throws \phpbb\install\exception\cannot_build_container_exception When container cannot be built + * @throws cannot_build_container_exception When container cannot be built * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException If the service is not defined * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException When a circular reference is detected * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException When the service is not defined @@ -101,7 +101,7 @@ class container_factory * * @return mixed * - * @throws \phpbb\install\exception\cannot_build_container_exception When container cannot be built + * @throws cannot_build_container_exception When container cannot be built */ public function get_parameter($param_name) { @@ -117,7 +117,7 @@ class container_factory /** * Build dependency injection container * - * @throws \phpbb\install\exception\cannot_build_container_exception When container cannot be built + * @throws cannot_build_container_exception When container cannot be built */ protected function build_container() { diff --git a/phpBB/phpbb/install/helper/database.php b/phpBB/phpbb/install/helper/database.php index 51fd18f874..8142d9284d 100644 --- a/phpBB/phpbb/install/helper/database.php +++ b/phpBB/phpbb/install/helper/database.php @@ -230,7 +230,7 @@ class database * * @return bool|array true if table prefix is valid, array of errors otherwise * - * @throws \phpbb\install\exception\invalid_dbms_exception When $dbms is not a valid + * @throws invalid_dbms_exception When $dbms is not a valid */ public function validate_table_prefix($dbms, $table_prefix) { diff --git a/phpBB/phpbb/install/helper/iohandler/factory.php b/phpBB/phpbb/install/helper/iohandler/factory.php index 1e8395760a..ec095729c8 100644 --- a/phpBB/phpbb/install/helper/iohandler/factory.php +++ b/phpBB/phpbb/install/helper/iohandler/factory.php @@ -54,7 +54,7 @@ class factory * * @return \phpbb\install\helper\iohandler\iohandler_interface * - * @throws \phpbb\install\helper\iohandler\exception\iohandler_not_implemented_exception + * @throws iohandler_not_implemented_exception * When the specified iohandler_interface does not exists */ public function get() diff --git a/phpBB/phpbb/install/installer.php b/phpBB/phpbb/install/installer.php index e04e233a76..77aa98cf96 100644 --- a/phpBB/phpbb/install/installer.php +++ b/phpBB/phpbb/install/installer.php @@ -137,7 +137,7 @@ class installer if (!$this->install_config->get('cache_purged_before', false) && $this->purge_cache_before) { - /** @var \phpbb\cache\driver\driver_interface $cache */ + /** @var driver_interface $cache */ $cache = $this->container_factory->get('cache.driver'); $cache->purge(); $this->install_config->set('cache_purged_before', true); @@ -311,7 +311,7 @@ class installer try { - /** @var \phpbb\cache\driver\driver_interface $cache */ + /** @var driver_interface $cache */ $cache = $this->container_factory->get('cache.driver'); $cache->purge(); } diff --git a/phpBB/phpbb/install/installer_configuration.php b/phpBB/phpbb/install/installer_configuration.php index dfafc40b4b..efd0229571 100644 --- a/phpBB/phpbb/install/installer_configuration.php +++ b/phpBB/phpbb/install/installer_configuration.php @@ -22,7 +22,7 @@ class installer_configuration implements ConfigurationInterface /** * Generates the configuration tree builder. * - * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder + * @return TreeBuilder The tree builder */ public function getConfigTreeBuilder() { diff --git a/phpBB/phpbb/install/module/install_data/task/add_modules.php b/phpBB/phpbb/install/module/install_data/task/add_modules.php index b64f4c31db..6f9f37e338 100644 --- a/phpBB/phpbb/install/module/install_data/task/add_modules.php +++ b/phpBB/phpbb/install/module/install_data/task/add_modules.php @@ -36,7 +36,7 @@ class add_modules extends \phpbb\install\task_base protected $extension_manager; /** - * @var \phpbb\install\helper\iohandler\iohandler_interface + * @var iohandler_interface */ protected $iohandler; diff --git a/phpBB/phpbb/install/module/install_finish/task/notify_user.php b/phpBB/phpbb/install/module/install_finish/task/notify_user.php index b045b708db..8bef7136e7 100644 --- a/phpBB/phpbb/install/module/install_finish/task/notify_user.php +++ b/phpBB/phpbb/install/module/install_finish/task/notify_user.php @@ -36,7 +36,7 @@ class notify_user extends \phpbb\install\task_base protected $auth; /** - * @var \phpbb\config\db + * @var db */ protected $config; diff --git a/phpBB/phpbb/install/module/obtain_data/task/obtain_admin_data.php b/phpBB/phpbb/install/module/obtain_data/task/obtain_admin_data.php index d1f1af6b83..c759f169ed 100644 --- a/phpBB/phpbb/install/module/obtain_data/task/obtain_admin_data.php +++ b/phpBB/phpbb/install/module/obtain_data/task/obtain_admin_data.php @@ -91,7 +91,7 @@ class obtain_admin_data extends \phpbb\install\task_base implements \phpbb\insta * * @param bool $use_request_data Whether to use submited data * - * @throws \phpbb\install\exception\user_interaction_required_exception When the user is required to provide data + * @throws user_interaction_required_exception When the user is required to provide data */ protected function request_form_data($use_request_data = false) { diff --git a/phpBB/phpbb/install/module/obtain_data/task/obtain_board_data.php b/phpBB/phpbb/install/module/obtain_data/task/obtain_board_data.php index ff2a0a2f86..ead853bebf 100644 --- a/phpBB/phpbb/install/module/obtain_data/task/obtain_board_data.php +++ b/phpBB/phpbb/install/module/obtain_data/task/obtain_board_data.php @@ -110,7 +110,7 @@ class obtain_board_data extends \phpbb\install\task_base implements \phpbb\insta * * @param bool $use_request_data Whether to use submited data * - * @throws \phpbb\install\exception\user_interaction_required_exception When the user is required to provide data + * @throws user_interaction_required_exception When the user is required to provide data */ protected function request_form_data($use_request_data = false) { diff --git a/phpBB/phpbb/install/module/obtain_data/task/obtain_database_data.php b/phpBB/phpbb/install/module/obtain_data/task/obtain_database_data.php index 6ec1e612b9..d59cfa3de0 100644 --- a/phpBB/phpbb/install/module/obtain_data/task/obtain_database_data.php +++ b/phpBB/phpbb/install/module/obtain_data/task/obtain_database_data.php @@ -108,7 +108,7 @@ class obtain_database_data extends \phpbb\install\task_base implements \phpbb\in * * @param bool $use_request_data Whether to use submited data * - * @throws \phpbb\install\exception\user_interaction_required_exception When the user is required to provide data + * @throws user_interaction_required_exception When the user is required to provide data */ protected function request_form_data($use_request_data = false) { diff --git a/phpBB/phpbb/install/module/obtain_data/task/obtain_file_updater_method.php b/phpBB/phpbb/install/module/obtain_data/task/obtain_file_updater_method.php index d5a8855c37..b22137a515 100644 --- a/phpBB/phpbb/install/module/obtain_data/task/obtain_file_updater_method.php +++ b/phpBB/phpbb/install/module/obtain_data/task/obtain_file_updater_method.php @@ -28,12 +28,12 @@ class obtain_file_updater_method extends task_base protected $available_methods; /** - * @var \phpbb\install\helper\config + * @var config */ protected $installer_config; /** - * @var \phpbb\install\helper\iohandler\iohandler_interface + * @var iohandler_interface */ protected $iohandler; diff --git a/phpBB/phpbb/install/module/obtain_data/task/obtain_update_ftp_data.php b/phpBB/phpbb/install/module/obtain_data/task/obtain_update_ftp_data.php index 3c17576c13..d94cc4b5fd 100644 --- a/phpBB/phpbb/install/module/obtain_data/task/obtain_update_ftp_data.php +++ b/phpBB/phpbb/install/module/obtain_data/task/obtain_update_ftp_data.php @@ -22,12 +22,12 @@ use phpbb\install\task_base; class obtain_update_ftp_data extends task_base { /** - * @var \phpbb\install\helper\config + * @var config */ protected $installer_config; /** - * @var \phpbb\install\helper\iohandler\iohandler_interface + * @var iohandler_interface */ protected $iohandler; diff --git a/phpBB/phpbb/install/module/obtain_data/task/obtain_update_settings.php b/phpBB/phpbb/install/module/obtain_data/task/obtain_update_settings.php index 3b24e8ba40..9da1872d69 100644 --- a/phpBB/phpbb/install/module/obtain_data/task/obtain_update_settings.php +++ b/phpBB/phpbb/install/module/obtain_data/task/obtain_update_settings.php @@ -21,12 +21,12 @@ use phpbb\install\task_base; class obtain_update_settings extends task_base { /** - * @var \phpbb\install\helper\config + * @var config */ protected $installer_config; /** - * @var \phpbb\install\helper\iohandler\iohandler_interface + * @var iohandler_interface */ protected $iohandler; diff --git a/phpBB/phpbb/install/updater_configuration.php b/phpBB/phpbb/install/updater_configuration.php index 5c1c29f1da..9795ed2cd9 100644 --- a/phpBB/phpbb/install/updater_configuration.php +++ b/phpBB/phpbb/install/updater_configuration.php @@ -22,7 +22,7 @@ class updater_configuration implements ConfigurationInterface /** * Generates the configuration tree builder. * - * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder + * @return TreeBuilder The tree builder */ public function getConfigTreeBuilder() { diff --git a/phpBB/phpbb/language/language.php b/phpBB/phpbb/language/language.php index 031f221034..fb50eba08e 100644 --- a/phpBB/phpbb/language/language.php +++ b/phpBB/phpbb/language/language.php @@ -399,7 +399,7 @@ class language * * @return int The plural-case we need to use for the number plural-rule combination * - * @throws \phpbb\language\exception\invalid_plural_rule_exception When $force_rule has an invalid value + * @throws invalid_plural_rule_exception When $force_rule has an invalid value */ public function get_plural_form($number, $force_rule = false) { diff --git a/phpBB/phpbb/module/module_manager.php b/phpBB/phpbb/module/module_manager.php index 00df33f62f..e17af7b220 100644 --- a/phpBB/phpbb/module/module_manager.php +++ b/phpBB/phpbb/module/module_manager.php @@ -76,7 +76,7 @@ class module_manager * * @return array Array of data fetched from the database * - * @throws \phpbb\module\exception\module_not_found_exception When there is no module with $module_id + * @throws module_not_found_exception When there is no module with $module_id */ public function get_module_row($module_id, $module_class) { @@ -243,7 +243,7 @@ class module_manager * * @param array &$module_data The module data * - * @throws \phpbb\module\exception\module_not_found_exception When parent module or the category is not exist + * @throws module_not_found_exception When parent module or the category is not exist */ public function update_module_data(&$module_data) { @@ -340,8 +340,8 @@ class module_manager * @param int $to_parent_id ID of the target parent module * @param string $module_class Class of the module (acp, ucp, mcp etc...) * - * @throws \phpbb\module\exception\module_not_found_exception If the module specified to move modules from does not - * have any children. + * @throws module_not_found_exception If the module specified to move modules from does not + * have any children. */ public function move_module($from_module_id, $to_parent_id, $module_class) { @@ -433,7 +433,7 @@ class module_manager * @param int $module_id ID of the module to delete * @param string $module_class Class of the module (acp, ucp, mcp etc...) * - * @throws \phpbb\module\exception\module_exception When the specified module cannot be removed + * @throws module_exception When the specified module cannot be removed */ public function delete_module($module_id, $module_class) { @@ -482,7 +482,7 @@ class module_manager * * @return string Returns the language name of the module * - * @throws \phpbb\module\exception\module_not_found_exception When the specified module does not exists + * @throws module_not_found_exception When the specified module does not exists */ public function move_module_by($module_row, $module_class, $action = 'move_up', $steps = 1) { diff --git a/phpBB/phpbb/notification/method/email.php b/phpBB/phpbb/notification/method/email.php index a9717e70df..cc115f05ce 100644 --- a/phpBB/phpbb/notification/method/email.php +++ b/phpBB/phpbb/notification/method/email.php @@ -108,7 +108,7 @@ class email extends \phpbb\notification\method\messenger_base { $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->notification_emails_table); - /** @var \phpbb\notification\type\type_interface $notification */ + /** @var type_interface $notification */ foreach ($this->queue as $notification) { $data = self::clean_data($notification->get_insert_array()); diff --git a/phpBB/phpbb/notification/method/messenger_base.php b/phpBB/phpbb/notification/method/messenger_base.php index f82017b70e..72163e75ea 100644 --- a/phpBB/phpbb/notification/method/messenger_base.php +++ b/phpBB/phpbb/notification/method/messenger_base.php @@ -97,7 +97,7 @@ abstract class messenger_base extends \phpbb\notification\method\base $messenger = new \messenger(); // Time to go through the queue and send emails - /** @var \phpbb\notification\type\type_interface $notification */ + /** @var type_interface $notification */ foreach ($this->queue as $notification) { if ($notification->get_email_template() === false) diff --git a/phpBB/phpbb/report/controller/report.php b/phpBB/phpbb/report/controller/report.php index a8f4abf338..8f1cf65704 100644 --- a/phpBB/phpbb/report/controller/report.php +++ b/phpBB/phpbb/report/controller/report.php @@ -92,7 +92,7 @@ class report * @param int $id ID of the entity to report * @param string $mode * @return \Symfony\Component\HttpFoundation\Response a Symfony response object - * @throws \phpbb\exception\http_exception when $mode or $id is invalid for some reason + * @throws http_exception when $mode or $id is invalid for some reason */ public function handle($id, $mode) { diff --git a/phpBB/phpbb/report/handler_factory.php b/phpBB/phpbb/report/handler_factory.php index ec229aac54..b25386c4b2 100644 --- a/phpBB/phpbb/report/handler_factory.php +++ b/phpBB/phpbb/report/handler_factory.php @@ -37,7 +37,7 @@ class handler_factory * * @param string $type * @return \phpbb\report\report_handler_interface - * @throws \phpbb\report\exception\factory_invalid_argument_exception if $type is not valid + * @throws factory_invalid_argument_exception if $type is not valid */ public function get_instance($type) { diff --git a/phpBB/phpbb/report/report_handler.php b/phpBB/phpbb/report/report_handler.php index 97acc1763e..ec2f1e035f 100644 --- a/phpBB/phpbb/report/report_handler.php +++ b/phpBB/phpbb/report/report_handler.php @@ -55,7 +55,7 @@ abstract class report_handler implements report_handler_interface * * @param \phpbb\db\driver\driver_interface $db * @param \phpbb\event\dispatcher_interface $dispatcher - * @param \phpbb\config\db $config + * @param \phpbb\config\config $config * @param \phpbb\auth\auth $auth * @param \phpbb\user $user * @param \phpbb\notification\manager $notification diff --git a/phpBB/phpbb/report/report_handler_pm.php b/phpBB/phpbb/report/report_handler_pm.php index 774ca329ad..1350ccccb0 100644 --- a/phpBB/phpbb/report/report_handler_pm.php +++ b/phpBB/phpbb/report/report_handler_pm.php @@ -22,7 +22,7 @@ class report_handler_pm extends report_handler { /** * {@inheritdoc} - * @throws \phpbb\report\exception\pm_reporting_disabled_exception when PM reporting is disabled on the board + * @throws pm_reporting_disabled_exception when PM reporting is disabled on the board */ public function add_report($id, $reason_id, $report_text, $user_notify) { @@ -92,7 +92,7 @@ class report_handler_pm extends report_handler /** * {@inheritdoc} - * @throws \phpbb\report\exception\pm_reporting_disabled_exception when PM reporting is disabled on the board + * @throws pm_reporting_disabled_exception when PM reporting is disabled on the board */ public function validate_report_request($id) { diff --git a/phpBB/phpbb/report/report_handler_post.php b/phpBB/phpbb/report/report_handler_post.php index 52f09683ce..814fd3d8bc 100644 --- a/phpBB/phpbb/report/report_handler_post.php +++ b/phpBB/phpbb/report/report_handler_post.php @@ -28,7 +28,7 @@ class report_handler_post extends report_handler /** * {@inheritdoc} - * @throws \phpbb\report\exception\report_permission_denied_exception when the user does not have permission to report the post + * @throws report_permission_denied_exception when the user does not have permission to report the post */ public function add_report($id, $reason_id, $report_text, $user_notify) { @@ -89,7 +89,7 @@ class report_handler_post extends report_handler /** * {@inheritdoc} - * @throws \phpbb\report\exception\report_permission_denied_exception when the user does not have permission to report the post + * @throws report_permission_denied_exception when the user does not have permission to report the post */ public function validate_report_request($id) { diff --git a/phpBB/phpbb/template/twig/twig.php b/phpBB/phpbb/template/twig/twig.php index af77335b9d..644cea49b3 100644 --- a/phpBB/phpbb/template/twig/twig.php +++ b/phpBB/phpbb/template/twig/twig.php @@ -129,7 +129,7 @@ class twig extends \phpbb\template\base * * @return array Style tree, most specific first * - * @throws \phpbb\template\exception\user_object_not_available When user service was not set + * @throws user_object_not_available When user service was not set */ public function get_user_style() { diff --git a/phpBB/phpbb/textformatter/s9e/bbcode_merger.php b/phpBB/phpbb/textformatter/s9e/bbcode_merger.php index d1bedb0b72..8173037455 100644 --- a/phpBB/phpbb/textformatter/s9e/bbcode_merger.php +++ b/phpBB/phpbb/textformatter/s9e/bbcode_merger.php @@ -25,7 +25,7 @@ class bbcode_merger protected $configurator; /** - * @param \phpbb\textformatter\s9e\factory $factory + * @param factory $factory */ public function __construct(factory $factory) { diff --git a/phpBB/phpbb/textformatter/s9e/factory.php b/phpBB/phpbb/textformatter/s9e/factory.php index 2285d99eb8..721549cf72 100644 --- a/phpBB/phpbb/textformatter/s9e/factory.php +++ b/phpBB/phpbb/textformatter/s9e/factory.php @@ -207,7 +207,7 @@ class factory implements \phpbb\textformatter\cache_interface * Modify the s9e\TextFormatter configurator before the default settings are set * * @event core.text_formatter_s9e_configure_before - * @var \s9e\TextFormatter\Configurator configurator Configurator instance + * @var Configurator configurator Configurator instance * @since 3.2.0-a1 */ $vars = array('configurator'); @@ -368,7 +368,7 @@ class factory implements \phpbb\textformatter\cache_interface * Modify the s9e\TextFormatter configurator after the default settings are set * * @event core.text_formatter_s9e_configure_after - * @var \s9e\TextFormatter\Configurator configurator Configurator instance + * @var Configurator configurator Configurator instance * @since 3.2.0-a1 */ $vars = array('configurator'); @@ -446,7 +446,7 @@ class factory implements \phpbb\textformatter\cache_interface /** * Configure the Autolink / Autoemail plugins used to linkify text * - * @param \s9e\TextFormatter\Configurator $configurator + * @param Configurator $configurator * @return void */ protected function configure_autolink(Configurator $configurator) From 3d8f7ee33b528b285a029a6a457f48837e322b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Bartus?= Date: Thu, 31 Dec 2020 14:49:49 +0100 Subject: [PATCH 3/6] [ticket/16671] Do not allow leading backslashes in use statements PHPBB3-16671 --- .../code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index f7f6300df5..fedbef3a28 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -86,6 +86,11 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $class_name_short = $tokens[$class_name_end - 1]['content']; } + if ($class_name_full[0] === '\\') + { + $phpcsFile->addError("There must not be a leading '\\' in use statements.", $stackPtr, 'Malformed'); + } + $ok = false; // Checks in simple statements (new, instanceof and extends) From b3e7c01ab6ed989332dbc1f70c6e2f2e29ea457e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Bartus?= Date: Thu, 31 Dec 2020 14:50:28 +0100 Subject: [PATCH 4/6] [ticket/16671] Fix coding style PHPBB3-166671 --- phpBB/phpbb/attachment/delete.php | 8 ++++---- phpBB/phpbb/attachment/resync.php | 2 +- phpBB/phpbb/attachment/upload.php | 14 +++++++------- phpBB/phpbb/controller/helper.php | 2 +- phpBB/phpbb/db/driver/factory.php | 2 +- phpBB/phpbb/language/language_file_loader.php | 2 +- .../factory_invalid_argument_exception.php | 2 +- .../report/exception/invalid_report_exception.php | 2 +- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/phpBB/phpbb/attachment/delete.php b/phpBB/phpbb/attachment/delete.php index 3c98e21587..bca7f735f5 100644 --- a/phpBB/phpbb/attachment/delete.php +++ b/phpBB/phpbb/attachment/delete.php @@ -13,10 +13,10 @@ namespace phpbb\attachment; -use \phpbb\config\config; -use \phpbb\db\driver\driver_interface; -use \phpbb\event\dispatcher; -use \phpbb\filesystem\filesystem; +use phpbb\config\config; +use phpbb\db\driver\driver_interface; +use phpbb\event\dispatcher; +use phpbb\filesystem\filesystem; /** * Attachment delete class diff --git a/phpBB/phpbb/attachment/resync.php b/phpBB/phpbb/attachment/resync.php index aeacf82511..4e30e94e2d 100644 --- a/phpBB/phpbb/attachment/resync.php +++ b/phpBB/phpbb/attachment/resync.php @@ -13,7 +13,7 @@ namespace phpbb\attachment; -use \phpbb\db\driver\driver_interface; +use phpbb\db\driver\driver_interface; /** * Attachment resync class diff --git a/phpBB/phpbb/attachment/upload.php b/phpBB/phpbb/attachment/upload.php index b9d32058db..ca83f9dabf 100644 --- a/phpBB/phpbb/attachment/upload.php +++ b/phpBB/phpbb/attachment/upload.php @@ -14,13 +14,13 @@ namespace phpbb\attachment; use phpbb\auth\auth; -use \phpbb\cache\service; -use \phpbb\config\config; -use \phpbb\event\dispatcher; -use \phpbb\language\language; -use \phpbb\mimetype\guesser; -use \phpbb\plupload\plupload; -use \phpbb\user; +use phpbb\cache\service; +use phpbb\config\config; +use phpbb\event\dispatcher; +use phpbb\language\language; +use phpbb\mimetype\guesser; +use phpbb\plupload\plupload; +use phpbb\user; /** * Attachment upload class diff --git a/phpBB/phpbb/controller/helper.php b/phpBB/phpbb/controller/helper.php index 93a243d66a..3262e6bbc4 100644 --- a/phpBB/phpbb/controller/helper.php +++ b/phpBB/phpbb/controller/helper.php @@ -14,7 +14,7 @@ namespace phpbb\controller; use phpbb\auth\auth; -use \phpbb\cache\driver\driver_interface as cache_interface; +use phpbb\cache\driver\driver_interface as cache_interface; use phpbb\config\config; use phpbb\cron\manager; use phpbb\db\driver\driver_interface; diff --git a/phpBB/phpbb/db/driver/factory.php b/phpBB/phpbb/db/driver/factory.php index db58897b88..b2a5707120 100644 --- a/phpBB/phpbb/db/driver/factory.php +++ b/phpBB/phpbb/db/driver/factory.php @@ -13,7 +13,7 @@ namespace phpbb\db\driver; -use \Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Database Abstraction Layer diff --git a/phpBB/phpbb/language/language_file_loader.php b/phpBB/phpbb/language/language_file_loader.php index 2910dd3c4a..6eb0e45756 100644 --- a/phpBB/phpbb/language/language_file_loader.php +++ b/phpBB/phpbb/language/language_file_loader.php @@ -13,7 +13,7 @@ namespace phpbb\language; -use \phpbb\language\exception\language_file_not_found; +use phpbb\language\exception\language_file_not_found; /** * Language file loader diff --git a/phpBB/phpbb/report/exception/factory_invalid_argument_exception.php b/phpBB/phpbb/report/exception/factory_invalid_argument_exception.php index 19de91eea3..346dd36316 100644 --- a/phpBB/phpbb/report/exception/factory_invalid_argument_exception.php +++ b/phpBB/phpbb/report/exception/factory_invalid_argument_exception.php @@ -13,7 +13,7 @@ namespace phpbb\report\exception; -use \phpbb\exception\runtime_exception; +use phpbb\exception\runtime_exception; class factory_invalid_argument_exception extends runtime_exception { diff --git a/phpBB/phpbb/report/exception/invalid_report_exception.php b/phpBB/phpbb/report/exception/invalid_report_exception.php index 03ff0a872d..27996de5e1 100644 --- a/phpBB/phpbb/report/exception/invalid_report_exception.php +++ b/phpBB/phpbb/report/exception/invalid_report_exception.php @@ -13,7 +13,7 @@ namespace phpbb\report\exception; -use \phpbb\exception\runtime_exception; +use phpbb\exception\runtime_exception; class invalid_report_exception extends runtime_exception { From fd8cb2a0e8370d0e8bb758ec97b9524fa2f53e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Bartus?= Date: Thu, 31 Dec 2020 15:22:50 +0100 Subject: [PATCH 5/6] [ticket/16671] Allow using objects from the global namespace PHPBB3-16671 --- .../phpbb/Sniffs/Namespaces/UnusedUseSniff.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index fedbef3a28..1b1c9935ee 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -29,18 +29,18 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff protected function check(File $phpcsFile, $found_name, $full_name, $short_name, $stack_pointer) { - $found_name = ltrim($found_name, '\\'); + $found_name_normalized = ltrim($found_name, '\\'); $full_name = ltrim($full_name, '\\'); - if ($found_name === $full_name) + $is_global = ($full_name === $short_name); + $unnecessarily_fully_qualified = ($is_global) + ? ($found_name_normalized !== $found_name && $found_name_normalized === $short_name) + : ($found_name_normalized === $full_name); + + if ($unnecessarily_fully_qualified) { $error = 'Either use statement or full name must be used.'; $phpcsFile->addError($error, $stack_pointer, 'FullName'); - - if (strpos($phpcsFile->getFilename(), 'cron/manager.php') !== false) - { - print("$found_name, $full_name, $short_name}\n"); - } } if ($found_name === $short_name) From 0208dbf98585d8a5bb50fa14808192d17a56f314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Bartus?= Date: Fri, 1 Jan 2021 11:46:23 +0100 Subject: [PATCH 6/6] [ticket/16671] Allow importing functions through use statements PHPBB3-16671 --- .../Sniffs/Namespaces/UnusedUseSniff.php | 125 +++++++++++++++--- 1 file changed, 106 insertions(+), 19 deletions(-) diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index 1b1c9935ee..78df9766dc 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -19,6 +19,12 @@ use PHP_CodeSniffer\Sniffs\Sniff; */ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff { + const FIND = [ + T_NS_SEPARATOR, + T_STRING, + T_WHITESPACE, + ]; + /** * {@inheritdoc} */ @@ -65,32 +71,50 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $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); + $class_name_end = $phpcsFile->findNext(self::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)); + $name_short = $tokens[$alias_position]['content']; + $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']; + $name_full = $phpcsFile->getTokensAsString($class_name_start, ($class_name_end - $class_name_start)); + $name_short = $tokens[$class_name_end - 1]['content']; } - if ($class_name_full[0] === '\\') + if ($tokens[$class_name_start]['content'] === 'function' + && $tokens[$class_name_start + 1]['code'] === T_WHITESPACE) + { + $class_name_start += 2; + $name_full = $phpcsFile->getTokensAsString( + $class_name_start, + ($class_name_end - $class_name_start - (int) ($aliasing_as_position !== false)) + ); + $ok = $this->findFunctionUsage($phpcsFile, $stackPtr, $tokens, $name_full, $name_short); + } + else + { + $ok = $this->findClassUsage($phpcsFile, $stackPtr, $tokens, $name_full, $name_short); + } + + if ($name_full[0] === '\\') { $phpcsFile->addError("There must not be a leading '\\' in use statements.", $stackPtr, 'Malformed'); } + if (!$ok) + { + $error = 'There must not be unused USE statements.'; + $phpcsFile->addError($error, $stackPtr, 'Unused'); + } + } + + private function findClassUsage(File $phpcsFile, $stackPtr, $tokens, $class_name_full, $class_name_short) + { $ok = false; // Checks in simple statements (new, instanceof and extends) @@ -107,7 +131,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff continue; } - $simple_class_name_end = $phpcsFile->findNext($find, ($simple_statement + 1), null, true); + $simple_class_name_end = $phpcsFile->findNext(self::FIND, ($simple_statement + 1), null, true); $simple_class_name = trim($phpcsFile->getTokensAsString($simple_class_name_start, ($simple_class_name_end - $simple_class_name_start))); @@ -121,7 +145,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff { $old_paamayim_nekudotayim = $paamayim_nekudotayim; - $paamayim_nekudotayim_class_name_start = $phpcsFile->findPrevious($find, $paamayim_nekudotayim - 1, null, true); + $paamayim_nekudotayim_class_name_start = $phpcsFile->findPrevious(self::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))); @@ -141,7 +165,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $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_end = $phpcsFile->findNext(self::FIND, ($implemented_class - 1), null, true); $implements_class_name = trim($phpcsFile->getTokensAsString($implements_class_name_start, ($implements_class_name_end - $implements_class_name_start))); @@ -177,18 +201,81 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff $old_catch = $catch; $caught_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), $catch + 1); - $caught_class_name_end = $phpcsFile->findNext($find, $caught_class_name_start + 1, null, true); + $caught_class_name_end = $phpcsFile->findNext(self::FIND, $caught_class_name_start + 1, null, true); $caught_class_name = trim($phpcsFile->getTokensAsString($caught_class_name_start, ($caught_class_name_end - $caught_class_name_start))); $ok = $this->check($phpcsFile, $caught_class_name, $class_name_full, $class_name_short, $catch) || $ok; } - if (!$ok) + return $ok; + } + + private function findFunctionUsage(File $phpcsFile, $stackPtr, $tokens, $name_full, $name_short) + { + $ok = false; + $position = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr + 1); + while ($position !== false) { - $error = 'There must not be unused USE statements.'; - $phpcsFile->addError($error, $stackPtr, 'Unused'); + $function_name_end = $position; + $found_start = 1 + $phpcsFile->findPrevious( + [T_NS_SEPARATOR, T_STRING, T_WHITESPACE], + $function_name_end - 1, + null, + true + ); + + $position = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $position + 1); + if ($found_start === null) + { + continue; + } + + $function_name_start = $found_start; + + // Trim the output. + while ($tokens[$function_name_start]['code'] === T_WHITESPACE && $function_name_start < $function_name_end) + { + ++$function_name_start; + } + + while ($tokens[$function_name_end]['code'] === T_WHITESPACE && $function_name_end > $function_name_start) + { + --$function_name_end; + } + + $function_name_length = $function_name_end - $function_name_start; + + // Filter out control structures, built in type constructors, etc. + if ($function_name_length <= 0) + { + continue; + } + + // This doesn't seem to be valid PHP, where is the opening tag? + if ($found_start === 0) + { + continue; + } + + $previous_token = $found_start - 1; + $filter = [ + T_FUNCTION, // Function declaration + T_OBJECT_OPERATOR, // Method call + T_DOUBLE_COLON, // Static method call + ]; + + // Filter out calls to methods and function declarations. + if (in_array($tokens[$previous_token]['code'], $filter)) + { + continue; + } + + $function_name = $phpcsFile->getTokensAsString($function_name_start, $function_name_length); + $ok = $this->check($phpcsFile, $function_name, $name_full, $name_short, $function_name_start) || $ok; } + + return $ok; } /**