Completed
Pull Request — master (#86)
by Alessandro
04:41
created

TempDirectory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Paraunit\File;
4
5
/**
6
 * Class TempDirectory
7
 * @package Paraunit\File
8
 */
9
class TempDirectory
10
{
11
    /** @var string[] */
12
    private static $tempDirs = array(
13
        '/dev/shm',
14
    );
15
16
    /** @var string */
17
    private static $timestamp;
18
19
    /**
20
     * Paraunit constructor.
21
     */
22 57
    public function __construct()
23
    {
24 57
        static::$timestamp = uniqid(date('Ymd-His'), true);
0 ignored issues
show
Bug introduced by
Since $timestamp is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $timestamp to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
25 57
    }
26
27
    /**
28
     * @return string
29
     */
30 49
    public function getTempDirForThisExecution()
31
    {
32 49
        $dir = self::getTempBaseDir() . DIRECTORY_SEPARATOR . static::$timestamp;
0 ignored issues
show
Bug introduced by
Since $timestamp is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $timestamp to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
33 49
        self::mkdirIfNotExists($dir);
34 49
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'config');
35 49
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'logs');
36 49
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'coverage');
37
38 49
        return $dir;
39
    }
40
41
    /**
42
     * @return string
43
     *
44
     * @throws \RuntimeException
45
     */
46 49
    public static function getTempBaseDir()
47
    {
48 49
        $dirs = self::$tempDirs;
49
        // Fallback to sys temp dir
50 49
        $dirs[] = sys_get_temp_dir();
51
52 49
        foreach ($dirs as $directory) {
53 49
            if (file_exists($directory)) {
54 49
                $baseDir = $directory . DIRECTORY_SEPARATOR . 'paraunit';
55
56
                try {
57 49
                    self::mkdirIfNotExists($baseDir);
58
59 49
                    return $baseDir;
60
                } catch (\RuntimeException $e) {
61
                    // ignore and try next dir
62
                }
63
            }
64
        }
65
66
        throw new \RuntimeException('Unable to create a temporary directory');
67
    }
68
69
    /**
70
     * @param string $path
71
     *
72
     * @throws \RuntimeException
73
     */
74 49
    private static function mkdirIfNotExists($path)
75
    {
76 49
        if (file_exists($path)) {
77 49
            return;
78
        }
79
80 49 View Code Duplication
        if (!mkdir($path) && !is_dir($path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            throw new \RuntimeException(
82
                sprintf('Unable to create temporary directory \'%s\'.', $path)
83
            );
84
        }
85 49
    }
86
}
87