1515/**
1616 * Class Configuration
1717 *
18- * @implements \ ArrayAccess<mixed , mixed>
18+ * @implements ArrayAccess<array-key , mixed>
1919 * @package Spacetab\Configuration
2020 */
2121final class Configuration implements ConfigurationInterface, ArrayAccess, LoggerAwareInterface
@@ -59,14 +59,7 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
5959 */
6060 private Obelix \Dot $ config ;
6161
62- /**
63- * @var string
64- */
6562 private string $ path ;
66-
67- /**
68- * @var string
69- */
7063 private string $ stage ;
7164
7265 /**
@@ -77,8 +70,8 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
7770 */
7871 public function __construct (?string $ path = null , ?string $ stage = null )
7972 {
80- $ envPath = $ this -> getEnvVariable (self ::CONFIG_PATH , self ::DEFAULT_CONFIG_PATH );
81- $ envStage = $ this -> getEnvVariable (self ::STAGE , self ::DEFAULT_STAGE );
73+ $ envPath = self :: getEnvVariable (self ::CONFIG_PATH , self ::DEFAULT_CONFIG_PATH );
74+ $ envStage = self :: getEnvVariable (self ::STAGE , self ::DEFAULT_STAGE );
8275
8376 null === $ path ? $ this ->setPath ($ envPath ) : $ this ->setPath ($ path );
8477 null === $ stage ? $ this ->setStage ($ envStage ) : $ this ->setStage ($ stage );
@@ -94,7 +87,7 @@ public function __construct(?string $path = null, ?string $stage = null)
9487 * Specially for lazy-based programmers like me.
9588 *
9689 * @param string|null $stage
97- * @throws \Spacetab\Configuration\Exception\ ConfigurationException
90+ * @throws ConfigurationException
9891 *
9992 * @return Configuration
10093 */
@@ -104,27 +97,27 @@ public static function auto(?string $stage = null): Configuration
10497 }
10598
10699 /**
107- * Get's a value from config by dot notation
108- * E.g get('x.y', 'foo') => returns the value of $config['x']['y']
100+ * Gets a value from config by dot notation
101+ * E.g. get('x.y', 'foo') => returns the value of $config['x']['y']
109102 * And if not exist, return 'foo'.
110103 *
111104 * Supported dot-notation syntax with an asterisk.
112105 * You can read about it here: https://github.com/spacetab-io/obelix-php
113106 *
114107 * @param string $key
115- * @param mixed $default
108+ * @param mixed|null $default
116109 *
117110 * @return mixed
118111 */
119- public function get ($ key , $ default = null )
112+ public function get (string $ key , mixed $ default = null ): mixed
120113 {
121114 return $ this ->config ->get ($ key , $ default )->getValue ();
122115 }
123116
124117 /**
125118 * Gets all the tree config.
126119 *
127- * @return array<mixed>
120+ * @return array
128121 */
129122 public function all (): array
130123 {
@@ -178,7 +171,7 @@ public function getStage(): string
178171 }
179172
180173 /**
181- * Whether a offset exists
174+ * Whether an offset exists
182175 *
183176 * @link https://php.net/manual/en/arrayaccess.offsetexists.php
184177 * @param mixed $offset <p>
@@ -188,7 +181,7 @@ public function getStage(): string
188181 *
189182 * @return boolean true on success or false on failure.
190183 */
191- public function offsetExists ($ offset ): bool
184+ public function offsetExists (mixed $ offset ): bool
192185 {
193186 return ! empty ($ this ->get ($ offset ));
194187 }
@@ -204,7 +197,7 @@ public function offsetExists($offset): bool
204197 *
205198 * @return mixed Can return all value types.
206199 */
207- public function offsetGet ($ offset )
200+ public function offsetGet (mixed $ offset ): mixed
208201 {
209202 return $ this ->get ($ offset );
210203 }
@@ -220,11 +213,11 @@ public function offsetGet($offset)
220213 * The value to set.
221214 * </p>
222215 * @since 5.0.0
223- * @throws \Spacetab\Configuration\Exception\ ConfigurationException
216+ * @throws ConfigurationException
224217 *
225218 * @return void
226219 */
227- public function offsetSet ($ offset , $ value )
220+ public function offsetSet (mixed $ offset , mixed $ value ): void
228221 {
229222 throw ConfigurationException::operationNotAllowed ();
230223 }
@@ -237,19 +230,19 @@ public function offsetSet($offset, $value)
237230 * The offset to unset.
238231 * </p>
239232 * @since 5.0.0
240- * @throws \Spacetab\Configuration\Exception\ ConfigurationException
233+ * @throws ConfigurationException
241234 *
242235 * @return void
243236 */
244- public function offsetUnset ($ offset ): void
237+ public function offsetUnset (mixed $ offset ): void
245238 {
246239 throw ConfigurationException::operationNotAllowed ();
247240 }
248241
249242 /**
250243 * Initialize all the magic down here
251244 *
252- * @throws \Spacetab\Configuration\Exception\ ConfigurationException
245+ * @throws ConfigurationException
253246 */
254247 public function load (): Configuration
255248 {
@@ -288,9 +281,9 @@ public function dump(int $inline = 10, int $indent = 2): string
288281 * Parses configuration and makes a tree of it.
289282 *
290283 * @param string $stage
291- * @throws \Spacetab\Configuration\Exception\ ConfigurationException
284+ * @throws ConfigurationException
292285 *
293- * @return array<mixed>
286+ * @return array
294287 */
295288 private function parseConfiguration (string $ stage = self ::DEFAULT_STAGE ): array
296289 {
@@ -346,38 +339,33 @@ private static function getEnvVariable(string $variable, string $default = ''):
346339 *
347340 * @param array ...$arrays
348341 *
349- * @return array|mixed
342+ * @return array
350343 */
351- private function arrayMergeRecursive (array ...$ arrays )
344+ private function arrayMergeRecursive (array ...$ arrays ): array
352345 {
353346 $ base = array_shift ($ arrays );
354- if ( ! is_array ($ base )) {
355- $ base = empty ($ base ) ? [] : [$ base ];
356- }
357347
358348 foreach ($ arrays as $ append ) {
359- if ( ! is_array ($ append )) {
349+ if (! is_array ($ append )) {
360350 $ append = [$ append ];
361351 }
362352 foreach ($ append as $ key => $ value ) {
363- if ( ! array_key_exists ($ key , $ base ) && ! is_numeric ($ key )) {
364- $ base [$ key ] = $ append [ $ key ] ;
353+ if (! array_key_exists ($ key , $ base ) && ! is_numeric ($ key )) {
354+ $ base [$ key ] = $ value ;
365355 continue ;
366356 }
367357
368358 if ((is_array ($ value ) || (isset ($ base [$ key ]) && is_array ($ base [$ key ]))) && $ this ->isAssoc ($ value )) {
369359 $ base [$ key ] = $ this ->arrayMergeRecursive (
370360 (array ) $ base [$ key ],
371- (array ) $ append [ $ key ]
361+ (array ) $ value
372362 );
373- } else {
374- if (is_numeric ($ key )) {
375- if ( ! in_array ($ value , $ base )) {
376- $ base [] = $ value ;
377- }
378- } else {
379- $ base [$ key ] = $ value ;
363+ } else if (is_numeric ($ key )) {
364+ if (!in_array ($ value , $ base , true )) {
365+ $ base [] = $ value ;
380366 }
367+ } else {
368+ $ base [$ key ] = $ value ;
381369 }
382370 }
383371 }
@@ -388,7 +376,7 @@ private function arrayMergeRecursive(array ...$arrays)
388376 /**
389377 * Check if array is associative or sequential list.
390378 *
391- * @param array<mixed> $array
379+ * @param array $array
392380 *
393381 * @return bool
394382 */
@@ -398,19 +386,19 @@ private function isAssoc(array $array): bool
398386 return false ;
399387 }
400388
401- return array_keys ( $ array ) !== range ( 0 , count ( $ array) - 1 );
389+ return ! array_is_list ( $ array );
402390 }
403391
404392 /**
405393 * Automatically find configuration in possible paths.
406394 *
407- * @throws \Spacetab\Configuration\Exception\ ConfigurationException
395+ * @throws ConfigurationException
408396 *
409397 * @return string
410398 */
411399 private static function findDirectories (): string
412400 {
413- if ($ value = trim (( string ) self ::getEnvVariable (self ::CONFIG_PATH ))) {
401+ if ($ value = trim (self ::getEnvVariable (self ::CONFIG_PATH ))) {
414402 // add env config path to top of possible locations.
415403 array_unshift (self ::$ possibleLocations , $ value );
416404 }
0 commit comments