Skip to content

Commit 426d9db

Browse files
committed
Upgrade package to supports PHP 8.1
1 parent eb7edeb commit 426d9db

File tree

10 files changed

+94
-90
lines changed

10 files changed

+94
-90
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ and overrides the `defaults` stage.
1313
composer require spacetab-io/configuration
1414
```
1515

16+
#### Versions map
17+
18+
* Package with version `4.*` requires PHP >= `8.1`
19+
* Package with version `3.*` requires PHP >= `7.4` <= `8.0`
20+
* Package with version `2.*` requires PHP < `7.4`
21+
1622
## Usage
1723

1824
By default, path to configuration directory and application stage
@@ -90,7 +96,7 @@ That all.
9096
## CLI utility
9197

9298
Also, you can install simple small cli-utility to dump total results of merged config.
93-
It possible with multiple ways:
99+
It is possible with multiple ways:
94100

95101
1) Install to `/usr/local/bin` as global binary
96102

@@ -149,14 +155,14 @@ Help:
149155
150156
## Depends
151157
152-
* \>= PHP 7.4 (for prev version use `2.x` releases of library)
158+
* \>= PHP 8.1
153159
* Composer for install package
154160
155161
## License
156162
157163
The MIT License
158164
159-
Copyright © 2020 spacetab.io, Inc. https://spacetab.io
165+
Copyright © 2022 spacetab.io, Inc. https://spacetab.io
160166
161167
Permission is hereby granted, free of charge, to any person obtaining a copy
162168
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
}
1717
},
1818
"require": {
19-
"php": ">=7.4",
19+
"php": ">=8.1",
2020
"symfony/yaml": "^4.2 || ^5.0 || ^6.0",
2121
"psr/log": "^1.1 || ^2.0 || ^3.0",
2222
"symfony/console": "^4.3 || ^5.0 || ^6.0",
2323
"spacetab-io/obelix": "^1.0"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^9",
27-
"symfony/var-dumper": "^4.2 || ^5.0",
26+
"phpunit/phpunit": "^9 || ^10",
27+
"symfony/var-dumper": "^4.2 || ^5.0 || ^6.0",
2828
"humbug/box": "^3.8",
29-
"phpstan/phpstan": "^0.12",
29+
"phpstan/phpstan": "^1.5.4",
3030
"spacetab-io/logger": "^2.0"
3131
},
3232
"suggest": {
@@ -37,5 +37,11 @@
3737
"phpunit --coverage-text",
3838
"phpstan analyse src --level 6"
3939
]
40+
},
41+
"config": {
42+
"sort-packages": true,
43+
"allow-plugins": {
44+
"ocramius/package-versions": true
45+
}
4046
}
4147
}

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parameters:
2+
checkMissingIterableValueType: false

src/Configuration.php

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Class Configuration
1717
*
18-
* @implements \ArrayAccess<mixed, mixed>
18+
* @implements ArrayAccess<array-key, mixed>
1919
* @package Spacetab\Configuration
2020
*/
2121
final 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
}

src/ConfigurationAwareInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface ConfigurationAwareInterface
1212
/**
1313
* Sets a configuration instance on the object.
1414
*
15-
* @param \Spacetab\Configuration\ConfigurationInterface $configuration
15+
* @param ConfigurationInterface $configuration
1616
*
1717
* @return void
1818
*/

src/ConfigurationAwareTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ trait ConfigurationAwareTrait
1212
/**
1313
* The Configuration instance.
1414
*
15-
* @var \Spacetab\Configuration\ConfigurationInterface
15+
* @var ConfigurationInterface
1616
*/
1717
protected ConfigurationInterface $configuration;
1818

1919
/**
2020
* Sets a configuration.
2121
*
22-
* @param \Spacetab\Configuration\ConfigurationInterface $configuration
22+
* @param ConfigurationInterface $configuration
2323
*/
2424
public function setConfiguration(ConfigurationInterface $configuration): void
2525
{

src/ConfigurationInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ interface ConfigurationInterface
2323
* @param mixed $default
2424
* @return mixed
2525
*/
26-
public function get($key, $default = null);
26+
public function get(string $key, mixed $default = null): mixed;
2727

2828
/**
2929
* Gets all the tree config
3030
*
31-
* @return array<mixed>
31+
* @return array
3232
*/
3333
public function all(): array;
3434
}

src/Console/DumpConfigCommand.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Spacetab\Configuration\Console;
66

77
use Spacetab\Configuration\Configuration;
8+
use Spacetab\Configuration\Exception\ConfigurationException;
89
use Spacetab\Logger\Logger;
910
use Psr\Log\LogLevel;
1011
use Symfony\Component\Console\Command\Command;
@@ -15,6 +16,8 @@
1516

1617
class DumpConfigCommand extends Command
1718
{
19+
protected static $defaultName = 'dump';
20+
protected static $defaultDescription = 'Dump loaded configuration';
1821

1922
protected function configure(): void
2023
{
@@ -32,9 +35,9 @@ protected function configure(): void
3235
/**
3336
* Execute command, captain.
3437
*
35-
* @param \Symfony\Component\Console\Input\InputInterface $input
36-
* @param \Symfony\Component\Console\Output\OutputInterface $output
37-
* @throws \Spacetab\Configuration\Exception\ConfigurationException
38+
* @param InputInterface $input
39+
* @param OutputInterface $output
40+
* @throws ConfigurationException
3841
*
3942
* @return int
4043
*/
@@ -58,6 +61,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5861

5962
$output->writeln($string);
6063

61-
return 0;
64+
return Command::SUCCESS;
6265
}
6366
}

0 commit comments

Comments
 (0)