InstalledVersions   C
last analyzed

Complexity

Total Complexity 54

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 54
eloc 96
dl 0
loc 199
rs 6.4799
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isInstalled() 0 8 5
A getRawData() 0 11 3
A getInstalledPackagesByType() 0 11 5
A getRootPackage() 0 4 1
A satisfies() 0 5 1
A getPrettyVersion() 0 12 4
B getVersionRanges() 0 22 7
A reload() 0 4 1
A getInstallPath() 0 9 4
A getReference() 0 12 4
A getAllRawData() 0 3 1
A getVersion() 0 12 4
A getInstalledPackages() 0 10 3
B getInstalled() 0 31 11

How to fix   Complexity   

Complex Class

Complex classes like InstalledVersions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use InstalledVersions, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace HumbugBox451\Composer;
4
5
use HumbugBox451\Composer\Autoload\ClassLoader;
0 ignored issues
show
Bug introduced by
The type HumbugBox451\Composer\Autoload\ClassLoader 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...
6
use HumbugBox451\Composer\Semver\VersionParser;
7
/** @internal */
8
class InstalledVersions
9
{
10
    /**
11
    @psalm-var
12
    */
13
    private static $installed;
14
    private static $canGetVendors;
15
    /**
16
    @psalm-var
17
    */
18
    private static $installedByVendor = array();
19
    /**
20
    @psalm-return
21
    */
22
    public static function getInstalledPackages()
23
    {
24
        $packages = array();
25
        foreach (self::getInstalled() as $installed) {
26
            $packages[] = \array_keys($installed['versions']);
27
        }
28
        if (1 === \count($packages)) {
29
            return $packages[0];
30
        }
31
        return \array_keys(\array_flip(\call_user_func_array('HumbugBox451\\array_merge', $packages)));
32
    }
33
    /**
34
    @psalm-return
35
    */
36
    public static function getInstalledPackagesByType($type)
37
    {
38
        $packagesByType = array();
39
        foreach (self::getInstalled() as $installed) {
40
            foreach ($installed['versions'] as $name => $package) {
41
                if (isset($package['type']) && $package['type'] === $type) {
42
                    $packagesByType[] = $name;
43
                }
44
            }
45
        }
46
        return $packagesByType;
47
    }
48
    public static function isInstalled($packageName, $includeDevRequirements = \true)
49
    {
50
        foreach (self::getInstalled() as $installed) {
51
            if (isset($installed['versions'][$packageName])) {
52
                return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === \false;
53
            }
54
        }
55
        return \false;
56
    }
57
    public static function satisfies(VersionParser $parser, $packageName, $constraint)
58
    {
59
        $constraint = $parser->parseConstraints((string) $constraint);
60
        $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
61
        return $provided->matches($constraint);
62
    }
63
    public static function getVersionRanges($packageName)
64
    {
65
        foreach (self::getInstalled() as $installed) {
66
            if (!isset($installed['versions'][$packageName])) {
67
                continue;
68
            }
69
            $ranges = array();
70
            if (isset($installed['versions'][$packageName]['pretty_version'])) {
71
                $ranges[] = $installed['versions'][$packageName]['pretty_version'];
72
            }
73
            if (\array_key_exists('aliases', $installed['versions'][$packageName])) {
74
                $ranges = \array_merge($ranges, $installed['versions'][$packageName]['aliases']);
75
            }
76
            if (\array_key_exists('replaced', $installed['versions'][$packageName])) {
77
                $ranges = \array_merge($ranges, $installed['versions'][$packageName]['replaced']);
78
            }
79
            if (\array_key_exists('provided', $installed['versions'][$packageName])) {
80
                $ranges = \array_merge($ranges, $installed['versions'][$packageName]['provided']);
81
            }
82
            return \implode(' || ', $ranges);
83
        }
84
        throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
85
    }
86
    public static function getVersion($packageName)
87
    {
88
        foreach (self::getInstalled() as $installed) {
89
            if (!isset($installed['versions'][$packageName])) {
90
                continue;
91
            }
92
            if (!isset($installed['versions'][$packageName]['version'])) {
93
                return null;
94
            }
95
            return $installed['versions'][$packageName]['version'];
96
        }
97
        throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
98
    }
99
    public static function getPrettyVersion($packageName)
100
    {
101
        foreach (self::getInstalled() as $installed) {
102
            if (!isset($installed['versions'][$packageName])) {
103
                continue;
104
            }
105
            if (!isset($installed['versions'][$packageName]['pretty_version'])) {
106
                return null;
107
            }
108
            return $installed['versions'][$packageName]['pretty_version'];
109
        }
110
        throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
111
    }
112
    public static function getReference($packageName)
113
    {
114
        foreach (self::getInstalled() as $installed) {
115
            if (!isset($installed['versions'][$packageName])) {
116
                continue;
117
            }
118
            if (!isset($installed['versions'][$packageName]['reference'])) {
119
                return null;
120
            }
121
            return $installed['versions'][$packageName]['reference'];
122
        }
123
        throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
124
    }
125
    public static function getInstallPath($packageName)
126
    {
127
        foreach (self::getInstalled() as $installed) {
128
            if (!isset($installed['versions'][$packageName])) {
129
                continue;
130
            }
131
            return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
132
        }
133
        throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
134
    }
135
    /**
136
    @psalm-return
137
    */
138
    public static function getRootPackage()
139
    {
140
        $installed = self::getInstalled();
141
        return $installed[0]['root'];
142
    }
143
    /**
144
    @psalm-return
145
    */
146
    public static function getRawData()
147
    {
148
        @\trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', \E_USER_DEPRECATED);
149
        if (null === self::$installed) {
150
            if (\substr(__DIR__, -8, 1) !== 'C') {
151
                self::$installed = (include __DIR__ . '/installed.php');
152
            } else {
153
                self::$installed = array();
154
            }
155
        }
156
        return self::$installed;
157
    }
158
    /**
159
    @psalm-return
160
    */
161
    public static function getAllRawData()
162
    {
163
        return self::getInstalled();
164
    }
165
    /**
166
    @psalm-param
167
    */
168
    public static function reload($data)
169
    {
170
        self::$installed = $data;
171
        self::$installedByVendor = array();
172
    }
173
    /**
174
    @psalm-return
175
    */
176
    private static function getInstalled()
177
    {
178
        if (null === self::$canGetVendors) {
179
            self::$canGetVendors = \method_exists('HumbugBox451\\Composer\\Autoload\\ClassLoader', 'getRegisteredLoaders');
180
        }
181
        $installed = array();
182
        if (self::$canGetVendors) {
183
            foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
184
                if (isset(self::$installedByVendor[$vendorDir])) {
185
                    $installed[] = self::$installedByVendor[$vendorDir];
186
                } elseif (\is_file($vendorDir . '/composer/installed.php')) {
187
                    $required = (require $vendorDir . '/composer/installed.php');
188
                    $installed[] = self::$installedByVendor[$vendorDir] = $required;
189
                    if (null === self::$installed && \strtr($vendorDir . '/composer', '\\', '/') === \strtr(__DIR__, '\\', '/')) {
190
                        self::$installed = $installed[\count($installed) - 1];
191
                    }
192
                }
193
            }
194
        }
195
        if (null === self::$installed) {
196
            if (\substr(__DIR__, -8, 1) !== 'C') {
197
                $required = (require __DIR__ . '/installed.php');
198
                self::$installed = $required;
199
            } else {
200
                self::$installed = array();
201
            }
202
        }
203
        if (self::$installed !== array()) {
204
            $installed[] = self::$installed;
205
        }
206
        return $installed;
207
    }
208
}
209