Passed
Pull Request — master (#1283)
by Diego
04:05
created

CommandHelper::compareDirectories()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 54
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 17
nc 9
nop 4
dl 0
loc 54
ccs 18
cts 18
cp 1
crap 10
rs 7.6666
c 1
b 0
f 0

How to fix   Long Method    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 JeroenNoten\LaravelAdminLte\Helpers;
4
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Str;
7
8
class CommandHelper
9
{
10
    /**
11
     * The path to the package's root folder.
12
     *
13
     * @var string
14
     */
15
    protected static $packagePath = __DIR__.'/../../';
16
17
    /**
18
     * The path to the package's stubs folder.
19
     *
20
     * @var string
21
     */
22
    protected static $stubsPath = __DIR__.'/../Console/stubs';
23
24
    /**
25
     * Copy an entire directory to a destination.
26
     *
27
     * @param  string  $dir  The path of the source folder
28
     * @param  string  $dest  The path of the destination folder
29
     * @param  bool  $force  Whether to force the overwrite of existing files
30
     * @param  bool  $recursive  Whether to copy subfolders recursively
31
     * @param  array  $ignores  Array of files to be ignored
32
     * @return void
33
     */
34 28
    public static function copyDirectory($dir, $dest, $force = false, $recursive = false, $ignores = [])
35
    {
36
        // Open the source folder. Return if fails to open.
37
38 28
        if (! is_resource($dirHandler = @opendir($dir))) {
39 2
            return;
40
        }
41
42
        // Ensure the destination folder exists.
43
44 26
        File::ensureDirectoryExists($dest);
45
46
        // Copy the source files to destination.
47
48 26
        while (($file = readdir($dirHandler)) !== false) {
49
            // Check if this file should be ignored.
50
51 26
            $filesToIgnore = array_merge($ignores, ['.', '..']);
52
53 26
            if (self::isIgnoredFile($file, $filesToIgnore)) {
54 26
                continue;
55
            }
56
57
            // Now, copy the file/folder. If the resource is a folder, proceed
58
            // recursively. Otherwise, copy the file to destination.
59
60 26
            $source = $dir.DIRECTORY_SEPARATOR.$file;
61 26
            $target = $dest.DIRECTORY_SEPARATOR.$file;
62
63 26
            if (is_dir($source) && $recursive) {
64 21
                self::copyDirectory($source, $target, $force, $recursive, $ignores);
65 26
            } elseif (is_file($source) && ($force || ! file_exists($target))) {
66 26
                copy($source, $target);
67
            }
68
        }
69
70
        // Close the source folder.
71
72 26
        closedir($dirHandler);
73
    }
74
75
    /**
76
     * Compare two directories file by file.
77
     *
78
     * @param  string  $dir1  The path of the first folder
79
     * @param  string  $dir2  The path of the second folder
80
     * @param  bool  $recursive  Whether to compare subfolders recursively
81
     * @param  array  $ignores  Array of files to be ignored
82
     * @return bool|null Result of comparison or null if a folder not exists
83
     */
84 25
    public static function compareDirectories($dir1, $dir2, $recursive = false, $ignores = [])
85
    {
86
        // Open the first folder. Return if fails to open.
87
88 25
        if (! is_resource($dirHandler = @opendir($dir1))) {
89 1
            return;
90
        }
91
92
        // Check if the second folder exists.
93
94 25
        if (! is_dir($dir2)) {
95 12
            return;
96
        }
97
98
        // Now, compare the folders.
99
100 24
        while (($file = readdir($dirHandler)) !== false) {
101
            // Check if this file should be ignored.
102
103 24
            $filesToIgnore = array_merge($ignores, ['.', '..']);
104
105 24
            if (self::isIgnoredFile($file, $filesToIgnore)) {
106 22
                continue;
107
            }
108
109
            // Get paths of the resources to compare.
110
111 24
            $source = $dir1.DIRECTORY_SEPARATOR.$file;
112 24
            $target = $dir2.DIRECTORY_SEPARATOR.$file;
113
114
            // If the resources to compare are files, check that both files are
115
            // equals.
116
117 24
            if (is_file($source) && ! self::compareFiles($source, $target)) {
118 5
                return false;
119
            }
120
121
            // If the resources to compare are folders, recursively compare the
122
            // folders.
123
124 24
            $isDir = is_dir($source) && $recursive;
125
126 24
            if ($isDir && ! (bool) self::compareDirectories($source, $target, $recursive, $ignores)) {
127 6
                return false;
128
            }
129
        }
130
131
        // Close the opened folder.
132
133 22
        closedir($dirHandler);
134
135
        // At this point all the resources compared are equals.
136
137 22
        return true;
138
    }
139
140
    /**
141
     * Checks if two files are equals by comparing their hash values.
142
     *
143
     * @param  string  $file1  The path to the first file
144
     * @param  string  $file2  The path to the second file
145
     * @return bool
146
     */
147 23
    public static function compareFiles($file1, $file2)
148
    {
149 23
        if (! File::isFile($file1) || ! File::isFile($file2)) {
150 6
            return false;
151
        }
152
153 23
        return File::hash($file1) === File::hash($file2);
154
    }
155
156
    /**
157
     * Gets the fully qualified path to the specified package resource.
158
     *
159
     * @param  string  $path  Relative path to the resource
160
     * @return string
161
     */
162 24
    public static function getPackagePath($path = null)
163
    {
164 24
        if (empty($path)) {
165 1
            return self::$packagePath;
166
        }
167
168 24
        return self::$packagePath.DIRECTORY_SEPARATOR.$path;
169
    }
170
171
    /**
172
     * Gets the fully qualified path to the specified package stub resource.
173
     *
174
     * @param  string  $path  Relative path to the stub resource
175
     * @return string
176
     */
177 24
    public static function getStubPath($path = null)
178
    {
179 24
        if (empty($path)) {
180 1
            return self::$stubsPath;
181
        }
182
183 24
        return self::$stubsPath.DIRECTORY_SEPARATOR.$path;
184
    }
185
186
    /**
187
     * Gets the fully qualified path to the specified view resource, relative to
188
     * the configured view path.
189
     *
190
     * @param  string  $path  Relative path to some view resource
191
     * @return string
192
     */
193 24
    public static function getViewPath($path = null)
194
    {
195 24
        $basePath = config('view.paths')[0] ?? resource_path('views');
196
197 24
        if (empty($path)) {
198 24
            return $basePath;
199
        }
200
201 24
        return $basePath.DIRECTORY_SEPARATOR.$path;
202
    }
203
204
    /**
205
     * Checks if a file belongs to the set of specified file patterns to be
206
     * ignored.
207
     *
208
     * @param  string  $file  The file to check
209
     * @param  array  $ignores  Array of file patterns to be ignored
210
     * @return bool
211
     */
212 31
    protected static function isIgnoredFile($file, $ignores)
213
    {
214 31
        foreach ($ignores as $pattern) {
215 31
            $match = Str::startsWith($pattern, 'regex:')
216 2
                ? preg_match(Str::substr($pattern, 6), $file)
217 31
                : Str::is($pattern, $file);
218
219 31
            if ($match) {
220 30
                return true;
221
            }
222
        }
223
224 31
        return false;
225
    }
226
}
227