Passed
Push — master ( 1f0b7e...5bb3b6 )
by Allan
02:45
created

SorterComponent::sortPackagePatches()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 9
nop 1
dl 0
loc 28
rs 9.4555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Patch\DefinitionList\LoaderComponents;
7
8
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
9
10
class SorterComponent implements \Vaimo\ComposerPatches\Interfaces\DefinitionListLoaderComponentInterface
11
{
12
    /**
13
     * @var \Vaimo\ComposerPatches\Utils\FilterUtils
14
     */
15
    private $filterUtils;
16
    
17
    public function __construct()
18
    {
19
        $this->filterUtils = new \Vaimo\ComposerPatches\Utils\FilterUtils();
20
    }
21
22
    /**
23
     * @param array $patches
24
     * @param \Composer\Package\PackageInterface[] $packagesByName
25
     * @return array
26
     */
27
    public function process(array $patches, array $packagesByName)
28
    {
29
        $sortKeys = array(PatchDefinition::BEFORE, PatchDefinition::AFTER);
30
        
31
        foreach ($patches as $patchTarget => $packagePatches) {
32
            foreach ($packagePatches as $patchPath => $patchInfo) {
33
                $otherPatches = array_diff(array_keys($packagePatches), array($patchPath));
34
35
                if (!$otherPatches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $otherPatches of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36
                    continue;
37
                }
38
                
39
                foreach ($sortKeys as $sortKey) {
40
                    if (!$patchInfo[$sortKey]) {
41
                        continue;
42
                    }
43
44
                    $filter = $this->filterUtils->composeRegex($patchInfo[$sortKey], '/');
45
46
                    $packagePatches[$patchPath][$sortKey] = preg_grep($filter, $otherPatches);
47
                }
48
            }
49
50
            $patches[$patchTarget] = $this->sortPackagePatches($packagePatches);
51
        }
52
53
        return $patches;
54
    }
55
    
56
    private function sortPackagePatches($packagePatches)
57
    {
58
        $patchDependencies = array_fill_keys(array_keys($packagePatches), array());
59
60
        foreach ($packagePatches as $patchPath => $patchInfo) {
61
            $patchDependencies[$patchPath] = array_merge(
62
                $patchDependencies[$patchPath],
63
                $patchInfo[PatchDefinition::AFTER]
64
            );
65
66
            foreach ($patchInfo[PatchDefinition::BEFORE] as $beforePath) {
67
                $patchDependencies[$beforePath][] = $patchPath;
68
            }
69
        }
70
71
        if (!array_filter($patchDependencies)) {
72
            return $packagePatches;
73
        }
74
75
        $sorter = new \MJS\TopSort\Implementations\StringSort();
0 ignored issues
show
Bug introduced by
The type MJS\TopSort\Implementations\StringSort 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...
76
77
        foreach ($patchDependencies as $path => $depends) {
78
            $sorter->add($path, array_unique($depends));
79
        }
80
81
        return array_replace(
82
            array_flip($sorter->sort()),
83
            $packagePatches
84
        );
85
    }
86
}
87