TmpFile::complete()   A
last analyzed

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\Task\File;
4
5
use Robo\Contract\CompletionInterface;
6
7
/**
8
 * Create a temporary file that is automatically cleaned up
9
 * once the task collection is is part of completes. When created,
10
 * it is given a random filename.
11
 *
12
 * This temporary file may be manipulated exacatly like taskWrite().
13
 * It is deleted as soon as the collection it is a part of completes
14
 * or rolls back.
15
 *
16
 * ``` php
17
 * <?php
18
 * $collection = $this->collectionBuilder();
19
 * $tmpFilePath = $collection->taskTmpFile()
20
 *      ->line('-----')
21
 *      ->line(date('Y-m-d').' '.$title)
22
 *      ->line('----')
23
 *      ->getPath();
24
 * $collection->run();
25
 * ?>
26
 * ```
27
 */
28
class TmpFile extends Write implements CompletionInterface
29
{
30
    /**
31
     * @param string $filename
32
     * @param string $extension
33
     * @param string $baseDir
34
     * @param bool $includeRandomPart
35
     */
36 View Code Duplication
    public function __construct($filename = 'tmp', $extension = '', $baseDir = '', $includeRandomPart = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
37
    {
38
        if (empty($baseDir)) {
39
            $baseDir = sys_get_temp_dir();
40
        }
41
        if ($includeRandomPart) {
42
            $random = static::randomString();
0 ignored issues
show
Bug introduced by
Since randomString() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of randomString() to at least protected.

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

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

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 {
      private static function getTemperature() {
        return "-182 °C";
    }
}

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

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

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
43
            $filename = "{$filename}_{$random}";
44
        }
45
        $filename .= $extension;
46
        parent::__construct("{$baseDir}/{$filename}");
47
    }
48
49
    /**
50
     * Generate a suitably random string to use as the suffix for our
51
     * temporary file.
52
     *
53
     * @param int $length
54
     *
55
     * @return string
56
     */
57
    private static function randomString($length = 12)
58
    {
59
        return substr(str_shuffle('23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'), 0, $length);
60
    }
61
62
    /**
63
     * Delete this file when our collection completes.
64
     * If this temporary file is not part of a collection,
65
     * then it will be deleted when the program terminates,
66
     * presuming that it was created by taskTmpFile() or _tmpFile().
67
     */
68
    public function complete()
69
    {
70
        unlink($this->getPath());
71
    }
72
}
73