Env::__invoke()   C
last analyzed

Complexity

Conditions 14
Paths 12

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 26
nc 12
nop 3
dl 0
loc 34
rs 5.0864
c 0
b 0
f 0

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
 * Created by PhpStorm.
4
 * User: afshin
5
 * Date: 11/11/17
6
 * Time: 2:57 PM
7
 */
8
9
namespace Core\Helpers;
10
use Dotenv\Dotenv;
11
12
class Env
13
{
14
    public function __invoke($filePath ,$key, $default = null)
15
    {
16
        if (file_exists($filePath . '.env')) {
17
            $_dotenv = new Dotenv($filePath );
18
            $_dotenv->load();
19
            unset($_dotenv);
20
        }else {
21
            return '';
22
        }
23
        $value = getenv($key);
24
25
        if ($value === false) {
26
            return $default;
27
        }
28
        switch (strtolower($value)) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $str of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
        switch (strtolower(/** @scrutinizer ignore-type */ $value)) {
Loading history...
29
            case 'true':
30
            case '(true)':
31
                return true;
32
            case 'false':
33
            case '(false)':
34
                return false;
35
            case 'empty':
36
            case '(empty)':
37
                return '';
38
            case 'null':
39
            case '(null)':
40
                return null;
41
        }
42
        $strLen = strlen($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $strLen = strlen(/** @scrutinizer ignore-type */ $value);
Loading history...
43
44
        if ($strLen > 1 && $value[0] === '"' && $value[$strLen - 1] === '"') {
45
            return substr($value, 1, -1);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            return substr(/** @scrutinizer ignore-type */ $value, 1, -1);
Loading history...
46
        }
47
        return $value;
48
    }
49
}