Completed
Push — master ( b02ee6...031221 )
by Marcel
02:16
created

Folder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 30.11 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 6
Bugs 4 Features 2
Metric Value
wmc 14
lcom 0
cbo 2
dl 28
loc 93
rs 10
c 6
b 4
f 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ensure() 14 14 3
A delete() 14 14 3
C deleteContents() 0 36 8

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
namespace nochso\Omni;
3
4
/**
5
 * Folder handles file system folders.
6
 */
7
final class Folder
8
{
9
    /**
10
     * Ensure a folder exists by creating it if missing and throw an exception on failure.
11
     *
12
     * @param string $path
13
     * @param int    $mode Optional, defaults to 0777.
14
     *
15
     * @throws \RuntimeException
16
     */
17 View Code Duplication
    public static function ensure($path, $mode = 0777)
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...
18
    {
19
        if (is_dir($path)) {
20
            return;
21
        }
22
        if (@mkdir($path, $mode, true)) {
23
            return;
24
        }
25
        throw new \RuntimeException(sprintf(
26
            "Unable to create folder '%s': %s",
27
            $path,
28
            error_get_last()['message']
29
        ));
30
    }
31
32
    /**
33
     * Delete a directory and all of its contents recursively.
34
     *
35
     * @param string $path Path of folder to delete
36
     *
37
     * @throws \RuntimeException Thrown when something could not be deleted.
38
     */
39 View Code Duplication
    public static function delete($path)
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...
40
    {
41
        if (!is_dir($path)) {
42
            return;
43
        }
44
        self::deleteContents($path);
45
        if (@rmdir($path) === false) {
46
            throw new \RuntimeException(sprintf(
47
                "Unable to delete folder '%s': %s",
48
                $path,
49
                error_get_last()['message']
50
            ));
51
        }
52
    }
53
54
    /**
55
     * deleteContents of a folder recursively, but not the folder itself.
56
     *
57
     * On Windows systems this will try to remove the read-only attribute if needed.
58
     *
59
     * @param string $path Path of folder whose contents will be deleted
60
     *
61
     * @throws \RuntimeException Thrown when something could not be deleted.
62
     */
63
    public static function deleteContents($path)
64
    {
65
        if (!is_dir($path)) {
66
            return;
67
        }
68
        /** @var \SplFileInfo[] $files */
69
        $files = new \RecursiveIteratorIterator(
70
            new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
71
            \RecursiveIteratorIterator::CHILD_FIRST
72
        );
73
        foreach ($files as $fileInfo) {
74
            if ($fileInfo->isDir()) {
75
                if (@rmdir($fileInfo->getRealPath()) === false) {
76
                    throw new \RuntimeException(sprintf(
77
                        "Unable to delete child folder '%s' in '%s': %s",
78
                        $fileInfo->getRealPath(),
79
                        $path,
80
                        error_get_last()['message']
81
                    ));
82
                }
83
            } elseif (@unlink($fileInfo->getRealPath()) === false) {
84
                if (OS::isWindows()) {
85
                    Exec::create('attrib', '-R', $fileInfo->getRealPath())->run();
86
                    if (@unlink($fileInfo->getRealPath())) {
87
                        continue;
88
                    }
89
                }
90
                throw new \RuntimeException(sprintf(
91
                    "Unable to delete file '%s' in folder '%s': %s",
92
                    $fileInfo->getRealPath(),
93
                    $path,
94
                    error_get_last()['message']
95
                ));
96
            }
97
        }
98
    }
99
}
100