Passed
Pull Request — master (#407)
by Kirill
05:31
created

TestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 70
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanTempDirectory() 0 24 5
A tearDown() 0 5 1
A setUp() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage;
6
7
use League\Flysystem\Local\LocalFilesystemAdapter;
8
use PHPUnit\Framework\TestCase as BaseTestCase;
9
use Spiral\Storage\Storage;
10
use Spiral\Storage\StorageInterface;
11
12
class TestCase extends BaseTestCase
13
{
14
    /**
15
     * @var string
16
     */
17
    protected const LOCAL_STORAGE_DIRECTORY = __DIR__ . '/storage';
18
19
    /**
20
     * @var StorageInterface
21
     */
22
    protected $local;
23
24
    /**
25
     * @var StorageInterface
26
     */
27
    protected $second;
28
29
    /**
30
     * @return void
31
     */
32
    public function setUp(): void
33
    {
34
        parent::setUp();
35
36
        $this->local = Storage::fromAdapter(
37
            new LocalFilesystemAdapter(self::LOCAL_STORAGE_DIRECTORY)
38
        );
39
40
        $this->second = Storage::fromAdapter(
41
            new LocalFilesystemAdapter(self::LOCAL_STORAGE_DIRECTORY . '/second')
42
        );
43
    }
44
45
    /**
46
     * @return void
47
     */
48
    public function tearDown(): void
49
    {
50
        $this->cleanTempDirectory();
51
52
        parent::tearDown();
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    protected function cleanTempDirectory(): void
59
    {
60
        $iterator = new \RecursiveIteratorIterator(
61
            new \RecursiveDirectoryIterator(self::LOCAL_STORAGE_DIRECTORY, \FilesystemIterator::SKIP_DOTS),
62
            \RecursiveIteratorIterator::CHILD_FIRST
63
        );
64
65
        /** @var \SplFileInfo $file */
66
        foreach($iterator as $file) {
67
            if ($file->getFilename() === '.gitignore') {
68
                continue;
69
            }
70
71
            \error_clear_last();
72
73
            if ($file->isDir()) {
74
                @\rmdir($file->getPathname());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

74
                /** @scrutinizer ignore-unhandled */ @\rmdir($file->getPathname());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
75
            } else {
76
                @\unlink($file->getPathname());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

76
                /** @scrutinizer ignore-unhandled */ @\unlink($file->getPathname());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
77
            }
78
79
            if ($error = \error_get_last()) {
80
                $prefix = 'An error occurred while clear temporary local storage directory: ';
81
                $this->addWarning($prefix . $error['message']);
82
            }
83
        }
84
    }
85
}
86