HasBasePathTrait::detectAppPath()   A
last analyzed

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 12
    public static function getBasePath()
21
    {
22 12
        if (static::$basePath === null) {
23 10
            static::$basePath = static::detectAppPath();
24
        }
25 12
        return static::$basePath;
26
    }
27
28
    /**
29
     * @param string|null $basePath
30
     */
31 12
    public static function setBasePath($basePath)
32
    {
33 12
        self::$basePath = $basePath;
34 12
    }
35
36
    /**
37
     * @return null|string
38
     */
39 10
    protected static function detectAppPath()
40
    {
41 10
        $basePath = __DIR__;
42
43 10
        $maxStep = 5;
44 10
        $step = 0;
45 10
        while ($step <= $maxStep) {
46 10
            $basePath = dirname($basePath);
47 10
            if (file_exists($basePath . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php')) {
48 10
                return $basePath;
49
            }
50 10
            $step++;
51
        }
52
        return null;
53
    }
54
}