MigrantUtil::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
            echo "adding $package\n";
65 2
            $this->setMergedPackages($mergedPackages);
66 2
            $packages = $this->getDependencies($package);
67 2
            if (count($packages) > 0) {
68 1
                $this->processDependencies($packages);
69
            }
70
        }
71 2
    }
72
73
    /**
74
     * @param $package
75
     * @return array
76
     */
77 2
    private function getDependencies(string $package): array
78
    {
79 2
        $srcFolder = getcwd() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package . DIRECTORY_SEPARATOR;
80 2 View Code Duplication
        if (file_exists($srcFolder . '.migrant')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81 2
            $depend = require($srcFolder . '.migrant');
82
83 2
            return isset($depend['packages']) ? $depend['packages'] : [];
84
        }
85
86
        try {
87
            if (class_exists($package)) {
88
                $mirror = new ReflectionClass($package);
89
                $location = $mirror->getFileName();
90
91
                if (false !== strpos($location, 'vendor') && preg_match('#(?<packagePath>.+)/src/.+\.php#', $location, $match)) {
92
                    $path = $match['packagePath'] . DIRECTORY_SEPARATOR . '.migrant';
93
94 View Code Duplication
                    if (file_exists($path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                        $depend = require_once $path;
96
97
                        return isset($depend['packages']) ? $depend['packages'] : [];
98
                    }
99
                }
100
            }
101
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
102
103
        }
104
105
        return [];
106
    }
107
}