diff options
| author | 2023-04-07 12:40:43 +0200 | |
|---|---|---|
| committer | 2023-04-07 12:40:43 +0200 | |
| commit | dbbae15a8458679db0f4540dacdbdcff9c02ec8c (patch) | |
| tree | 19d84d19660d3aa8e616f75d5c6eafba99679e1c /lib | |
| parent | d23d10bcde1a9b86c784d58b891f61e740e0124e (diff) | |
Remove ConfigurationSetter (#5251)
This class has not been maintained for a while. Only a subset of our configuration properties are there, and some code is not relevant anymore. Furthermore, it is relying exclusively on dynamically invoked functions, making it challenging to maintain, in particular to find out what is used and what is not, what is handled and what is not.
It is not well suited for changes in data formats, which have been handled in the Context class instead.
It is also not able to handle configuration properties that are missing.
It is the class with most errors for PHPStan level 6 (179 errors). It is also making intense use of is_callable and call_user_func_array, which are performance killers.
Should the need arrise again to perform validation of our internal configuration files, I suggest an implementation with the opposite approach, namely driven by our code instead of driven by the data.
In summary, at the moment, this class is costly, while not offering many guarantees.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Minz/Configuration.php | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php index f286138e2..4259c4052 100644 --- a/lib/Minz/Configuration.php +++ b/lib/Minz/Configuration.php @@ -14,6 +14,7 @@ class Minz_Configuration { /** * The list of configurations. + * @var array<string,static> */ private static $config_list = array(); @@ -25,7 +26,7 @@ class Minz_Configuration { * @param string $default_filename a filename containing default values for the configuration * @param object $configuration_setter an optional helper to set values in configuration */ - public static function register($namespace, $config_filename, $default_filename = null, $configuration_setter = null) { + public static function register(string $namespace, string $config_filename, string $default_filename = null, object $configuration_setter = null): void { self::$config_list[$namespace] = new static( $namespace, $config_filename, $default_filename, $configuration_setter ); @@ -35,10 +36,10 @@ class Minz_Configuration { * Parse a file and return its data. * * @param string $filename the name of the file to parse. - * @return array of values + * @return array<string,mixed> of values * @throws Minz_FileNotExistException if the file does not exist or is invalid. */ - public static function load($filename) { + public static function load(string $filename): array { $data = @include($filename); if (is_array($data)) { return $data; @@ -54,7 +55,7 @@ class Minz_Configuration { * @return static object * @throws Minz_ConfigurationNamespaceException if the namespace does not exist. */ - public static function get($namespace) { + public static function get(string $namespace) { if (!isset(self::$config_list[$namespace])) { throw new Minz_ConfigurationNamespaceException( $namespace . ' namespace does not exist' @@ -73,21 +74,25 @@ class Minz_Configuration { /** * The filename for the current configuration. + * @var string */ private $config_filename = ''; /** * The filename for the current default values, null by default. + * @var string|null */ private $default_filename = null; /** * The configuration values, an empty array by default. + * @var array<string,mixed> */ private $data = array(); /** * An object which help to set good values in configuration. + * @var object|null */ private $configuration_setter = null; @@ -99,7 +104,7 @@ class Minz_Configuration { * @param string $default_filename the file containing default values, null by default. * @param object $configuration_setter an optional helper to set values in configuration */ - private final function __construct($namespace, $config_filename, $default_filename = null, $configuration_setter = null) { + private final function __construct(string $namespace, string $config_filename, string $default_filename = null, object $configuration_setter = null) { $this->namespace = $namespace; $this->config_filename = $config_filename; $this->default_filename = $default_filename; @@ -122,25 +127,23 @@ class Minz_Configuration { /** * Set a configuration setter for the current configuration. - * @param object $configuration_setter the setter to call when modifying data. It + * @param object|null $configuration_setter the setter to call when modifying data. It * must implement an handle($key, $value) method. */ - public function _configurationSetter($configuration_setter) { + public function _configurationSetter(?object $configuration_setter): void { if (is_callable(array($configuration_setter, 'handle'))) { $this->configuration_setter = $configuration_setter; } } - public function configurationSetter() { + public function configurationSetter(): object { return $this->configuration_setter; } /** * Check if a parameter is defined in the configuration - * - * @return bool */ - public function hasParam(string $key) { + public function hasParam(string $key): bool { return isset($this->data[$key]); } @@ -149,10 +152,10 @@ class Minz_Configuration { * * @param string $key the name of the param. * @param mixed $default default value to return if key does not exist. - * @return mixed value corresponding to the key. + * @return array|mixed value corresponding to the key. * @throws Minz_ConfigurationParamException if the param does not exist */ - public function param($key, $default = null) { + public function param(string $key, $default = null) { if (isset($this->data[$key])) { return $this->data[$key]; } elseif (!is_null($default)) { @@ -165,8 +168,9 @@ class Minz_Configuration { /** * A wrapper for param(). + * @return array|mixed */ - public function __get($key) { + public function __get(string $key) { return $this->param($key); } @@ -176,7 +180,7 @@ class Minz_Configuration { * @param string $key the param name to set. * @param mixed $value the value to set. If null, the key is removed from the configuration. */ - public function _param($key, $value = null) { + public function _param(string $key, $value = null): void { if (!is_null($this->configuration_setter) && $this->configuration_setter->support($key)) { $this->configuration_setter->handle($this->data, $key, $value); } elseif (isset($this->data[$key]) && is_null($value)) { @@ -188,15 +192,16 @@ class Minz_Configuration { /** * A wrapper for _param(). + * @param mixed $value */ - public function __set($key, $value) { + public function __set(string $key, $value): void { $this->_param($key, $value); } /** * Save the current configuration in the configuration file. */ - public function save() { + public function save(): bool { $back_filename = $this->config_filename . '.bak.php'; @rename($this->config_filename, $back_filename); |
