FileSystemUtils   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A applyDir() 0 10 2
A mkdirParents() 0 10 3
A listDirectories() 0 6 2
A allInstalledProjectsInBaseDir() 0 11 2
A allInstalledProjectsInOneOrg() 0 14 3
1
<?php
2
3
namespace Consolidation\Cgr;
4
5
/**
6
 * A few convenience utility functions for filesystem operations.
7
 */
8
class FileSystemUtils
9
{
10
    /**
11
     * Set the current working directory if it was specified.
12
     */
13
    public static function applyDir($dir)
14
    {
15
        if (empty($dir)) {
16
            return $dir;
17
        }
18
        $origDir = getcwd();
19
        static::mkdirParents($dir);
20
        chdir($dir);
21
        return $origDir;
22
    }
23
24
    /**
25
     * Create a directory at the specified path. Also create any parent
26
     * directories that do not yet exist.
27
     *
28
     * @param $path The directory path to create.
29
     * @return boolean
30
     */
31
    public static function mkdirParents($path)
32
    {
33
        if (is_dir($path)) {
34
            return true;
35
        }
36
37
        if (static::mkdirParents(dirname($path))) {
38
            return mkdir($path);
39
        }
40
    }
41
42
    /**
43
     * Return all of the directories in a given directory
44
     *
45
     * @param string $d directory to scann
46
     * @return string[]
47
     */
48
    public static function listDirectories($d)
49
    {
50
        return array_filter(scandir($d), function ($f) use ($d) {
51
            return is_dir($d . DIRECTORY_SEPARATOR . $f) && ($f[0] != '.');
52
        });
53
    }
54
55
56
    /**
57
     * Find all installed projects in the global 'base-dir'.
58
     *
59
     * @param string $globalBaseDir
60
     * @return string[]
61
     */
62
    public static function allInstalledProjectsInBaseDir($globalBaseDir)
63
    {
64
        $projects = array();
65
66
        $orgs = static::listDirectories($globalBaseDir);
67
        foreach ($orgs as $org) {
68
            $projects = array_merge($projects, static::allInstalledProjectsInOneOrg($globalBaseDir, $org));
69
        }
70
71
        return $projects;
72
    }
73
74
    /**
75
     * Find all installed projects in one organization dir.
76
     *
77
     * @param string $globalOrgDir
0 ignored issues
show
Bug introduced by
There is no parameter named $globalOrgDir. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
78
     * @return string[]
79
     */
80
    protected static function allInstalledProjectsInOneOrg($globalBaseDir, $org)
81
    {
82
        $globalOrgDir = "$globalBaseDir/$org";
83
        $projects = array();
84
85
        $projectDirs = static::listDirectories($globalOrgDir);
86
        foreach ($projectDirs as $projectDir) {
87
            if (is_file("$globalOrgDir/$projectDir/composer.json")) {
88
                $projects[] = "$org/$projectDir";
89
            }
90
        }
91
92
        return $projects;
93
    }
94
}
95