Completed
Push — v5 ( 0b505a...012ddd )
by Georges
02:41
created

Directory::rrmdir()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 18
c 1
b 1
f 0
nc 10
nop 2
dl 0
loc 37
rs 4.8196

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]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
namespace phpFastCache\Util;
15
16
use RecursiveDirectoryIterator;
17
use RecursiveIteratorIterator;
18
use SplFileInfo;
19
20
class Directory
21
{
22
    /**
23
     * Get the directory size
24
     * @param string $directory
25
     * @param bool $includeDirAllocSize
26
     * @return integer
27
     */
28
    public static function dirSize($directory, $includeDirAllocSize = false)
29
    {
30
        $size = 0;
31
        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) {
32
            /**
33
             * @var \SplFileInfo $file
34
             */
35
            if ($file->isFile()) {
36
                $size += filesize($file->getRealPath());
37
            } else if ($includeDirAllocSize) {
38
                $size += $file->getSize();
39
            }
40
        }
41
42
        return $size;
43
    }
44
45
    /**
46
     * @param string $path
47
     * @return int
48
     */
49
    public static function getFileCount($path)
50
    {
51
        $count = 0;
52
        $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST);
53
        foreach ($objects as $object) {
54
            /**
55
             * @var \SplFileInfo $object
56
             */
57
            if ($object->isFile()) {
58
                $count++;
59
            }
60
        }
61
62
        return $count;
63
    }
64
65
    /**
66
     * Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line.
67
     * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure.
68
     *
69
     * @param string $source absolute path to directory or file to delete.
70
     * @param bool $removeOnlyChildren set to true will only remove content inside directory.
71
     *
72
     * @return bool true on success; false on failure
73
     */
74
    public static function rrmdir($source, $removeOnlyChildren = false)
75
    {
76
        if (empty($source) || file_exists($source) === false) {
77
            return false;
78
        }
79
80
        if (is_file($source) || is_link($source)) {
81
            return unlink($source);
0 ignored issues
show
Security File Manipulation introduced by
$source can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
82
        }
83
84
        $files = new RecursiveIteratorIterator
85
        (
86
          new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
87
          RecursiveIteratorIterator::CHILD_FIRST
88
        );
89
        
90
        foreach ($files as $fileinfo) {
91
            /**
92
             * @var SplFileInfo $fileinfo
93
             */
94
            if ($fileinfo->isDir()) {
95
                if (self::rrmdir($fileinfo->getRealPath()) === false) {
96
                    return false;
97
                }
98
            } else {
99
                if (unlink($fileinfo->getRealPath()) === false) {
100
                    return false;
101
                }
102
            }
103
        }
104
105
        if ($removeOnlyChildren === false) {
106
            return rmdir($source);
0 ignored issues
show
Security File Manipulation introduced by
$source can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
107
        }
108
109
        return true;
110
    }
111
}