Completed
Pull Request — master (#86)
by Alessandro
05:32
created

TempDirectory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 6.41 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 85.19%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 5
loc 78
ccs 23
cts 27
cp 0.8519
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTempDirForThisExecution() 0 10 1
B getTempBaseDir() 0 22 4
A mkdirIfNotExists() 5 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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