Ajde_Environment::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Ajde_Environment extends Ajde_Object_Singleton
4
{
5
    const ENV_DEFAULT = 'production';
6
7
    /**
8
     * @var string
9
     */
10
    private $environment;
11
12
    /**
13
     * Environment <-> network map.
14
     *
15
     * @author http://en.wikipedia.org/wiki/Private_network
16
     *
17
     * @var array
18
     */
19
    public static $networks = [
20
        'dev' => [
21
            '/::1/',
22
            '/127\.0\.0\.1/',
23
            '/10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/',
24
            '/172\.[1-3][0-9]\.[0-9]{1,3}\.[0-9]{1,3}/',
25
            '/192\.168\.[0-9]{1,3}\.[0-9]{1,3}/',
26
        ],
27
    ];
28
29
    /**
30
     * TODO.
31
     */
32
    public function __construct()
33
    {
34
        $this->environment = $this->autoDetect();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->autoDetect() can also be of type integer. However, the property $environment is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
35
    }
36
37
    /**
38
     * TODO.
39
     *
40
     * @return Ajde_Environment
41
     */
42
    public static function getInstance()
43
    {
44
        static $instance;
45
46
        return $instance === null ? $instance = new self() : $instance;
47
    }
48
49
    /**
50
     * TODO.
51
     *
52
     * @return string
53
     */
54
    public static function current()
55
    {
56
        $instance = self::getInstance();
57
58
        return $instance->environment();
59
    }
60
61
    /**
62
     * TODO.
63
     *
64
     * @return string
65
     */
66
    public function environment()
67
    {
68
        return $this->environment;
69
    }
70
71
    /**
72
     * TODO.
73
     *
74
     * @return string
75
     */
76
    private function autoDetect()
77
    {
78
        if (file_exists(LOCAL_ROOT.'.env')) {
79
            return trim(file_get_contents(LOCAL_ROOT.'.env'));
80
        }
81
82
        foreach (self::$networks as $environment => $ips) {
83
            foreach ($ips as $pattern) {
84
                if (preg_match($pattern, $_SERVER['SERVER_ADDR'])) {
85
                    return $environment;
86
                }
87
            }
88
        }
89
90
        return self::ENV_DEFAULT;
91
    }
92
}
93