Completed
Push — master ( 931184...014faa )
by Derek Stephen
01:45
created

MigrantUtil::getDependencies()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 37.0965

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 6
cts 17
cp 0.3529
rs 7.6666
c 0
b 0
f 0
cc 10
nc 17
nop 1
crap 37.0965

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
80 2
        if (file_exists($srcFolder . '.migrant')) {
81
            $depend = require($srcFolder . '.migrant');
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
                if (false !== strpos($location, 'vendor') && preg_match('#(?<packagePath>.+)/src/.+\.php#', $location, $match)) {
95
                    $path = $match['packagePath'] . DIRECTORY_SEPARATOR . '.migrant';
96
                    if (file_exists($path)) {
97
                        $depend = require_once $path;
98
99 2
                        return isset($depend['packages']) ? $depend['packages'] : [];
100
                    }
101
                }
102
            }
103
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
104
105
        }
106
107 2
        return [];
108
    }
109
}