Directory::rrmdir()   B
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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