From abd29e96b531cf465628473a2ca97b71c5599790 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 19 Aug 2023 22:05:37 +0200 Subject: [PATCH] [ticket/17176] Resolve deprecation notices PHPBB3-17176 --- phpBB/includes/utf/utf_tools.php | 11 ++++++++--- phpBB/phpbb/config/config.php | 4 ++-- phpBB/phpbb/controller/resolver.php | 2 +- phpBB/phpbb/db/migrator.php | 10 +++++----- phpBB/phpbb/di/extension/config.php | 2 +- phpBB/phpbb/di/ordered_service_collection.php | 10 +++++----- phpBB/phpbb/di/service_collection.php | 6 +++--- phpBB/phpbb/di/service_collection_iterator.php | 2 +- .../event/recursive_event_filter_iterator.php | 4 ++-- phpBB/phpbb/files/filespec.php | 2 +- phpBB/phpbb/files/filespec_storage.php | 2 +- phpBB/phpbb/filesystem/filesystem.php | 2 +- phpBB/phpbb/finder/finder.php | 2 +- phpBB/phpbb/notification/type/base.php | 2 +- phpBB/phpbb/profilefields/type/type_date.php | 4 ++-- phpBB/phpbb/profilefields/type/type_int.php | 2 +- .../profilefields/type/type_string_common.php | 8 ++------ phpBB/phpbb/request/deactivated_super_global.php | 14 ++++++++------ phpBB/phpbb/session.php | 2 +- tests/mock/lang.php | 10 +++++----- tests/mock/request.php | 6 +++--- tests/notification/submit_post_base.php | 4 ++-- 22 files changed, 57 insertions(+), 54 deletions(-) diff --git a/phpBB/includes/utf/utf_tools.php b/phpBB/includes/utf/utf_tools.php index 5a1ca5b6fe..b8c35a5048 100644 --- a/phpBB/includes/utf/utf_tools.php +++ b/phpBB/includes/utf/utf_tools.php @@ -1448,17 +1448,22 @@ function utf8_wordwrap($string, $width = 75, $break = "\n", $cut = false) * @param string $filename The filename basename() should be applied to * @return string The basenamed filename */ -function utf8_basename($filename) +function utf8_basename($filename): string { + if (!$filename) + { + return ''; + } + // We always check for forward slash AND backward slash // because they could be mixed or "sneaked" in. ;) // You know, never trust user input... - if (strpos($filename, '/') !== false) + if (str_contains($filename, '/')) { $filename = utf8_substr($filename, utf8_strrpos($filename, '/') + 1); } - if (strpos($filename, '\\') !== false) + if (str_contains($filename, '\\')) { $filename = utf8_substr($filename, utf8_strrpos($filename, '\\') + 1); } diff --git a/phpBB/phpbb/config/config.php b/phpBB/phpbb/config/config.php index 92d6857c32..ae2ce658f2 100644 --- a/phpBB/phpbb/config/config.php +++ b/phpBB/phpbb/config/config.php @@ -39,7 +39,7 @@ class config implements \ArrayAccess, \IteratorAggregate, \Countable * * @return \ArrayIterator An iterator over all config data */ - public function getIterator() + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->config); } @@ -99,7 +99,7 @@ class config implements \ArrayAccess, \IteratorAggregate, \Countable * * @return int Number of config options */ - public function count() + public function count(): int { return count($this->config); } diff --git a/phpBB/phpbb/controller/resolver.php b/phpBB/phpbb/controller/resolver.php index 087c1e89b9..e2d3e2f193 100644 --- a/phpBB/phpbb/controller/resolver.php +++ b/phpBB/phpbb/controller/resolver.php @@ -160,7 +160,7 @@ class resolver implements ControllerResolverInterface $arguments[] = $attributes[$param->name]; } } - else if ($param->getClass() && $param->getClass()->isInstance($request)) + else if ($param->getType() && $param->getType() instanceof $request) { $arguments[] = $request; } diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index 95bc88423e..0df548ca90 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -91,9 +91,9 @@ class migrator * * 'effectively_installed' set and set to true if the migration was effectively_installed * - * @var array|false + * @var array */ - protected $last_run_migration = false; + protected $last_run_migration = []; /** * The output handler. A null handler is configured by default. @@ -193,9 +193,9 @@ class migrator * The array contains 'name', 'class' and 'state'. 'effectively_installed' is set * and set to true if the last migration was effectively_installed. * - * @return array|false Last run migration information or false if no migration has been run yet + * @return array Last run migration information or false if no migration has been run yet */ - public function get_last_run_migration() + public function get_last_run_migration(): array { return $this->last_run_migration; } @@ -630,7 +630,7 @@ class migrator WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; $this->db->sql_query($sql); - $this->last_run_migration = false; + $this->last_run_migration = []; unset($this->migration_state[$name]); $this->output_handler->write(array('MIGRATION_REVERT_SCHEMA_DONE', $name, $total_time), migrator_output_handler_interface::VERBOSITY_NORMAL); diff --git a/phpBB/phpbb/di/extension/config.php b/phpBB/phpbb/di/extension/config.php index 2b73a4871a..041ac20708 100644 --- a/phpBB/phpbb/di/extension/config.php +++ b/phpBB/phpbb/di/extension/config.php @@ -73,7 +73,7 @@ class config extends Extension */ protected function convert_30_acm_type($acm_type) { - if (preg_match('#^[a-z]+$#', $acm_type)) + if (preg_match('#^[a-z]+$#', $acm_type ?? '')) { return 'phpbb\\cache\\driver\\' . $acm_type; } diff --git a/phpBB/phpbb/di/ordered_service_collection.php b/phpBB/phpbb/di/ordered_service_collection.php index 57b674b986..71f2a089b6 100644 --- a/phpBB/phpbb/di/ordered_service_collection.php +++ b/phpBB/phpbb/di/ordered_service_collection.php @@ -46,7 +46,7 @@ class ordered_service_collection extends service_collection /** * {@inheritdoc} */ - public function getIterator() + public function getIterator(): \Iterator { if (!$this->is_ordered) { @@ -59,27 +59,27 @@ class ordered_service_collection extends service_collection /** * {@inheritdoc} */ - public function offsetExists($index) + public function offsetExists($key): bool { if (!$this->is_ordered) { $this->sort_services(); } - return parent::offsetExists($index); + return parent::offsetExists($key); } /** * {@inheritdoc} */ - public function offsetGet($index) + public function offsetGet($key): mixed { if (!$this->is_ordered) { $this->sort_services(); } - return parent::offsetGet($index); + return parent::offsetGet($key); } /** diff --git a/phpBB/phpbb/di/service_collection.php b/phpBB/phpbb/di/service_collection.php index 5c108bbb84..b6f5af5ed9 100644 --- a/phpBB/phpbb/di/service_collection.php +++ b/phpBB/phpbb/di/service_collection.php @@ -46,7 +46,7 @@ class service_collection extends \ArrayObject /** * {@inheritdoc} */ - public function getIterator() + public function getIterator(): \Iterator { return new service_collection_iterator($this); } @@ -54,9 +54,9 @@ class service_collection extends \ArrayObject /** * {@inheritdoc} */ - public function offsetGet($index) + public function offsetGet($key): mixed { - return $this->container->get($index); + return $this->container->get($key); } /** diff --git a/phpBB/phpbb/di/service_collection_iterator.php b/phpBB/phpbb/di/service_collection_iterator.php index 31bc156e99..732f677809 100644 --- a/phpBB/phpbb/di/service_collection_iterator.php +++ b/phpBB/phpbb/di/service_collection_iterator.php @@ -39,7 +39,7 @@ class service_collection_iterator extends \ArrayIterator /** * {@inheritdoc} */ - public function current() + public function current(): mixed { return $this->collection->offsetGet($this->key()); } diff --git a/phpBB/phpbb/event/recursive_event_filter_iterator.php b/phpBB/phpbb/event/recursive_event_filter_iterator.php index 22a6861522..f671c474d9 100644 --- a/phpBB/phpbb/event/recursive_event_filter_iterator.php +++ b/phpBB/phpbb/event/recursive_event_filter_iterator.php @@ -39,7 +39,7 @@ class recursive_event_filter_iterator extends \RecursiveFilterIterator * * @return recursive_event_filter_iterator */ - public function getChildren() + public function getChildren(): ?\RecursiveFilterIterator { $inner_iterator = $this->getInnerIterator(); assert($inner_iterator instanceof \RecursiveIterator); @@ -49,7 +49,7 @@ class recursive_event_filter_iterator extends \RecursiveFilterIterator /** * {@inheritDoc} */ - public function accept() + public function accept(): bool { $relative_path = str_replace(DIRECTORY_SEPARATOR, '/', $this->current()); $filename = $this->current()->getFilename(); diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 3063b5db33..6e4c224e39 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -137,7 +137,7 @@ class filespec $this->mimetype = $upload_ary['type']; // Opera adds the name to the mime type - $this->mimetype = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype; + $this->mimetype = ($this->mimetype && str_contains($this->mimetype, '; name')) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype; if (!$this->mimetype) { diff --git a/phpBB/phpbb/files/filespec_storage.php b/phpBB/phpbb/files/filespec_storage.php index f718d82d74..e775935756 100644 --- a/phpBB/phpbb/files/filespec_storage.php +++ b/phpBB/phpbb/files/filespec_storage.php @@ -117,7 +117,7 @@ class filespec_storage $this->mimetype = $upload_ary['type']; // Opera adds the name to the mime type - $this->mimetype = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype; + $this->mimetype = ($this->mimetype && str_contains($this->mimetype, '; name')) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype; if (!$this->mimetype) { diff --git a/phpBB/phpbb/filesystem/filesystem.php b/phpBB/phpbb/filesystem/filesystem.php index 67b25907b3..6d34d990e2 100644 --- a/phpBB/phpbb/filesystem/filesystem.php +++ b/phpBB/phpbb/filesystem/filesystem.php @@ -606,7 +606,7 @@ class filesystem implements filesystem_interface protected function phpbb_own_realpath($path) { // Replace all directory separators with '/' - $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); + $path = str_replace(DIRECTORY_SEPARATOR, '/', $path ?: ''); $is_absolute_path = false; $path_prefix = ''; diff --git a/phpBB/phpbb/finder/finder.php b/phpBB/phpbb/finder/finder.php index 9e7f201ea3..7bad84e36d 100644 --- a/phpBB/phpbb/finder/finder.php +++ b/phpBB/phpbb/finder/finder.php @@ -77,7 +77,7 @@ class finder ); $this->extensions = array(); - $this->cached_queries = ($this->cache) ? $this->cache->get($this->cache_name) : false; + $this->cached_queries = $this->cache ? ($this->cache->get($this->cache_name) ?: []) : []; } /** diff --git a/phpBB/phpbb/notification/type/base.php b/phpBB/phpbb/notification/type/base.php index 54a9319d26..cf0ce88a62 100644 --- a/phpBB/phpbb/notification/type/base.php +++ b/phpBB/phpbb/notification/type/base.php @@ -118,7 +118,7 @@ abstract class base implements \phpbb\notification\type\type_interface { // The row from the database (unless this is a new notification we're going to add) $this->data = $data; - $this->data['notification_data'] = (isset($this->data['notification_data'])) ? unserialize($this->data['notification_data']) : array(); + $this->data['notification_data'] = !empty($this->data['notification_data']) ? unserialize($this->data['notification_data']) : []; } /** diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 5a1a6dbd5c..da76c3cce6 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -150,7 +150,7 @@ class type_date extends type_base */ public function validate_profile_field(&$field_value, $field_data) { - $field_validate = explode('-', $field_value); + $field_validate = explode('-', $field_value ?: ''); $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0; $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0; @@ -184,7 +184,7 @@ class type_date extends type_base */ public function get_profile_value($field_value, $field_data) { - $date = explode('-', $field_value); + $date = explode('-', $field_value ?: ''); $day = (isset($date[0])) ? (int) $date[0] : 0; $month = (isset($date[1])) ? (int) $date[1] : 0; $year = (isset($date[2])) ? (int) $date[2] : 0; diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 9dc0181cb8..f22765b171 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -120,7 +120,7 @@ class type_int extends type_base */ public function validate_profile_field(&$field_value, $field_data) { - if (trim($field_value) === '' && !$field_data['field_required']) + if (trim($field_value ?: '') === '' && !$field_data['field_required']) { return false; } diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index f5e1992044..6248285e10 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -63,13 +63,9 @@ abstract class type_string_common extends type_base */ public function validate_string_profile_field($field_type, &$field_value, $field_data) { - if (trim($field_value) === '' && !$field_data['field_required']) + if (trim($field_value ?? '') === '') { - return false; - } - else if (trim($field_value) === '' && $field_data['field_required']) - { - return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name'])); + return $field_data['field_required'] ? $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name'])) : false; } if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen']) diff --git a/phpBB/phpbb/request/deactivated_super_global.php b/phpBB/phpbb/request/deactivated_super_global.php index ba7111e000..2cc2ddf62e 100644 --- a/phpBB/phpbb/request/deactivated_super_global.php +++ b/phpBB/phpbb/request/deactivated_super_global.php @@ -13,6 +13,8 @@ namespace phpbb\request; +use ReturnTypeWillChange; + /** * Replacement for a superglobal (like $_GET or $_POST) which calls * trigger_error on all operations but isset, overloads the [] operator with SPL. @@ -74,7 +76,7 @@ class deactivated_super_global implements \ArrayAccess, \Countable, \IteratorAgg * * @return bool Whether the key on the super global exists. */ - public function offsetExists($offset) + public function offsetExists($offset): bool { return $this->request->is_set($offset, $this->super_global); } @@ -82,17 +84,17 @@ class deactivated_super_global implements \ArrayAccess, \Countable, \IteratorAgg /**#@+ * Part of the \ArrayAccess implementation, will always result in a FATAL error. */ - public function offsetGet($offset) + public function offsetGet($offset): mixed { $this->error(); } - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { $this->error(); } - public function offsetUnset($offset) + public function offsetUnset($offset): void { $this->error(); } @@ -103,7 +105,7 @@ class deactivated_super_global implements \ArrayAccess, \Countable, \IteratorAgg * @return void * @psalm-suppress InvalidReturnType */ - public function count() + public function count(): int { $this->error(); } @@ -113,7 +115,7 @@ class deactivated_super_global implements \ArrayAccess, \Countable, \IteratorAgg * @return void * @psalm-suppress InvalidReturnType */ - public function getIterator() + #[ReturnTypeWillChange] public function getIterator(): void { $this->error(); } diff --git a/phpBB/phpbb/session.php b/phpBB/phpbb/session.php index ae9624cd49..bd7620b840 100644 --- a/phpBB/phpbb/session.php +++ b/phpBB/phpbb/session.php @@ -47,7 +47,7 @@ class session // First of all, get the request uri... $script_name = $request->escape($symfony_request->getScriptName(), true); - $args = $request->escape(explode('&', $symfony_request->getQueryString()), true); + $args = $request->escape(explode('&', $symfony_request->getQueryString() ?? ''), true); // If we are unable to get the script name we use REQUEST_URI as a failover and note it within the page array for easier support... if (!$script_name) diff --git a/tests/mock/lang.php b/tests/mock/lang.php index bebbe5a29b..5de41f5775 100644 --- a/tests/mock/lang.php +++ b/tests/mock/lang.php @@ -17,25 +17,25 @@ */ class phpbb_mock_lang implements ArrayAccess { - public function offsetExists($offset) + public function offsetExists($offset): bool { return true; } - public function offsetGet($offset) + public function offsetGet($offset): mixed { return $offset; } - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { } - public function offsetUnset($offset) + public function offsetUnset($offset): void { } - public function lang() + public function lang(): string { return implode(' ', func_get_args()); } diff --git a/tests/mock/request.php b/tests/mock/request.php index 1253e40915..4985d6db2c 100644 --- a/tests/mock/request.php +++ b/tests/mock/request.php @@ -41,13 +41,13 @@ class phpbb_mock_request extends \phpbb\request\request public function variable($var_name, $default, $multibyte = false, $super_global = \phpbb\request\request_interface::REQUEST) { - return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default; + return $this->data[$super_global][$var_name] ?? $default; } public function server($var_name, $default = '') { $super_global = \phpbb\request\request_interface::SERVER; - return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default; + return $this->data[$super_global][$var_name] ?? $default; } public function header($header_name, $default = '') @@ -115,7 +115,7 @@ class phpbb_mock_request extends \phpbb\request\request $this->data[\phpbb\request\request_interface::SERVER][$var_name] = $value; } - public function merge($super_global = \phpbb\request\request_interface::REQUEST, $values) + public function merge($values, $super_global = \phpbb\request\request_interface::REQUEST): void { $this->data[$super_global] = array_merge($this->data[$super_global], $values); } diff --git a/tests/notification/submit_post_base.php b/tests/notification/submit_post_base.php index fb87dde787..e8ad00e5ef 100644 --- a/tests/notification/submit_post_base.php +++ b/tests/notification/submit_post_base.php @@ -80,6 +80,7 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c 'num_posts' => 1, 'allow_board_notifications' => true, 'allow_mentions' => true, + 'board_startdate' => 1692429414, )); // Event dispatcher @@ -113,8 +114,7 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c ); // Request - $type_cast_helper = $this->createMock('\phpbb\request\type_cast_helper_interface'); - $request = $this->createMock('\phpbb\request\request'); + $request = new phpbb_mock_request(); $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper') ->disableOriginalConstructor()