Completed
Push — app ( d2c122...aa71e8 )
by Akihito
05:34
created

Unlink   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 0
dl 0
loc 42
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 10 5
A once() 0 10 2
A force() 0 7 1
1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package;
8
9
final class Unlink
10
{
11
    /**
12
     * @var array
13
     */
14
    private static $unlinkedPath = [];
15
16
    /**
17
     * @var bool
18
     */
19
    private $isOptional = true;
20
21 12
    public function __invoke(string $path)
22
    {
23 12
        if ($this->isOptional && file_exists($path . '/.do_not_clear')) {
24 7
            return;
25
        }
26 12
        foreach (\glob(\rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*') as $file) {
27 12
            \is_dir($file) ? $this->__invoke($file) : \unlink($file);
28 12
            @\rmdir($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
29
        }
30 12
    }
31
32 29
    public function once(string $path) : bool
33
    {
34 29
        if (in_array($path, self::$unlinkedPath, true)) {
35 24
            return true;
36
        }
37 7
        self::$unlinkedPath[] = $path;
38 7
        $this->__invoke($path);
39
40 7
        return false;
41
    }
42
43 9
    public function force(string $path) : bool
44
    {
45 9
        $this->isOptional = false;
46 9
        ($this)($path);
47
48 9
        return true;
49
    }
50
}
51