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

HasBasePathTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 44
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setBasePath() 0 3 1
A detectAppPath() 0 14 3
A getBasePath() 0 6 2
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
}