Resolver::isDep()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Composer plugin for config assembling
4
 *
5
 * @link      https://github.com/hiqdev/composer-config-plugin
6
 * @package   composer-config-plugin
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\composer\config;
12
13
use hiqdev\composer\config\exceptions\CircularDependencyException;
14
15
/**
16
 * Resolver class.
17
 * Reorders files according to their cross dependencies
18
 * and resolves `$name` pathes.
19
 * @author Andrii Vasyliev <[email protected]>
20
 *
21
 * @since php5.5
22
 */
23
class Resolver
24
{
25
    protected $order = [];
26
27
    protected $deps = [];
28
29
    protected $following = [];
30
31
    public function __construct(array $files)
32
    {
33
        $this->files = $files;
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
35
        $this->collectDeps();
36
        foreach (array_keys($this->files) as $name) {
37
            $this->followDeps($name);
38
        }
39
    }
40
41
    public function get()
42
    {
43
        $result = [];
44
        foreach ($this->order as $name) {
45
            $result[$name] = $this->resolveDeps($this->files[$name]);
46
        }
47
48
        return $result;
49
    }
50
51
    protected function resolveDeps(array $paths)
52
    {
53
        foreach ($paths as &$path) {
54
            $dep = $this->isDep($path);
55
            if ($dep) {
56
                $path = Builder::path($dep);
57
            }
58
        }
59
60
        return $paths;
61
    }
62
63
    protected function followDeps($name)
64
    {
65
        if (isset($this->order[$name])) {
66
            return;
67
        }
68
        if (isset($this->following[$name])) {
69
            throw new CircularDependencyException($name . ' ' . implode(',', $this->following));
70
        }
71
        $this->following[$name] = $name;
72
        if (isset($this->deps[$name])) {
73
            foreach ($this->deps[$name] as $dep) {
74
                $this->followDeps($dep);
75
            }
76
        }
77
        $this->order[$name] = $name;
78
        unset($this->following[$name]);
79
    }
80
81
    protected function collectDeps()
82
    {
83
        foreach ($this->files as $name => $paths) {
84
            foreach ($paths as $path) {
85
                $dep = $this->isDep($path);
86
                if ($dep) {
87
                    if (!isset($this->deps[$name])) {
88
                        $this->deps[$name] = [];
89
                    }
90
                    $this->deps[$name][$dep] = $dep;
91
                }
92
            }
93
        }
94
    }
95
96
    protected function isDep($path)
97
    {
98
        return 0 === strncmp($path, '$', 1) ? substr($path, 1) : false;
99
    }
100
}
101