Completed
Push — master ( ae9cf8...373d57 )
by KwangSeob
02:56
created

DotEnvConfiguration::loadDotEnv()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 7
nop 1
dl 0
loc 30
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace JiraRestApi\Configuration;
4
5
use JiraRestApi\JiraException;
6
7
/**
8
 * Class DotEnvConfiguration.
9
 */
10
class DotEnvConfiguration extends AbstractConfiguration
11
{
12
    /**
13
     * @param string $path
14
     *
15
     * @throws JiraException
16
     */
17
    public function __construct($path = '.')
18
    {
19
        $this->loadDotEnv($path);
20
21
        $this->jiraHost = $this->env('JIRA_HOST');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('JIRA_HOST') can also be of type boolean. However, the property $jiraHost 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...
22
        $this->jiraUser = $this->env('JIRA_USER');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('JIRA_USER') can also be of type boolean. However, the property $jiraUser 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...
23
        $this->jiraPassword = $this->env('JIRA_PASS');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('JIRA_PASS') can also be of type boolean. However, the property $jiraPassword 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...
24
        $this->oauthAccessToken = $this->env('OAUTH_ACCESS_TOKEN');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('OAUTH_ACCESS_TOKEN') can also be of type boolean. However, the property $oauthAccessToken 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...
25
        $this->cookieAuthEnabled = $this->env('COOKIE_AUTH_ENABLED', false);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('COOKIE_AUTH_ENABLED', false) can also be of type string. However, the property $cookieAuthEnabled is declared as type boolean. 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...
26
        $this->cookieFile = $this->env('COOKIE_FILE', 'jira-cookie.txt');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('COOKIE_FILE', 'jira-cookie.txt') can also be of type boolean. However, the property $cookieFile 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...
27
        $this->jiraLogFile = $this->env('JIRA_LOG_FILE', 'jira-rest-client.log');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('JIRA_LOG_FIL...'jira-rest-client.log') can also be of type boolean. However, the property $jiraLogFile 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...
28
        $this->jiraLogLevel = $this->env('JIRA_LOG_LEVEL', 'WARNING');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('JIRA_LOG_LEVEL', 'WARNING') can also be of type boolean. However, the property $jiraLogLevel 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...
29
        $this->curlOptSslVerifyHost = $this->env('CURLOPT_SSL_VERIFYHOST', false);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('CURLOPT_SSL_VERIFYHOST', false) can also be of type string. However, the property $curlOptSslVerifyHost is declared as type boolean. 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...
30
        $this->curlOptSslVerifyPeer = $this->env('CURLOPT_SSL_VERIFYPEER', false);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('CURLOPT_SSL_VERIFYPEER', false) can also be of type string. However, the property $curlOptSslVerifyPeer is declared as type boolean. 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...
31
        $this->curlOptUserAgent = $this->env('CURLOPT_USERAGENT', $this->getDefaultUserAgentString());
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('CURLOPT_USER...faultUserAgentString()) can also be of type boolean. However, the property $curlOptUserAgent 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...
32
        $this->curlOptVerbose = $this->env('CURLOPT_VERBOSE', false);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('CURLOPT_VERBOSE', false) can also be of type string. However, the property $curlOptVerbose is declared as type boolean. 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...
33
        $this->proxyServer = $this->env('PROXY_SERVER');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('PROXY_SERVER') can also be of type boolean. However, the property $proxyServer 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...
34
        $this->proxyPort = $this->env('PROXY_PORT');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('PROXY_PORT') can also be of type boolean. However, the property $proxyPort 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
        $this->proxyUser = $this->env('PROXY_USER');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('PROXY_USER') can also be of type boolean. However, the property $proxyUser 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...
36
        $this->proxyPassword = $this->env('PROXY_PASSWORD');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env('PROXY_PASSWORD') can also be of type boolean. However, the property $proxyPassword 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...
37
    }
38
39
    /**
40
     * Gets the value of an environment variable. Supports boolean, empty and null.
41
     *
42
     * @param string $key
43
     * @param mixed  $default
44
     *
45
     * @return mixed
46
     */
47
    private function env($key, $default = null)
48
    {
49
        $value = getenv($key);
50
51
        if ($value === false) {
52
            return $default;
53
        }
54
55
        switch (strtolower($value)) {
56
            case 'true':
57
            case '(true)':
58
                return true;
59
60
            case 'false':
61
            case '(false)':
62
                return false;
63
64
            case 'empty':
65
            case '(empty)':
66
                return '';
67
68
            case 'null':
69
            case '(null)':
70
                return;
71
        }
72
73
        if ($this->startsWith($value, '"') && $this->endsWith($value, '"')) {
74
            return substr($value, 1, -1);
75
        }
76
77
        return $value;
78
    }
79
80
    /**
81
     * Determine if a given string starts with a given substring.
82
     *
83
     * @param string       $haystack
84
     * @param string|array $needles
85
     *
86
     * @return bool
87
     */
88
    public function startsWith($haystack, $needles)
89
    {
90
        foreach ((array) $needles as $needle) {
91
            if ($needle != '' && strpos($haystack, $needle) === 0) {
92
                return true;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * Determine if a given string ends with a given substring.
101
     *
102
     * @param string       $haystack
103
     * @param string|array $needles
104
     *
105
     * @return bool
106
     */
107
    public function endsWith($haystack, $needles)
108
    {
109
        foreach ((array) $needles as $needle) {
110
            if ((string) $needle === substr($haystack, -strlen($needle))) {
111
                return true;
112
            }
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * load dotenv
120
     *
121
     * @param $path
122
     * @throws JiraException
123
     */
124
    private function loadDotEnv($path)
125
    {
126
        $requireParam = [
127
            'JIRA_HOST', 'JIRA_USER', 'JIRA_PASS',
128
        ];
129
130
        // support for dotenv 1.x and 2.x. see also https://github.com/lesstif/php-jira-rest-client/issues/102
131
        if (class_exists('\Dotenv\Dotenv')) {
132
133
            // dirty solution for check whether dotenv v3 or v2
134
            try {
135
                $method = new \ReflectionMethod('\Dotenv\Dotenv', 'create');
0 ignored issues
show
Unused Code introduced by
The assignment to $method is dead and can be removed.
Loading history...
136
137
                $dotenv = \Dotenv\Dotenv::create($path);
138
139
                $dotenv->safeLoad();
140
                $dotenv->required($requireParam);
141
            } catch (\ReflectionException $re) {
142
                // dotenv v2 doesn't have create method.
143
                $dotenv = new \Dotenv\Dotenv($path);
144
145
                $dotenv->load();
146
147
                $dotenv->required($requireParam);
148
            }
149
        } elseif (class_exists('\Dotenv')) {    // DotEnv v1
150
            \Dotenv::load($path);
151
            \Dotenv::required($requireParam);
152
        } else {
153
            throw new JiraException('can not load PHP dotenv class.!');
154
        }
155
    }
156
}
157