Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

FileHelper::recurseCopyIfEdited()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 35
Code Lines 19

Duplication

Lines 3
Ratio 8.57 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 3
loc 35
rs 5.2653
c 1
b 1
f 0
cc 11
eloc 19
nc 8
nop 3

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 /** MicroFileHelper */
2
3
namespace Micro\File;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * MFile io class
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/lugnsk/micro
12
 * @copyright Copyright &copy; 2013 Oleg Lunegov
13
 * @license /LICENSE
14
 * @package Micro
15
 * @subpackage File
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class FileHelper
20
{
21
    /**
22
     * Directory size
23
     *
24
     * @access public
25
     *
26
     * @param string $dirName directory name
27
     *
28
     * @return integer
29
     * @static
30
     */
31
    public static function dirSize($dirName)
32
    {
33
        $totalSize = 0;
34
        if ($dirStream = opendir($dirName)) {
35
            while (false !== ($fileName = readdir($dirStream))) {
36
                if ($fileName !== '.' && $fileName !== '..') {
37
                    if (is_file($dirName . '/' . $fileName)) {
38
                        $totalSize += filesize($dirName . '/' . $fileName);
39
                    }
40
41
                    if (is_dir($dirName . '/' . $fileName)) {
42
                        $totalSize += self::dirSize($dirName . '/' . $fileName);
43
                    }
44
                }
45
            }
46
        }
47
48
        closedir($dirStream);
49
50
        return $totalSize;
51
    }
52
53
    /**
54
     * Recursive remove dir
55
     *
56
     * @access public
57
     *
58
     * @param string $path path to remove
59
     *
60
     * @return void
61
     * @static
62
     */
63
    public static function removeDir($path)
64
    {
65
        if (is_file($path)) {
66
            unlink($path);
67
        } else {
68
            foreach (scandir($path) as $dir) {
69
                if ($dir === '.' || $dir === '..') {
70
                    continue;
71
                }
72
73
                static::removeDir($path . '/' . $dir);
74
            }
75
76
            rmdir($path);
77
        }
78
    }
79
80
    /**
81
     * Recursive copy files
82
     *
83
     * @access public
84
     *
85
     * @param string $src source path
86
     * @param string $dst destination path
87
     *
88
     * @return void
89
     * @throws Exception
90
     * @static
91
     */
92
    public static function recurseCopy($src, $dst)
93
    {
94
        $dir = opendir($src);
95 View Code Duplication
        if (!@mkdir($dst, 0777) && !is_dir($dst)) {
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...
96
            throw new Exception('Copy dir error, access denied for path: ' . $dst);
97
        }
98
99
        while (false !== ($file = readdir($dir))) {
100
            if (($file !== '.') && ($file !== '..')) {
101
                if (is_dir($src . '/' . $file)) {
102
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
103
                } else {
104
                    copy($src . '/' . $file, $dst . '/' . $file);
105
                    chmod($dst . '/' . $file, 0666);
106
                }
107
            }
108
        }
109
        closedir($dir);
110
    }
111
112
    /**
113
     * Recursive copy files if edited
114
     *
115
     * @access public
116
     *
117
     * @param string $src source path
118
     * @param string $dst destination path
119
     * @param array $excludes excludes extensions
120
     *
121
     * @return void
122
     * @throws Exception
123
     * @static
124
     */
125
    public static function recurseCopyIfEdited($src = '', $dst = '', array $excludes = ['php'])
126
    {
127 View Code Duplication
        if (!@mkdir($dst, 0777) && !is_dir($dst)) {
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...
128
            throw new Exception('Copy dir error, access denied for path: ' . $dst);
129
        }
130
131
        $dir = opendir($src);
132
        if (!$dir) {
133
            throw new Exception('Unable to read dir: '. $src);
134
        }
135
136
        while (false !== ($file = readdir($dir))) {
137
            if ($file === '.' || $file === '..') {
138
                continue;
139
            }
140
141
            if (is_dir($src . '/' . $file)) {
142
                self::recurseCopyIfEdited($src . '/' . $file, $dst . '/' . $file, $excludes);
143
                continue;
144
            }
145
146
            if (in_array(substr($file, strrpos($file, '.') + 1), $excludes, null)) {
147
                continue;
148
            }
149
150
151
            if (file_exists($dst . '/' . $file) && (filemtime($src . '/' . $file) === filemtime($dst . '/' . $file))) {
152
                continue;
153
            }
154
155
            copy($src . '/' . $file, $dst . '/' . $file);
156
            chmod($dst . '/' . $file, 0666);
157
        }
158
        closedir($dir);
159
    }
160
}
161