Completed
Push — master ( 4e7427...4a94bb )
by Georges
12s
created

Directory::rrmdir()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 10
nop 2
dl 0
loc 37
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> https://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Util;
17
18
use RecursiveDirectoryIterator;
19
use RecursiveIteratorIterator;
20
use SplFileInfo;
21
22
/**
23
 * Class Directory
24
 * @package phpFastCache\Util
25
 */
26
class Directory
27
{
28
    /**
29
     * Get the directory size
30
     * @param string $directory
31
     * @param bool $includeDirAllocSize
32
     * @return integer
33
     */
34
    public static function dirSize(string $directory, bool $includeDirAllocSize = false): int
35
    {
36
        $size = 0;
37
        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) {
38
            /**
39
             * @var \SplFileInfo $file
40
             */
41
            if ($file->isFile()) {
42
                $size += filesize($file->getRealPath());
43
            } else {
44
                if ($includeDirAllocSize) {
45
                    $size += $file->getSize();
46
                }
47
            }
48
        }
49
50
        return $size;
51
    }
52
53
    /**
54
     * @param string $path
55
     * @return int
56
     */
57
    public static function getFileCount(string $path): int
58
    {
59
        $count = 0;
60
        $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST);
61
        foreach ($objects as $object) {
62
            /**
63
             * @var \SplFileInfo $object
64
             */
65
            if ($object->isFile()) {
66
                $count++;
67
            }
68
        }
69
70
        return $count;
71
    }
72
73
    /**
74
     * Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line.
75
     * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure.
76
     *
77
     * @param string $source absolute path to directory or file to delete.
78
     * @param bool $removeOnlyChildren set to true will only remove content inside directory.
79
     *
80
     * @return bool true on success; false on failure
81
     */
82
    public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool
83
    {
84
        if (empty($source) || \file_exists($source) === false) {
85
            return false;
86
        }
87
88
        if (\is_file($source) || \is_link($source)) {
89
            \clearstatcache(true, $source);
90
            return \unlink($source);
91
        }
92
93
        $files = new RecursiveIteratorIterator
94
        (
95
            new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
96
            RecursiveIteratorIterator::CHILD_FIRST
97
        );
98
99
        foreach ($files as $fileinfo) {
100
            /**
101
             * @var SplFileInfo $fileinfo
102
             */
103
            if ($fileinfo->isDir()) {
104
                if (self::rrmdir($fileinfo->getRealPath()) === false) {
105
                    return false;
106
                }
107
            } else {
108
                if (\unlink($fileinfo->getRealPath()) === false) {
109
                    return false;
110
                }
111
            }
112
        }
113
114
        if ($removeOnlyChildren === false) {
115
            return \rmdir($source);
116
        }
117
118
        return true;
119
    }
120
121
    /**
122
     * Alias of realpath() but work
123
     * on non-existing files
124
     *
125
     * @param string $path
126
     * @return string
127
     */
128
    public static function getAbsolutePath(string $path): string
129
    {
130
        $parts = \preg_split('~[/\\\\]+~', $path, 0, \PREG_SPLIT_NO_EMPTY);
131
        $absolutes = [];
132
        foreach ($parts as $part) {
133
            if ('.' === $part) {
134
                continue;
135
            }
136
            if ('..' === $part) {
137
                \array_pop($absolutes);
138
            } else {
139
                $absolutes[] = $part;
140
            }
141
        }
142
143
        /**
144
         * Allows to dereference char
145
         */
146
        $__FILE__ = \preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc.
147
        $prefix = $__FILE__[0] === \DIRECTORY_SEPARATOR ? \DIRECTORY_SEPARATOR : '';
148
        return $prefix . \implode(\DIRECTORY_SEPARATOR, $absolutes);
149
    }
150
}