Completed
Push — master ( 3a857c...52f6fa )
by Greg
8s
created

SystemInformation   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
C 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()
0 ignored issues
show
Coding Style introduced by
useXdg uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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