Passed
Push — master ( d50017...68c32a )
by Michael
02:26
created

FS_Storage::dircopy()   C

Complexity

Conditions 15
Paths 5

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 42
rs 5.9166
c 0
b 0
f 0
cc 15
nc 5
nop 5

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
namespace XoopsModules\Adslight;
4
5
// ------------------------------------------------------------------------- //
6
//                       XOOPS - Module MP Manager                           //
7
//                       <http://www.xoops.org/>                             //
8
// ------------------------------------------------------------------------- //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  This program is distributed in the hope that it will be useful,          //
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
//  GNU General Public License for more details.                             //
18
//                                                                           //
19
//  You should have received a copy of the GNU General Public License        //
20
//  along with this program; if not, write to the Free Software              //
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------- //
23
//                 Votre nouveau systeme de messagerie priver                //
24
//                                                                           //
25
//                               "MP"                                        //
26
//                                                                           //
27
//                       http://lexode.info/mods                             //
28
//                                                                           //
29
//                                                                           //
30
//---------------------------------------------------------------------------//
31
32
/**
33
 * Class FS_Storage
34
 * @package XoopsModules\Adslight
35
 */
36
class FS_Storage
37
{
38
39
    /**
40
     * @var String
41
     */
42
    public $rootDir;
43
44
    /**
45
     * FS_Storage constructor.
46
     * @param $rootDir
47
     */
48
    public function __construct($rootDir)
49
    {
50
        $this->rootDir = $rootDir;
51
    }
52
53
    /**
54
     * @param $location
55
     */
56
    public static function deldir($location)
57
    {
58
        if (is_dir($location)) {
59
            $all = opendir($location);
60
            while ($file = readdir($all)) {
0 ignored issues
show
Bug introduced by
It seems like $all can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            while ($file = readdir(/** @scrutinizer ignore-type */ $all)) {
Loading history...
61
                if ('..' !== $file && '.' !== $file && is_dir("$location/$file")) {
62
                    self::deldir("$location/$file");
63
                    if (file_exists("$location/$file")) {
64
                        rmdir("$location/$file");
65
                    }
66
                    unset($file);
67
                } elseif (!is_dir("$location/$file")) {
68
                    if (file_exists("$location/$file")) {
69
                        unlink("$location/$file");
70
                    }
71
                    unset($file);
72
                }
73
            }
74
            closedir($all);
0 ignored issues
show
Bug introduced by
It seems like $all can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            closedir(/** @scrutinizer ignore-type */ $all);
Loading history...
75
            rmdir($location);
76
        } else {
77
            if (file_exists((string)$location)) {
78
                unlink((string)$location);
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param $fichier
85
     * @return false|string
86
     */
87
    public static function date_modif($fichier)
88
    {
89
        $tmp = filemtime($fichier);
90
        return date('d/m/Y H:i', $tmp);
91
    }
92
93
94
    // A function to copy files from one directory to another one, including subdirectories and
95
    // nonexisting or newer files. Function returns number of files copied.
96
    // This function is PHP implementation of Windows xcopy  A:\dir1\* B:\dir2 /D /E /F /H /R /Y
97
    // Syntaxis: [$number =] dircopy($sourcedirectory, $destinationdirectory [, $verbose]);
98
    // Example: $num = dircopy('A:\dir1', 'B:\dir2', 1);
99
100
    /**
101
     * @param      $srcdir
102
     * @param      $dstdir
103
     * @param      $errors
104
     * @param      $success
105
     * @param bool $verbose
106
     * @return int
107
     */
108
    public static function dircopy($srcdir, $dstdir, &$errors, &$success, $verbose = false)
109
    {
110
        $num = 0;
111
        if (!is_dir($dstdir)) {
112
            if (!mkdir($dstdir) && !is_dir($dstdir)) {
113
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $dstdir));
114
            }
115
        }
116
        if ($curdir = opendir($srcdir)) {
117
            while ($file = readdir($curdir)) {
118
                if ('.' !== $file && '..' !== $file) {
119
                    $srcfile = $srcdir . DIRECTORY_SEPARATOR . $file;
120
                    $dstfile = $dstdir . DIRECTORY_SEPARATOR . $file;
121
                    if (is_file($srcfile)) {
122
                        if (is_file($dstfile)) {
123
                            $ow = filemtime($srcfile) - filemtime($dstfile);
124
                        } else {
125
                            $ow = 1;
126
                        }
127
                        if ($ow > 0) {
128
                            if ($verbose) {
129
                                echo "Copying '$srcfile' to '$dstfile'...";
130
                            }
131
                            if (copy($srcfile, $dstfile)) {
132
                                touch($dstfile, filemtime($srcfile));
133
                                $num++;
134
                                if ($verbose) {
135
                                    echo "OK\n";
136
                                }
137
                                $success[] = $srcfile;
138
                            } else {
139
                                $errors[] = $srcfile;
140
                            }
141
                        }
142
                    } else if (is_dir($srcfile)) {
143
                        $num += self::dircopy($srcfile, $dstfile, $errors, $success, $verbose);
144
                    }
145
                }
146
            }
147
            closedir($curdir);
148
        }
149
        return $num;
150
    }
151
152
    /**
153
     * @param      $destDir
154
     * @param      $srcFile
155
     * @param      $error
156
     * @param      $success
157
     * @param bool $move
158
     */
159
    public static function copyOrMoveFile($destDir, $srcFile, &$error, &$success, $move = false)
160
    {
161
        $mess        = ConfService::getMessages();
0 ignored issues
show
Bug introduced by
The type XoopsModules\Adslight\ConfService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
162
        $destFile    = ConfService::getRootDir() . $destDir . '/' . basename($srcFile);
163
        $realSrcFile = ConfService::getRootDir() . "/$srcFile";
164
        if (!file_exists($realSrcFile)) {
165
            $error[] = $mess[100] . $srcFile;
166
            return;
167
        }
168
        if ($realSrcFile == $destFile) {
169
            $error[] = $mess[101];
170
            return;
171
        }
172
        if (is_dir($realSrcFile)) {
173
            $errors    = [];
174
            $succFiles = [];
175
            $dirRes    = self::dircopy($realSrcFile, $destFile, $errors, $succFiles);
176
            if (count($errors)) {
177
                $error[] = $mess[114];
178
                return;
179
            }
180
        } else {
181
            $res = copy($realSrcFile, $destFile);
182
            if (1 != $res) {
183
                $error[] = $mess[114];
184
                return;
185
            }
186
        }
187
188
        if ($move) {
189
            // Now delete original
190
            self::deldir($realSrcFile); // both file and dir
191
            $messagePart = $mess[74] . " $destDir";
192
            if ($destDir == '/' . ConfService::getRecycleBinDir()) {
193
                $messagePart = $mess[123] . ' ' . $mess[122];
194
            }
195
            if (isset($dirRes)) {
196
                $success[] = $mess[117] . ' ' . basename($srcFile) . ' ' . $messagePart . " ($dirRes " . $mess[116] . ') ';
197
            } else {
198
                $success[] = $mess[34] . ' ' . basename($srcFile) . ' ' . $messagePart;
199
            }
200
        } else {
201
            if (isSet($dirRes)) {
202
                $success[] = $mess[117] . ' ' . basename($srcFile) . ' ' . $mess[73] . " $destDir (" . $dirRes . ' ' . $mess[116] . ')';
203
            } else {
204
                $success[] = $mess[34] . ' ' . basename($srcFile) . ' ' . $mess[73] . " $destDir";
205
            }
206
        }
207
208
    }
209
210
}
211