Passed
Push — master ( 28f242...472b4c )
by Gabriel
04:44
created

HasBasePathTrait::detectAppPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 14
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Migrations\Utility\Traits;
4
5
/**
6
 * Trait HasBasePathTrait
7
 * @package ByTIC\Migrations\Utility\Traits
8
 */
9
trait HasBasePathTrait
10
{
11
12
    /**
13
     * @var null|string
14
     */
15
    protected static $basePath = null;
16
17
    /**
18
     * @return null|string
19
     */
20 9
    public static function getBasePath()
21
    {
22 9
        if (static::$basePath === null) {
23 7
            static::$basePath = static::detectAppPath();
24
        }
25 9
        return static::$basePath;
26
    }
27
28
    /**
29
     * @param string|null $basePath
30
     */
31 9
    public static function setBasePath($basePath)
32
    {
33 9
        self::$basePath = $basePath;
34 9
    }
35
36
    /**
37
     * @return null|string
38
     */
39 7
    protected static function detectAppPath()
40
    {
41 7
        $basePath = __DIR__;
42
43 7
        $maxStep = 5;
44 7
        $step = 0;
45 7
        while ($step <= $maxStep) {
46 7
            $basePath = dirname($basePath);
47 7
            if (file_exists($basePath . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php')) {
48 7
                return $basePath;
49
            }
50 7
            $step++;
51
        }
52
        return null;
53
    }
54
}