From 8f48ee027f2e4e3ed9e69a6e2910ed577196813e Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms <tleilax+studip@gmail.com> Date: Mon, 11 Jul 2022 09:52:05 +0000 Subject: [PATCH] fix errors of type 'Unsafe usage of new static', fixes #1245 Closes #1245 Merge request studip/studip!760 --- lib/classes/I18N.php | 2 +- lib/classes/Interactable.class.php | 3 +-- lib/classes/LicenseAvatar.php | 4 ++-- lib/classes/MultiDimArrayObject.class.php | 6 ++++-- lib/classes/QuestionBox.php | 2 +- lib/classes/QuickSearch.class.php | 2 +- lib/classes/RangeConfig.class.php | 2 +- lib/classes/WidgetContainer.php | 18 ++++++++---------- lib/classes/forms/Form.php | 9 +++++++++ lib/classes/forms/Input.php | 2 +- lib/classes/helpbar/Helpbar.php | 4 +--- lib/classes/sidebar/Sidebar.php | 4 +--- lib/models/OERDownloadcounter.php | 2 +- lib/models/OERHost.php | 2 +- lib/models/SimpleORMap.class.php | 6 ++++-- lib/models/StudipNews.class.php | 21 +-------------------- lib/models/Token.php | 2 +- 17 files changed, 39 insertions(+), 52 deletions(-) diff --git a/lib/classes/I18N.php b/lib/classes/I18N.php index 4ed94b78396..18e1e7aa749 100644 --- a/lib/classes/I18N.php +++ b/lib/classes/I18N.php @@ -66,7 +66,7 @@ class I18N * @param array_merge $attributes Additional variables for the * element */ - protected function __construct($template, $name, $value, array $attributes) + final protected function __construct($template, $name, $value, array $attributes) { $this->template = $GLOBALS['template_factory']->open($template); $this->name = $name; diff --git a/lib/classes/Interactable.class.php b/lib/classes/Interactable.class.php index b401115d976..6ba7c25ab7e 100644 --- a/lib/classes/Interactable.class.php +++ b/lib/classes/Interactable.class.php @@ -15,7 +15,6 @@ namespace Studip; */ abstract class Interactable { - public $label, $attributes; /** @@ -107,7 +106,7 @@ abstract class Interactable * @param string $trait the specific trait of the current element * @param array $attributes the attributes of the button element * - * @return Interactable a Interactable element + * @return Interactable element */ public static function create($label = NULL, $trait = NULL, $attributes = []) { diff --git a/lib/classes/LicenseAvatar.php b/lib/classes/LicenseAvatar.php index 9d48f0a83d0..27072abcbfe 100644 --- a/lib/classes/LicenseAvatar.php +++ b/lib/classes/LicenseAvatar.php @@ -25,7 +25,7 @@ class LicenseAvatar extends Avatar */ public static function getAvatar($id) { - return new static($id); + return new self($id); } /** @@ -35,7 +35,7 @@ class LicenseAvatar extends Avatar */ public static function getNobody() { - return new static('nobody'); + return new self('nobody'); } /** diff --git a/lib/classes/MultiDimArrayObject.class.php b/lib/classes/MultiDimArrayObject.class.php index bcbdee49fe5..8af74a7ea74 100644 --- a/lib/classes/MultiDimArrayObject.class.php +++ b/lib/classes/MultiDimArrayObject.class.php @@ -99,7 +99,8 @@ class MultiDimArrayObject extends StudipArrayObject { $new_value = $this->recursiveArrayToArrayObjects($value); if (is_array($new_value)) { - $new_value = new static($new_value, $this->getFlags(), $this->getIteratorClass()); + $class = get_called_class(); + $new_value = new $class($new_value, $this->getFlags(), $this->getIteratorClass()); } if (is_null($key)) { return $this->storage[] = $new_value; @@ -117,7 +118,8 @@ class MultiDimArrayObject extends StudipArrayObject foreach ($data as $key => $value) { $new_value = $this->recursiveArrayToArrayObjects($value); if (is_array($new_value)) { - $new_data[$key] = new static($new_value, $this->getFlags(), $this->getIteratorClass()); + $class = get_called_class(); + $new_data[$key] = new $class($new_value, $this->getFlags(), $this->getIteratorClass()); } else { $new_data[$key] = $value; } diff --git a/lib/classes/QuestionBox.php b/lib/classes/QuestionBox.php index d4d33be5fe6..8c335b1599a 100644 --- a/lib/classes/QuestionBox.php +++ b/lib/classes/QuestionBox.php @@ -60,7 +60,7 @@ class QuestionBox implements LayoutMessage * * @param string $question The question that should be confirmed */ - protected function __construct($question, $accept_url, $decline_url) + final protected function __construct($question, $accept_url, $decline_url) { $this->question = $question; $this->setAcceptURL($accept_url); diff --git a/lib/classes/QuickSearch.class.php b/lib/classes/QuickSearch.class.php index e0e57d306a1..b058c53a2d0 100644 --- a/lib/classes/QuickSearch.class.php +++ b/lib/classes/QuickSearch.class.php @@ -174,7 +174,7 @@ class QuickSearch * * @return void */ - public function __construct($name, $search = NULL) + final public function __construct($name, $search = NULL) { self::$count_QS++; $this->name = $name; diff --git a/lib/classes/RangeConfig.class.php b/lib/classes/RangeConfig.class.php index 17375d15e70..b1030e39615 100644 --- a/lib/classes/RangeConfig.class.php +++ b/lib/classes/RangeConfig.class.php @@ -78,7 +78,7 @@ class RangeConfig extends Config * @param string $range_id * @param array $data */ - public function __construct($range_id = null, $data = null) + final public function __construct($range_id = null, $data = null) { $this->range_id = $range_id; if ($range_id !== null || $data !== null) { diff --git a/lib/classes/WidgetContainer.php b/lib/classes/WidgetContainer.php index 39665a9a6cb..b4aaa8cdd05 100644 --- a/lib/classes/WidgetContainer.php +++ b/lib/classes/WidgetContainer.php @@ -9,6 +9,14 @@ */ abstract class WidgetContainer { + /** + * Protected constructor to ensure that the singleton Get() method is always + * used. + * + * @see WidgetContainer::Get + */ + abstract protected function __construct(); + /** * The singleton instance of the container */ @@ -30,16 +38,6 @@ abstract class WidgetContainer return static::$instances[$class]; } - /** - * Private constructor to ensure that the singleton Get() method is always - * used. - * - * @see WidgetContainer::Get - */ - protected function __construct() - { - } - /** * Contains the widgets of the container */ diff --git a/lib/classes/forms/Form.php b/lib/classes/forms/Form.php index 06331901cc1..569eb1fa32a 100644 --- a/lib/classes/forms/Form.php +++ b/lib/classes/forms/Form.php @@ -49,6 +49,15 @@ class Form extends Part return $form; } + /** + * Finalized constructor. + * + * @param mixed[] ...$parts + */ + final public function __construct(...$parts) + { + parent::__construct($parts); + } /** * Adds a new Fieldset to the Form object with the SORM object's fields as diff --git a/lib/classes/forms/Input.php b/lib/classes/forms/Input.php index 3018a5baf39..aa3069b70b1 100644 --- a/lib/classes/forms/Input.php +++ b/lib/classes/forms/Input.php @@ -88,7 +88,7 @@ abstract class Input * @param $value * @param $attributes */ - public function __construct($name, $title, $value, array $attributes = []) + final public function __construct($name, $title, $value, array $attributes = []) { $this->name = $name; $this->title = $title; diff --git a/lib/classes/helpbar/Helpbar.php b/lib/classes/helpbar/Helpbar.php index 4725a782b7d..cfa98a34b8f 100644 --- a/lib/classes/helpbar/Helpbar.php +++ b/lib/classes/helpbar/Helpbar.php @@ -17,10 +17,8 @@ class Helpbar extends WidgetContainer /** * Constructs the helpbar */ - public function __construct() + protected function __construct() { - parent::__construct(); - $this->help_admin = isset($GLOBALS['perm']) && ($GLOBALS['perm']->have_perm('root') || RolePersistence::isAssignedRole($GLOBALS['user']->id, 'Hilfe-Administrator(in)')); } diff --git a/lib/classes/sidebar/Sidebar.php b/lib/classes/sidebar/Sidebar.php index d91578654cf..7536a7f9df0 100644 --- a/lib/classes/sidebar/Sidebar.php +++ b/lib/classes/sidebar/Sidebar.php @@ -18,10 +18,8 @@ class Sidebar extends WidgetContainer /** * Constructor, tries to automagically set the sidebar's title. */ - public function __construct() + protected function __construct() { - parent::__construct(); - $this->setTitle(); } diff --git a/lib/models/OERDownloadcounter.php b/lib/models/OERDownloadcounter.php index 0c918dc1d87..fa603a380a5 100755 --- a/lib/models/OERDownloadcounter.php +++ b/lib/models/OERDownloadcounter.php @@ -9,7 +9,7 @@ class OERDownloadcounter extends SimpleORMap public static function addCounter($material_id) { - $counter = new static(); + $counter = new self(); $counter['material_id'] = $material_id; if (Config::get()->oer_GEOLOCATOR_API) { list($url, $lon, $lat) = explode(" ", Config::get()->oer_GEOLOCATOR_API); diff --git a/lib/models/OERHost.php b/lib/models/OERHost.php index bba555d1e76..7cb2ba5c9d4 100755 --- a/lib/models/OERHost.php +++ b/lib/models/OERHost.php @@ -21,7 +21,7 @@ class OERHost extends OERIdentity } return $host; } else { - $host = new static(); + $host = new self(); $host['name'] = Config::get()->UNI_NAME_CLEAN; $host['url'] = $GLOBALS['oer_PREFERRED_URI'] ?: $GLOBALS['ABSOLUTE_URI_STUDIP']."dispatch.php/oer/endpoints/"; $host['last_updated'] = time(); diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php index 3eeac13f643..ab2c332a635 100644 --- a/lib/models/SimpleORMap.class.php +++ b/lib/models/SimpleORMap.class.php @@ -458,7 +458,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate */ public static function create($data) { - $record = new static(); + $class = get_called_class(); + $record = new $class(); $record->setData($data, false); if ($record->store()) { return $record; @@ -653,7 +654,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate $sql = "WHERE {$sql}"; } - $record = new static(); + $class = get_called_class(); + $record = new $class(); $record->setNew(false); $db_table = static::config('db_table'); diff --git a/lib/models/StudipNews.class.php b/lib/models/StudipNews.class.php index 643f9eba6bb..2d5652aa14f 100644 --- a/lib/models/StudipNews.class.php +++ b/lib/models/StudipNews.class.php @@ -305,7 +305,7 @@ class StudipNews extends SimpleORMap implements PrivacyObject $objects[$area][$id]['title'] = _('Ankündigungen auf der Stud.IP Startseite'); } if ($as_objects) { - $objects[$area][$id]['object'] = new static(); + $objects[$area][$id]['object'] = new self(); $objects[$area][$id]['object']->setData($result, true); $objects[$area][$id]['object']->setNew(false); } @@ -441,25 +441,6 @@ class StudipNews extends SimpleORMap implements PrivacyObject } } - /** - * DEPRECATED - */ - public static function TouchNews($news_id, $touch_stamp = null) - { - $ret = false; - if (!$touch_stamp) { - $touch_stamp = time(); - } - $news = new static($news_id); - if (!$news->isNew()) { - $news->date = strtotime('today 0:00:00', $touch_stamp); - if (!$news->store()) { - $news->triggerChdate(); - } - } - return $ret; - } - public static function DeleteNewsRanges($range_id) { $query = "DELETE FROM news_range WHERE range_id = :id"; diff --git a/lib/models/Token.php b/lib/models/Token.php index dd52602f9a3..f2fd6fa683b 100644 --- a/lib/models/Token.php +++ b/lib/models/Token.php @@ -50,7 +50,7 @@ class Token extends SimpleORMap */ public static function create($duration = 30, $user_id = null) { - $token = new static(); + $token = new self(); $token->user_id = $user_id ?? $GLOBALS['user']->id; $token->expiration = strtotime("+{$duration} seconds"); $token->store(); -- GitLab