Completed
Pull Request — master (#924)
by Greg
02:39
created

Fixtures::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Robo;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
class Fixtures
8
{
9
    protected $testDir;
10
    protected $tmpDirs = [];
11
    protected $clonedRepos = [];
12
13
    /**
14
     * Fixtures constructor
15
     */
16
    public function __construct()
17
    {
18
        $testDir = false;
0 ignored issues
show
Unused Code introduced by
$testDir is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
19
    }
20
21
    /**
22
     * Clean up any temporary directories that may have been created
23
     */
24
    public function cleanup()
25
    {
26
        $fs = new Filesystem();
27
        foreach ($this->tmpDirs as $tmpDir) {
28
            $fs->remove($tmpDir);
29
        }
30
        $this->tmpDirs = [];
31
    }
32
33
    public function cloneRepo($url)
34
    {
35
        if (!isset($this->clonedRepos[$url])) {
36
            $this->clonedRepos[$url] = $this->doCloneRepo($url);
0 ignored issues
show
Bug introduced by
The method doCloneRepo() does not exist on Robo\Fixtures. Did you maybe mean cloneRepo()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
37
        }
38
        return $this->clonedRepos[$url];
39
    }
40
41
    /**
42
     * Create a new temporary directory.
43
     *
44
     * @param string|bool $basedir Where to store the temporary directory
45
     * @return type
46
     */
47
    public function mktmpdir($basedir = false)
48
    {
49
        $tempfile = tempnam($basedir ?: $this->testDir ?: sys_get_temp_dir(),'robo-tests');
50
        unlink($tempfile);
51
        mkdir($tempfile);
52
        $this->tmpDirs[] = $tempfile;
53
        return $tempfile;
54
    }
55
56
    public function createAndCdToSandbox()
57
    {
58
        $sourceSandbox = $this->sandboxDir();
59
        $targetSandbox = $this->mktmpdir();
60
        $fs = new Filesystem();
61
        $fs->mirror($sourceSandbox, $targetSandbox);
62
        chdir($targetSandbox);
63
64
        return $targetSandbox;
65
    }
66
67
    protected function fixturesDir()
68
    {
69
        return dirname(__DIR__) . '/_data';
70
    }
71
72
    protected function sandboxDir()
73
    {
74
        return $this->fixturesDir() . '/claypit';
75
    }
76
77
    protected function testDir()
78
    {
79
        if (!$this->testDir) {
80
            $this->testDir = $this->mktmpdir();
81
        }
82
        return $this->testDir;
83
    }
84
}
85