Lines
53.84%
7 / 13
Functions and Methods
33.33%
1 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| EnvironmentVariables | 53.84% | 7 / 13 | 33.33% | 1 / 3 | 14.29 | 0.00% | 0 / 1 | |||
| __construct | n/a | 0 / 0 | n/a | 0 / 0 | 1 | |||||
| fromGlobals | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 12 | |||||
| get | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| filterByPrefix | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Config; | |
| 6 | ||
| 7 | use SensitiveParameter; | |
| 8 | ||
| 9 | final readonly class EnvironmentVariables | |
| 10 | { | |
| 11 | /** @param array<string, string> $variables */ | |
| 12 | public function __construct(#[SensitiveParameter] private array $variables = []) {} | |
| 13 | ||
| 14 | public static function fromGlobals(): self | |
| 15 | { | |
| 16 | $variables = getenv(); | |
| 17 | ||
| 18 | /** @var mixed $value */ | |
| 19 | foreach ($_ENV as $key => $value) { | |
| 20 | if (\is_string($value)) { | |
| 21 | $variables[(string) $key] = $value; | |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | return new self($variables); | |
| 26 | } | |
| 27 | ||
| 28 | public function get(string $name): ?string | |
| 29 | { | |
| 30 | return $this->variables[$name] ?? null; | |
| 31 | } | |
| 32 | ||
| 33 | /** @return array<string, string> */ | |
| 34 | public function filterByPrefix(string $prefix): array | |
| 35 | { | |
| 36 | $matching = array_filter( | |
| 37 | $this->variables, | |
| 38 | static fn(string $value, int|string $name): bool => $value !== '' && $name !== $prefix && str_starts_with((string) $name, $prefix), | |
| 39 | \ARRAY_FILTER_USE_BOTH, | |
| 40 | ); | |
| 41 | ksort($matching); | |
| 42 | ||
| 43 | return $matching; | |
| 44 | } | |
| 45 | } |