Completed
Push — master ( 2c60e2...52d276 )
by Derek Stephen
01:52
created

MigrantUtil::getDependencies()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 28.656

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 6
cts 14
cp 0.4286
rs 7.6666
c 0
b 0
f 0
cc 10
nc 14
nop 1
crap 28.656

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
namespace Del\Common\Util;
4
5
use Exception;
6
use ReflectionClass;
7
8
/**
9
 * User: delboy1978uk
10
 * Date: 15/10/2016
11
 * Time: 16:18
12
 */
13
class MigrantUtil
14
{
15
    /**
16
     * @var array
17
     */
18
    private $mergedPackages;
19
20 2
    public function __construct()
21
    {
22 2
        $this->setMergedPackages([]);
23 2
    }
24
25
    /**
26
     * @return array
27
     */
28 2
    private function getMergedPackages()
29
    {
30 2
        return $this->mergedPackages;
31
    }
32
33
    /**
34
     * @param array $mergedPackages
35
     * @return MigrantUtil
36
     */
37 2
    private function setMergedPackages($mergedPackages)
38
    {
39 2
        $this->mergedPackages = $mergedPackages;
40 2
        return $this;
41
    }
42
43
    /**
44
     * @param array $packages
45
     * @return array
46
     */
47 2
    public function processDependencies(array $packages): array
48
    {
49 2
        foreach ($packages as $package) {
50 2
            $this->processPackage($package);
51
        }
52
53 2
        return $this->getMergedPackages();
54
    }
55
56
    /**
57
     * @param $package
58
     */
59 2
    private function processPackage($package)
60
    {
61 2
        $mergedPackages = $this->getMergedPackages();
62 2
        if (!in_array($package, $mergedPackages)) {
63 2
            $mergedPackages[] = $package;
64 2
            $this->setMergedPackages($mergedPackages);
65 2
            $packages = $this->getDependencies($package);
66 2
            if (count($packages) > 0) {
67
                $this->processDependencies($packages);
68
            }
69
        }
70 2
    }
71
72
    /**
73
     * @param $package
74
     * @return array
75
     */
76 2
    private function getDependencies(string $package): array
77
    {
78 2
        $srcFolder = 'vendor' . DIRECTORY_SEPARATOR . $package . DIRECTORY_SEPARATOR;
79 2
        if (file_exists($srcFolder . '.migrant')) {
80
            die($srcFolder . '.migrant exists');
81
            $depend = require($srcFolder . '.migrant');
0 ignored issues
show
Unused Code introduced by
$depend = (require $srcFolder . '.migrant'); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
82
83
            if (file_exists('.migrant_local')) {
84
                $depend = array_merge($depend, require_once '.migrant_local');
85
            }
86
87
            return isset($depend['packages']) ? $depend['packages'] : [];
88
        }
89
90
        try {
91 2
            if (class_exists($package)) {
92
                $mirror = new ReflectionClass($package);
93
                $location = $mirror->getFileName();
94
95
                if (false !== strpos($location, 'vendor') && preg_match('#(?<packagePath>.+)/src/.+\.php#', $location, $match)) {
96
                    $path = $match['packagePath'] . DIRECTORY_SEPARATOR . '.migrant';
97
98
                    if (file_exists($path)) {
99
                        $depend = require_once $path;
100
101 2
                        return isset($depend['packages']) ? $depend['packages'] : [];
102
                    }
103
                }
104
            }
105
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
106
107
        }
108
109 2
        return [];
110
    }
111
}