Completed
Push — master ( 6d41ca...2bcfec )
by
unknown
07:51 queued 10s
created

CommandHelper   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 144
Duplicated Lines 20.83 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 38
lcom 0
cbo 0
dl 30
loc 144
rs 9.36
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureDirectoriesExist() 0 7 2
C directoryCopy() 15 42 15
C compareDirectories() 15 57 17
A removeDirectory() 0 9 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Http\Helpers;
4
5
class CommandHelper
6
{
7
    /**
8
     * Check if the directories for the files exists.
9
     *
10
     * @param $directory
11
     * @return void
12
     */
13
    public static function ensureDirectoriesExist($directory)
14
    {
15
        // CHECK if directory exists, if not create it
16
        if (! is_dir($directory)) {
17
            mkdir($directory, 0755, true);
18
        }
19
    }
20
21
    /**
22
     * Recursive function that copies an entire directory to a destination.
23
     *
24
     * @param $source_directory
25
     * @param $destination_directory
26
     */
27
    public static function directoryCopy($source_directory, $destination_directory, $force = false, $recursive = false, $ignore = [], $ignore_ending = null)
28
    {
29
        //Checks destination folder existance
30
        self::ensureDirectoriesExist($destination_directory);
31
        //Open source directory
32
        $directory = opendir($source_directory);
33
34
        while (false !== ($file = readdir($directory))) {
35
            if (($file != '.') && ($file != '..')) {
36
                if (is_dir($source_directory.'/'.$file) && $recursive) {
37
                    self::directoryCopy($source_directory.'/'.$file, $destination_directory.'/'.$file, $force, $recursive, $ignore, $ignore_ending);
38
                } elseif (! is_dir($source_directory.'/'.$file)) {
39
                    $checkup = true;
40
41 View Code Duplication
                    if ($ignore_ending) {
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...
42
                        if (! is_array($ignore_ending)) {
43
                            $ignore_ending = str_replace('*', '', $ignore_ending);
44
45
                            $checkup = (substr($file, -strlen($ignore_ending)) !== $ignore_ending);
46
                        } else {
47
                            foreach ($ignore_ending as $key => $ignore_ending_sub) {
48
                                if ($checkup) {
49
                                    $ignore_ending_sub = str_replace('*', '', $ignore_ending_sub);
50
51
                                    $checkup = (substr($file, -strlen($ignore_ending_sub)) !== $ignore_ending_sub);
52
                                }
53
                            }
54
                        }
55
                    }
56
57
                    if ($checkup && (! in_array($file, $ignore))) {
58
                        if (file_exists($destination_directory.'/'.$file) && ! $force) {
59
                            continue;
60
                        }
61
                        copy($source_directory.'/'.$file, $destination_directory.'/'.$file);
62
                    }
63
                }
64
            }
65
        }
66
67
        closedir($directory);
68
    }
69
70
    /**
71
     * Compare Directories.
72
     *
73
     * @return void
74
     */
75
    public static function compareDirectories($package_path, $assets_path, $sub_folder = '', $ignore = [], $ignore_ending = [], $recursive = false, $internal = null)
76
    {
77
        $return = [];
0 ignored issues
show
Unused Code introduced by
$return is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
        $package_sha1 = '';
79
        $assets_sha1 = '';
80
81
        $handle = opendir($package_path);
82
83
        while ($file = readdir($handle)) {
84
            $checkup = true;
85
86
            if (in_array($file, $ignore)) {
87
                $checkup = false;
88 View Code Duplication
            } elseif ($ignore_ending) {
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...
89
                if (! is_array($ignore_ending)) {
90
                    $ignore_ending = str_replace('*', '', $ignore_ending);
91
92
                    $checkup = (substr($file, -strlen($ignore_ending)) !== $ignore_ending);
93
                } else {
94
                    foreach ($ignore_ending as $key => $ignore_ending_sub) {
95
                        if ($checkup) {
96
                            $ignore_ending_sub = str_replace('*', '', $ignore_ending_sub);
97
98
                            $checkup = (substr($file, -strlen($ignore_ending_sub)) !== $ignore_ending_sub);
99
                        }
100
                    }
101
                }
102
            }
103
104
            if ($file == '.' || $file == '..' || ! $checkup) {
105
                continue;
106
            }
107
            $sourcepath = $package_path.$sub_folder.DIRECTORY_SEPARATOR.$file;
108
            $destpath = $assets_path.DIRECTORY_SEPARATOR.$file;
109
            if (is_file($sourcepath)) {
110
                $package_sha1 .= sha1_file($sourcepath);
111
                if (file_exists($destpath) && is_file($destpath)) {
112
                    $assets_sha1 .= sha1_file($destpath);
113
                }
114
            } elseif ($recursive) {
115
                $return = self::compareDirectories($sourcepath, $destpath, '', $ignore, $ignore_ending, $recursive, true);
0 ignored issues
show
Bug introduced by
It seems like $ignore_ending defined by str_replace('*', '', $ignore_ending) on line 90 can also be of type string; however, JeroenNoten\LaravelAdmin...r::compareDirectories() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
                $package_sha1 .= $return['package_sha1'];
117
                $assets_sha1 .= $return['assets_sha1'];
118
            }
119
        }
120
121
        closedir($handle);
122
123
        if ($internal) {
124
            return [
125
                'package_sha1' => $package_sha1,
126
                'assets_sha1' => $assets_sha1,
127
            ];
128
        }
129
130
        return $package_sha1 == $assets_sha1 ? true : ($assets_sha1 == '' ? null : false);
131
    }
132
133
    /**
134
     * Rescursive directory remove.
135
     *
136
     * @param $directory
137
     * @return void
138
     */
139
    public static function removeDirectory($directory)
140
    {
141
        if (file_exists($directory)) {
142
            foreach (glob($directory.'/{,.}*[!.]', GLOB_MARK | GLOB_BRACE) as $file) {
143
                is_dir($file) ? self::removeDirectory($file) : unlink($file);
144
            }
145
            rmdir($directory);
146
        }
147
    }
148
}
149