Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Environment
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 get
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Environment;
21
22/**
23 * Reads environment variables through PHP's native runtime.
24 */
25 class Environment implements EnvironmentInterface
26{
27    /**
28     * Reads an environment variable or the current environment map.
29     *
30     * @param string|null $name the environment variable name, or null to read the current environment map
31     * @param string|null $default the value returned when the named variable is not defined
32     *
33     * @return array<string, string>|string|null the environment map, variable value, or default fallback
34     */
35    public function get(?string $name = null, ?string $default = null): array|string|null
36    {
37        if (null === $name) {
38            $environment = getenv();
39
40            return \is_array($environment) ? $environment : [];
41        }
42
43        $value = getenv($name);
44
45        if (false === $value) {
46            return $default;
47        }
48
49        return $value;
50    }
51}