Settings class: Add type hints

This commit is contained in:
slawkens
2025-08-22 15:30:19 +02:00
parent d85681880e
commit 3451715e96

View File

@@ -7,16 +7,13 @@ use MyAAC\Models\Settings as ModelsSettings;
class Settings implements \ArrayAccess class Settings implements \ArrayAccess
{ {
static private $instance; static private ?Settings $instance = null;
private $settingsFile = []; private array $settingsFile = [];
private $settingsDatabase = []; private array $settingsDatabase = [];
private $cache = []; private array $cache = [];
private $valuesAsked = []; private array $valuesAsked = [];
private $errors = []; private array $errors = [];
/**
* @return Settings
*/
public static function getInstance(): Settings public static function getInstance(): Settings
{ {
if (!self::$instance) { if (!self::$instance) {
@@ -26,7 +23,7 @@ class Settings implements \ArrayAccess
return self::$instance; return self::$instance;
} }
public function load() public function load(): void
{ {
$cache = Cache::getInstance(); $cache = Cache::getInstance();
if ($cache->enabled()) { if ($cache->enabled()) {
@@ -47,7 +44,7 @@ class Settings implements \ArrayAccess
} }
} }
public function save($pluginName, $values) public function save($pluginName, $values): bool
{ {
$this->loadPlugin($pluginName); $this->loadPlugin($pluginName);
@@ -104,7 +101,7 @@ class Settings implements \ArrayAccess
return true; return true;
} }
public function updateInDatabase($pluginName, $key, $value) public function updateInDatabase($pluginName, $key, $value): void
{ {
if (ModelsSettings::where(['name' => $pluginName, 'key' => $key])->exists()) { if (ModelsSettings::where(['name' => $pluginName, 'key' => $key])->exists()) {
ModelsSettings::where(['name' => $pluginName, 'key' => $key])->update(['value' => $value]); ModelsSettings::where(['name' => $pluginName, 'key' => $key])->update(['value' => $value]);
@@ -117,7 +114,7 @@ class Settings implements \ArrayAccess
$this->clearCache(); $this->clearCache();
} }
public function deleteFromDatabase($pluginName, $key = null) public function deleteFromDatabase($pluginName, $key = null): void
{ {
if (!isset($key)) { if (!isset($key)) {
ModelsSettings::where('name', $pluginName)->delete(); ModelsSettings::where('name', $pluginName)->delete();
@@ -383,7 +380,7 @@ class Settings implements \ArrayAccess
} }
#[\ReturnTypeWillChange] #[\ReturnTypeWillChange]
public function offsetSet($offset, $value) public function offsetSet($offset, $value): void
{ {
if (is_null($offset)) { if (is_null($offset)) {
throw new \RuntimeException("Settings: You cannot set empty offset with value: $value!"); throw new \RuntimeException("Settings: You cannot set empty offset with value: $value!");
@@ -423,7 +420,7 @@ class Settings implements \ArrayAccess
} }
#[\ReturnTypeWillChange] #[\ReturnTypeWillChange]
public function offsetUnset($offset) public function offsetUnset($offset): void
{ {
$this->loadPlugin($offset); $this->loadPlugin($offset);
@@ -455,7 +452,7 @@ class Settings implements \ArrayAccess
* @return array|mixed * @return array|mixed
*/ */
#[\ReturnTypeWillChange] #[\ReturnTypeWillChange]
public function offsetGet($offset) public function offsetGet($offset): mixed
{ {
// try cache hit // try cache hit
if(isset($this->cache[$offset])) { if(isset($this->cache[$offset])) {
@@ -521,7 +518,7 @@ class Settings implements \ArrayAccess
return $ret; return $ret;
} }
private function updateValuesAsked($offset) private function updateValuesAsked($offset): void
{ {
$pluginKeyName = $offset; $pluginKeyName = $offset;
if (strpos($offset, '.')) { if (strpos($offset, '.')) {
@@ -537,7 +534,7 @@ class Settings implements \ArrayAccess
} }
} }
private function loadPlugin($offset) private function loadPlugin($offset): void
{ {
$this->updateValuesAsked($offset); $this->updateValuesAsked($offset);
@@ -566,7 +563,7 @@ class Settings implements \ArrayAccess
} }
} }
public static function saveConfig($config, $filename, &$content = '') public static function saveConfig($config, $filename, &$content = ''): bool|int
{ {
$content = "<?php" . PHP_EOL; $content = "<?php" . PHP_EOL;