SystemInformation   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getHomeDir() 0 23 7
A isWindows() 0 4 1
A useXdg() 0 9 3
A getUserDir() 0 8 2
1
<?php
2
3
namespace Consolidation\Cgr;
4
5
/**
6
 * Determine information about the system
7
 */
8
class SystemInformation
9
{
10
    /**
11
     * @throws \RuntimeException
12
     * @return string
13
     */
14
    public static function getHomeDir()
15
    {
16
        $home = getenv('COMPOSER_HOME');
17
        if ($home) {
18
            return $home;
19
        }
20
        if (self::isWindows()) {
21
            if (!getenv('APPDATA')) {
22
                throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly');
23
            }
24
            return rtrim(strtr(getenv('APPDATA'), '\\', '/'), '/') . '/Composer';
25
        }
26
        $userDir = self::getUserDir();
27
        if (is_dir($userDir . '/.composer')) {
28
            return $userDir . '/.composer';
29
        }
30
        if (self::useXdg()) {
31
            // XDG Base Directory Specifications
32
            $xdgConfig = getenv('XDG_CONFIG_HOME') ?: $userDir . '/.config';
33
            return $xdgConfig . '/composer';
34
        }
35
        return $userDir . '/.composer';
36
    }
37
38
    /**
39
     * @return bool Whether the host machine is running a Windows OS
40
     */
41
    public static function isWindows()
42
    {
43
        return defined('PHP_WINDOWS_VERSION_BUILD');
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    private static function useXdg()
50
    {
51
        foreach (array_keys($_SERVER) as $key) {
52
            if (substr($key, 0, 4) === 'XDG_') {
53
                return true;
54
            }
55
        }
56
        return false;
57
    }
58
59
    /**
60
     * @throws \RuntimeException
61
     * @return string
62
     */
63
    private static function getUserDir()
64
    {
65
        $home = getenv('HOME');
66
        if (!$home) {
67
            throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly');
68
        }
69
        return rtrim(strtr($home, '\\', '/'), '/');
70
    }
71
}
72