Passed
Push — master ( 724be3...506a08 )
by
unknown
03:41
created

DependencyComponent   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 95
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 41 6
A generateDependencyMatchPatterns() 0 32 5
A processNegatedDependencies() 0 15 3
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Patch\Definition\NormalizerComponents;
7
8
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
9
use Vaimo\ComposerPatches\Config as PluginConfig;
10
11
class DependencyComponent implements \Vaimo\ComposerPatches\Interfaces\DefinitionNormalizerComponentInterface
12
{
13
    public function normalize($target, $label, array $data, array $ownerConfig)
14
    {
15
        $depends = isset($data[PatchDefinition::DEPENDS])
16
            ? $data[PatchDefinition::DEPENDS]
17
            : array();
18
19
        if (isset($data[PatchDefinition::VERSION])) {
20
            if (!is_array($data[PatchDefinition::VERSION])) {
21
                $dependsTarget = $target;
22
23
                $dependsPatterns = $this->generateDependencyMatchPatterns($ownerConfig);
24
25
                if ($dependsPatterns) {
26
                    $matches = array_filter(
27
                        array_keys($dependsPatterns),
28
                        function ($pattern) use ($target) {
29
                            return preg_match('#' . $pattern . '#', $target);
30
                        }
31
                    );
32
33
                    if (!empty($matches)) {
34
                        $dependsTarget = $dependsPatterns[reset($matches)];
35
                    }
36
                }
37
38
                $data[PatchDefinition::VERSION] = array(
39
                    $dependsTarget => $data[PatchDefinition::VERSION]
40
                );
41
            }
42
43
            $depends = array_replace(
44
                $depends,
45
                $data[PatchDefinition::VERSION]
46
            );
47
        }
48
49
        // Handle negated dependencies in JSON config
50
        $depends = $this->processNegatedDependencies($depends);
51
52
        return array(
53
            PatchDefinition::DEPENDS => $depends
54
        );
55
    }
56
57
    private function generateDependencyMatchPatterns(array $config)
58
    {
59
        $dependsConfig = array();
60
61
        if (isset($config[PluginConfig::PATCHES_DEPENDS])) {
62
            $dependsConfig = $config[PluginConfig::PATCHES_DEPENDS];
63
64
            if (!is_array($dependsConfig)) {
65
                $dependsConfig = array(
66
                    PluginConfig::PATCHES_CONFIG_DEFAULT => $dependsConfig
67
                );
68
            }
69
        }
70
71
        $patterns = array_map(function ($candidate) {
72
            return trim($candidate, '*')
73
                ? str_replace(chr(32), '.*', preg_quote(str_replace('*', chr(32), $candidate), '#'))
74
                : preg_quote($candidate);
75
        }, array_keys($dependsConfig));
76
77
        $patternValues = array_combine($patterns, $dependsConfig);
78
79
        if (isset($patternValues[PluginConfig::PATCHES_CONFIG_DEFAULT])) {
80
            $patternValues = array_replace(
81
                $patternValues,
82
                array('.*' => $patternValues[PluginConfig::PATCHES_CONFIG_DEFAULT])
83
            );
84
85
            unset($patternValues[PluginConfig::PATCHES_CONFIG_DEFAULT]);
86
        }
87
88
        return $patternValues;
89
    }
90
91
    private function processNegatedDependencies(array $depends)
92
    {
93
        $processedDepends = array();
94
95
        foreach ($depends as $packageName => $version) {
96
            // Handle negated dependencies with ! prefix in JSON config
97
            if (strpos($packageName, '!') === 0) {
98
                $packageName = substr($packageName, 1);
99
                $version = array('version' => $version, 'negated' => true);
100
            }
101
102
            $processedDepends[$packageName] = $version;
103
        }
104
105
        return $processedDepends;
106
    }
107
}
108