Locator::getLocation()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 6
nop 2
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
ccs 8
cts 10
cp 0.8
crap 5.2
1
<?php
2
3
namespace Buttress\Concrete\Locator;
4
5
use Buttress\Concrete\Locator\Detector\Detector;
6
use Buttress\Concrete\Locator\Detector\LegacyDetector;
7
use Buttress\Concrete\Locator\Detector\ModernDetector;
8
9
/**
10
 * concrete5 installation locator
11
 */
12
class Locator
13
{
14
15
    /** @var \Buttress\Concrete\Locator\Site */
16
    protected $location;
17
18
    /** @var Detector[] */
19
    protected $detectors = [];
20
21
    /** @var \Buttress\Concrete\Locator\WebrootLocator */
22
    protected $locator;
23
24 12
    public function __construct(WebrootLocator $locator, ModernDetector $modern, LegacyDetector $legacy)
25
    {
26 12
        $this->locator = $locator;
27 12
        $this->addDetector($modern);
28 12
        $this->addDetector($legacy);
29 12
    }
30
31
    /**
32
     * Add a detector to detect with
33
     *
34
     * @param \Buttress\Concrete\Locator\Detector\Detector $detector
35
     */
36 12
    public function addDetector(Detector $detector)
37
    {
38 12
        $this->detectors[] = $detector;
39 12
    }
40
41
    /**
42
     * Locate the concrete5 core we're wanting
43
     *
44
     * @param string|null $currentPath If null is passed, getcwd will be used.
45
     * @param bool $recursive
46
     * @return \Buttress\Concrete\Locator\Site|null
47 9
     */
48
    public function getLocation($currentPath = null, $recursive = true)
49
    {
50 9
        // If we weren't given a currentpath, just use the cwd
51
        if (!$currentPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $currentPath of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
52
            $currentPath = getcwd();
53
        }
54
55 9
        // If we have found a webroot
56
        if ($path = $this->locator->searchWorkingDirectory($currentPath, $recursive)) {
57 9
            // Check each of the detectors to see if we've found a concrete5 site
58 9
            foreach ($this->detectors as $detector) {
59 6
                if ($result = $detector->detect($path)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $detector->detect($path) (which targets Buttress\Concrete\Locato...ctor\Detector::detect()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
                    return $result;
61 4
                }
62 2
            }
63
        }
64 3
65
        return null;
66
    }
67
}
68