Completed
Pull Request — master (#5)
by Greg
02:03
created

FileSystemUtils::listDirectories()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 1
nop 1
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
       return array_filter(scandir($d), function ($f) use($d) {
50
           return is_dir($d . DIRECTORY_SEPARATOR . $f) && ($f[0] != '.');
51
       });
52
    }
53
54
55
    /**
56
     * Find all installed projects in the global 'base-dir'.
57
     *
58
     * @param array $globalBaseDir
59
     * @return string[]
60
     */
61
    public static function allInstalledProjectsInBaseDir($globalBaseDir)
62
    {
63
        $projects = array();
64
65
        $orgs = static::listDirectories($globalBaseDir);
0 ignored issues
show
Documentation introduced by
$globalBaseDir is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        foreach ($orgs as $org) {
67
            $projects = array_merge($projects, static::allInstalledProjectsInOneOrg($globalBaseDir, $org));
68
        }
69
70
        return $projects;
71
    }
72
73
    /**
74
     * Find all installed projects in one organization dir.
75
     *
76
     * @param array $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...
77
     * @return string[]
78
     */
79
    protected static function allInstalledProjectsInOneOrg($globalBaseDir, $org)
80
    {
81
        $globalOrgDir = "$globalBaseDir/$org";
82
        $projects = array();
83
84
        $projectDirs = static::listDirectories($globalOrgDir);
85
        foreach ($projectDirs as $projectDir) {
86
            if (is_file("$globalOrgDir/$projectDir/composer.json")) {
87
                $projects[] = "$org/$projectDir";
88
            }
89
        }
90
91
        return $projects;
92
    }
93
}
94