Passed
Push — master ( a52b81...a1d8bc )
by Allan
02:35
created

PackageUtils::getPrettyVersion()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 6
nop 1
dl 0
loc 15
rs 10
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\Utils;
7
8
use Composer\Package\PackageInterface;
9
use Vaimo\ComposerPatches\Config as PluginConfig;
10
11
class PackageUtils
12
{
13
    public function getRealPackage(PackageInterface $package)
14
    {
15
        while ($package instanceof \Composer\Package\AliasPackage) {
16
            $package = $package->getAliasOf();
17
        }
18
19
        return $package;
20
    }
21
    
22
    public function hasPatchChanges(PackageInterface $package, array $patches)
23
    {
24
        $extra = $package->getExtra();
25
26
        if (isset($extra[PluginConfig::APPLIED_FLAG])) {
27
            $appliedPatches = $extra[PluginConfig::APPLIED_FLAG];
28
29
            if ($appliedPatches === true) {
30
                return true;
31
            }
32
33
            return array_diff_assoc($appliedPatches, $patches)
34
                || array_diff_assoc($patches, $appliedPatches);
35
        }
36
37
        return (bool)count($patches);
38
    }
39
40
    public function resetAppliedPatches(PackageInterface $package, $replacement = null)
41
    {
42
        $patchesApplied = $this->getAppliedPatches($package);
43
44
        $extra = $package->getExtra();
45
46
        unset($extra[PluginConfig::APPLIED_FLAG]);
47
48
        if ($patchesApplied && $replacement !== null) {
49
            $extra[PluginConfig::APPLIED_FLAG] = $replacement;
50
        }
51
52
        if (method_exists($package, 'setExtra')) {
53
            $package->setExtra($extra);
54
        }
55
56
        return $patchesApplied;
57
    }
58
59
    public function getAppliedPatches(PackageInterface $package)
60
    {
61
        $extra = $package->getExtra();
62
63
        return isset($extra[PluginConfig::APPLIED_FLAG])
64
            ? $extra[PluginConfig::APPLIED_FLAG]
65
            : array();
66
    }
67
68
    public function getPrettyVersion($package)
69
    {
70
        while ($package instanceof \Composer\Package\AliasPackage) {
71
            $package = $package->getAliasOf();
72
        }
73
        
74
        if (method_exists($package, 'getPrettyVersion')) {
75
            return $package->getPrettyVersion();
76
        }
77
        
78
        if (method_exists($package, 'getPrettyConstraint')) {
79
            return $package->getPrettyConstraint();
80
        }
81
82
        return '';
83
    }
84
}
85