Environment::get()   B
last analyzed

Complexity

Conditions 11
Paths 20

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 19
c 2
b 0
f 0
nc 20
nop 2
dl 0
loc 29
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Support;
24
25
use Syscodes\Dotenv\Repository\RepositoryCreator;
26
use Syscodes\Dotenv\Repository\Adapters\PutenvAdapter;
27
28
/**
29
 * Gets the adapter environment and value of an environment variable.
30
 * 
31
 * @author Alexander Campo <[email protected]>
32
 */
33
class Environment
34
{
35
    /**
36
     * Activate use of putenv, by default is true.
37
     * 
38
     * @var bool $enabledPutenv
39
     */
40
    protected static $enabledPutenv = true;
41
42
    /**
43
     * The environment repository instance.
44
     * 
45
     * @var Syscodes\Dotenv\Repository\RepositoryCreator|null $repository
0 ignored issues
show
Bug introduced by
The type Syscodes\Support\Syscode...itory\RepositoryCreator was not found. Did you mean Syscodes\Dotenv\Repository\RepositoryCreator? If so, make sure to prefix the type with \.
Loading history...
46
     */
47
    protected static $repository;
48
49
    /**
50
     * Get the environment repository instance.
51
     * 
52
     * @return  Syscodes\Dotenv\Repository\RepositoryCreator
53
     */
54
    public static function getRepositoryCreator()
55
    {
56
        if (null === static::$repository) {
57
            $repository = RepositoryCreator::createDefaultAdapters();
58
59
            if (static::$enabledPutenv) {
60
                $repository = $repository->addAdapter(PutenvAdapter::class);
61
            }
62
63
            static::$repository = $repository->make();
0 ignored issues
show
Documentation Bug introduced by
It seems like $repository->make() of type Syscodes\Dotenv\Repository\AdapterRepository is incompatible with the declared type Syscodes\Support\Syscode...\RepositoryCreator|null of property $repository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
        }
65
66
        return static::$repository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::repository also could return the type Syscodes\Dotenv\Repository\AdapterRepository which is incompatible with the documented return type Syscodes\Support\Syscode...itory\RepositoryCreator.
Loading history...
67
    }
68
69
    /**
70
     * Gets the value of an environment variable.
71
     * 
72
     * @param  string  $key
73
     * @param  mixed|null  $default 
74
     * 
75
     * @return mixed
76
     */
77
    public static function get($key, $default = null)
78
    {
79
        $value = Environment::getRepositoryCreator()->get($key);
80
81
        if ($value === null) {
82
            $value = $_ENV[$key] ?? $_SERVER[$key] ?? false;
83
        }
84
85
        if ($value === false) {
86
            return value($default);
87
        }
88
89
        // Handle any boolean values
90
        switch (strtolower($value)) {
91
            case 'true':
92
            case '(true)':
93
                return true;
94
            case 'false':
95
            case '(false)':
96
                return false;
97
            case 'empty':
98
            case '(empty)':
99
                return '';
100
            case 'null':
101
            case '(null)':
102
                return null;
103
        }
104
        
105
        return $value;
106
    }
107
}