Completed
Push — master ( d37fab...069828 )
by Igor
02:14
created

TFileSystem   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 30
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B removeFromDir() 0 21 8
1
<?php
2
/**
3
 * @license MIT
4
 * @author Igor Sorokin <[email protected]>
5
 */
6
namespace Dspbee\Bundle\Common;
7
8
/**
9
 * Class TFileSystem
10
 * @package Dspbee\Bundle\Common
11
 */
12
trait TFileSystem
13
{
14
    /**
15
     * Deleting all subdirectories and files.
16
     *
17
     * @param string $dir   Path to the directory
18
     * @param bool   $self  If true then delete root directory
19
     */
20
    private static function removeFromDir($dir, $self = false)
21
    {
22
        if (is_dir($dir)) {
23
            $objects = scandir($dir);
0 ignored issues
show
Security File Exposure introduced by
$dir can contain request data and is used in file inclusion 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...
24
            foreach ($objects as $object) {
25
                if ('.' != $object && '..' != $object) {
26
                    if ('dir' == filetype($dir . '/' .$object)) {
27
                        self::removeFromDir($dir . '/' . $object, true);
28
                    } else {
29
                        unlink($dir . '/' . $object);
0 ignored issues
show
Security File Manipulation introduced by
$dir . '/' . $object 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...
30
                    }
31
                }
32
            }
33
            if ($self) {
34
                reset($objects);
35
                if (count(scandir($dir)) == 2) {
0 ignored issues
show
Security File Exposure introduced by
$dir can contain request data and is used in file inclusion 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...
36
                    rmdir($dir);
0 ignored issues
show
Security File Manipulation introduced by
$dir 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...
37
                }
38
            }
39
        }
40
    }
41
}