Completed
Push — master ( 4a6b89...c84e82 )
by Andrii
17:16 queued 14s
created

Resolver::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\utils;
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
class Resolver
22
{
23
    protected $order = [];
24
25
    protected $deps = [];
26
27
    protected $following = [];
28
29
    public function __construct(array $files)
30
    {
31
        $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...
32
33
        $this->collectDeps();
34
        foreach (array_keys($this->files) as $name) {
35
            $this->followDeps($name);
36
        }
37
    }
38
39
    public function get()
40
    {
41
        $result = [];
42
        foreach ($this->order as $name) {
43
            $result[$name] = $this->resolveDeps($this->files[$name]);
44
        }
45
46
        return $result;
47
    }
48
49
    protected function resolveDeps(array $paths)
50
    {
51
        foreach ($paths as &$path) {
52
            $dep = $this->isDep($path);
53
            if ($dep) {
54
                $path = Builder::path($dep);
0 ignored issues
show
Bug introduced by
The type hiqdev\composer\config\utils\Builder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
            }
56
        }
57
58
        return $paths;
59
    }
60
61
    protected function followDeps($name)
62
    {
63
        if (isset($this->order[$name])) {
64
            return;
65
        }
66
        if (isset($this->following[$name])) {
67
            throw new CircularDependencyException($name . ' ' . implode(',', $this->following));
68
        }
69
        $this->following[$name] = $name;
70
        if (isset($this->deps[$name])) {
71
            foreach ($this->deps[$name] as $dep) {
72
                $this->followDeps($dep);
73
            }
74
        }
75
        $this->order[$name] = $name;
76
        unset($this->following[$name]);
77
    }
78
79
    protected function collectDeps()
80
    {
81
        foreach ($this->files as $name => $paths) {
82
            foreach ($paths as $path) {
83
                $dep = $this->isDep($path);
84
                if ($dep) {
85
                    if (!isset($this->deps[$name])) {
86
                        $this->deps[$name] = [];
87
                    }
88
                    $this->deps[$name][$dep] = $dep;
89
                }
90
            }
91
        }
92
    }
93
94
    protected function isDep($path)
95
    {
96
        return 0 === strncmp($path, '$', 1) ? substr($path, 1) : false;
97
    }
98
}
99