Completed
Push — master ( cab8e0...64094d )
by Hong
02:15
created

LocalDirTrait::isRealDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Storage
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Storage\Traits;
16
17
use Phossa2\Storage\Message\Message;
18
19
/**
20
 * LocalDirTrait
21
 *
22
 * Dealing with directories in LocalDriver
23
 *
24
 * @package Phossa2\Storage
25
 * @author  Hong Zhang <[email protected]>
26
 * @version 2.0.0
27
 * @since   2.0.0 added
28
 */
29
trait LocalDirTrait
30
{
31
    /**
32
     * {@inheritDoc}
33
     */
34
    protected function isRealDir(/*# string */ $realPath)/*# : bool */
35
    {
36
        return is_dir($realPath);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function readDir(
43
        /*# string */ $realPath,
44
        /*# string */ $prefix = ''
45
    )/*# : array */ {
46
        try {
47
            $res = [];
48
            foreach (new \DirectoryIterator($realPath) as $fileInfo) {
49
                if($fileInfo->isDot()) continue;
50
                $res[] = $prefix . $fileInfo->getFilename();
51
            }
52
            return $res;
53
54
        } catch (\Exception $e) {
55
            $this->setError(
56
                Message::get(Message::STR_READDIR_FAIL, $realPath),
57
                Message::STR_READDIR_FAIL
58
            );
59
            return [];
60
        }
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    protected function renameDir(
67
        /*# string */ $from,
68
        /*# string */ $to
69
    )/*# : bool */ {
70
        return @rename($from, $to);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    protected function copyDir(
77
        /*# string */ $from,
78
        /*# string */ $to
79
    )/*# : bool */ {
80
        $this->makeDirectory($to);
81
        foreach ($this->readDir($from) as $file) {
82
            $f = $from . \DIRECTORY_SEPARATOR . $file;
83
            $t = $to . \DIRECTORY_SEPARATOR . $file;
84
            $res = is_dir($f) ? $this->copyDir($f, $t) : @copy($f, $t);
85
            if (false === $res) {
86
                return $this->setError(
87
                    Message::get(Message::STR_COPY_FAIL, $f),
88
                    Message::STR_COPY_FAIL
89
                );
90
            }
91
        }
92
        return $res;
0 ignored issues
show
Bug introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    protected function deleteDir(
99
        /*# string */ $realPath,
100
        /*# bool */ $keep = false
101
    )/*# : bool */ {
102
        $pref = rtrim($realPath, '/\\') . \DIRECTORY_SEPARATOR;
103
        foreach ($this->readDir($realPath, $pref) as $file) {
104
            $res = is_dir($file) ? $this->deleteDir($file) : unlink($file);
105
            if (false === $res) {
106
                return $this->setError(
107
                    Message::get(Message::STR_DELETE_FAIL, $file),
108
                    Message::STR_DELETE_FAIL
109
                );
110
            }
111
        }
112
        return $keep ? true : rmdir($realPath);
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118
    protected function makeDirectory(/*# string */ $realPath)/*# : bool */
119
    {
120
        if (!is_dir($realPath)) {
121
            $umask = umask(0);
122
            @mkdir($realPath, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
123
            umask($umask);
124
125 View Code Duplication
            if (!is_dir($realPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
126
                $this->setError(
127
                    Message::get(Message::STR_MKDIR_FAIL, $realPath),
128
                    Message::STR_MKDIR_FAIL
129
                );
130
                return false;
131
            }
132
        }
133
        return true;
134
    }
135
136
    abstract public function setError(
137
        /*# string */ $message = '',
138
        /*# string */ $code = ''
139
    )/*# : bool */;
140
}