Completed
Pull Request — master (#429)
by
unknown
03:12
created

AdminLteStatusCommand::checkAsset()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
cc 6
nc 5
nop 2
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console;
4
5
use Illuminate\Console\Command;
6
use JeroenNoten\LaravelAdminLte\Http\Helpers\CommandHelper;
7
8
class AdminLteStatusCommand extends Command
9
{
10
    protected $signature = 'adminlte:status'.
11
        '{--include-images : Includes AdminLTE asset images to the checkup}';
12
13
    protected $description = 'Checks the install status for AdminLTE assets, routes & views.';
14
15
    protected $extra_steps = [
16
        'config', 'translations', 'main_views', 'auth_views', 'basic_views',
17
        'basic_routes',
18
    ];
19
20
    /**
21
     * Execute the console command.
22
     *
23
     * @return void
24
     */
25
    public function handle()
26
    {
27
        $headers = ['Group', 'Assets Name', 'Status', 'Required'];
28
        $step_count = 5;
29
        $table_content = [];
30
        $install_command = new AdminLteInstallCommand();
31
32
        $assets = $install_command->getProtected('assets');
33
        $package_path = $install_command->getProtected('package_path');
34
35
        $this->line('Checking Installation ...');
36
        $bar = $this->output->createProgressBar($step_count);
37
        $bar->start();
38
39
        // Checking Assets
40
        foreach ($assets as $asset_key => $asset) {
41
            $table_content[] = ['assets', $asset['name'], $this->resolveCompare($this->checkAsset($asset_key, $this->option('include-images'))), 'true'];
42
        }
43
        $bar->advance();
44
45
        // Checking Config
46
        $table_content[] = ['config', 'Default Config', $this->resolveCompare($this->compareFile($package_path.'config/adminlte.php', base_path('config/adminlte.php'))), 'true'];
47
        $bar->advance();
48
49
        // Checking Translations
50
        $table_content[] = ['translations', 'Default Translations', $this->resolveCompare($this->compareFolder('resources/lang', 'resources/lang', $package_path, base_path(), true, ['menu.php'])), 'true'];
51
        $bar->advance();
52
53
        // Checking Main Views
54
        $table_content[] = ['auth_views', 'Auth Views', $this->resolveCompare($this->compareAuthViews()), 'false'];
55
        $bar->advance();
56
57
        // Checking Main Views
58
        $table_content[] = ['main_views', 'Main Views', $this->resolveCompare($this->compareFolder('resources/views', 'resources/views/vendor/adminlte/', $package_path, base_path(), true)), 'false'];
59
        $bar->advance();
60
61
        $bar->finish();
62
63
        $this->line('');
64
        $this->line('Installation Checked');
65
66
        $this->table($headers, $table_content);
67
    }
68
69
    /**
70
     * Resolve Compare.
71
     *
72
     * @param  $compare
73
     * @return string
74
     */
75
    protected function resolveCompare($compare)
76
    {
77
        if ($compare === 1) {
78
            return 'Installed';
79
        } elseif ($compare === 2) {
80
            return 'Update Available / Modified';
81
        } elseif ($compare === 0) {
82
            return 'Not Installed';
83
        }
84
    }
85
86
    /**
87
     * Check Plugin.
88
     *
89
     * @param  $asset_key
90
     * @param  $include_images
91
     * @return string
92
     */
93
    protected function checkAsset($asset_key, $include_images)
0 ignored issues
show
Unused Code introduced by
The parameter $include_images is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        $install_command = new AdminLteInstallCommand();
96
        $asset = $install_command->getProtected('assets')[$asset_key];
97
        $assets_path = $install_command->getProtected('assets_path');
98
        $package_path = $install_command->getProtected('assets_package_path');
99
        $compare = $compare_multiple = null;
100
101
        if (is_array($asset['package_path'])) {
102
            foreach ($asset['package_path'] as $key => $value) {
103
                $compare_multiple += $this->compareFolder($asset['package_path'][$key], $asset['assets_path'][$key], base_path($package_path), public_path($assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['ignore_ending'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
0 ignored issues
show
Documentation introduced by
$package_path is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$assets_path is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
            }
105
106
            $compare_multiple /= count($asset['package_path']);
107
            if ($compare_multiple == 1) {
108
                $compare = 1;
109
            } elseif ($compare_multiple >= 1) {
110
                $compare = 2;
111
            } elseif ($compare_multiple <= 1) {
112
                $compare = 0;
113
            }
114
        } else {
115
            $compare = $this->compareFolder($asset['package_path'], $asset['assets_path'], base_path($package_path), public_path($assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['ignore_ending'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
0 ignored issues
show
Documentation introduced by
$package_path is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$assets_path is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
        }
117
118
        return $compare;
119
    }
120
121
    /**
122
     * Compare Folder.
123
     *
124
     * @param  $source_path
125
     * @param  $destination_path
126
     * @param  $source_base_path
127
     * @param  $destination_base_path
128
     * @param  $recursive
129
     * @param  $ignore
130
     * @param  $ignore_ending
131
     * @param  $images
132
     * @param  $images_path
133
     * @return int
134
     */
135
    public function compareFolder($source_path, $destination_path, $source_base_path = null, $destination_base_path = null, $recursive = true, $ignore = [], $ignore_ending = [], $images = null, $images_path = null, $ignore_base_folder = null)
136
    {
137
        $dest_exist = true;
138
        $dest_missing = false;
139
        $dest_missmatch = false;
140
        $dest_child_exist = true;
141
        $dest_child_missmatch = false;
142
143
        if (! $source_base_path) {
144
            $source_base_path = base_path();
145
        }
146
        if (substr($source_base_path, -1) !== '/') {
147
            $source_base_path .= '/';
148
        }
149
150
        if (! $destination_base_path) {
151
            $destination_base_path = public_path();
152
        }
153
        if (substr($destination_base_path, -1) !== '/') {
154
            $destination_base_path .= '/';
155
        }
156
157
        if (is_array($source_path)) {
158
            foreach ($source_path as $key => $destination_child_path) {
159
                if (! file_exists($destination_base_path.$destination_child_path)) {
160
                    $dest_exist = false;
161
                    $dest_child_exist = false;
162
                } else {
163
                    $compare = CommandHelper::compareDirectories($source_base_path.$source_path[$key], $destination_base_path.$destination_child_path, '', $ignore, $ignore_ending, $recursive);
164
165
                    if (! $dest_child_missmatch && $compare) {
166
                        $dest_child_missmatch = false;
167
                    } else {
168
                        $dest_child_missmatch = true;
169
                    }
170
                }
171
            }
172
        } else {
173
            if (! file_exists($destination_base_path.$destination_path)) {
174
                $dest_exist = false;
175
            } else {
176
                $compare = CommandHelper::compareDirectories($source_base_path.$source_path, $destination_base_path.$destination_path, '', $ignore, $ignore_ending, $recursive, null, true);
0 ignored issues
show
Unused Code introduced by
The call to CommandHelper::compareDirectories() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
177
                if ($compare === false) {
178
                    $dest_missmatch = true;
179
                } elseif ($compare === null) {
180
                    $dest_missing = true;
181
                }
182
            }
183
        }
184
185
        if ($images_path && $images) {
186
            $asset_images_path = $images_path;
0 ignored issues
show
Unused Code introduced by
$asset_images_path 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...
187
188
            foreach ($images as $image_destination_path => $image_asset_path) {
189
                $compareFile = $this->compareFile($source_base_path.$image_destination_path, $destination_base_path.$images_path.$image_asset_path);
190
                if ($compareFile === 0) {
191
                    $dest_child_exist = false;
192
                } elseif ($compareFile == 1) {
193
                    $dest_child_missmatch = false;
194
                } elseif ($compareFile == 2) {
195
                    $dest_child_missmatch = true;
196
                }
197
            }
198
        }
199
200
        if ($dest_exist && $dest_child_exist && ! $ignore_base_folder && (! $dest_missmatch && ! $dest_child_missmatch) && ! $dest_missing) {
201
            return 1;
202
        } elseif ($dest_exist && (($dest_missmatch || $dest_child_missmatch) || ! $dest_child_exist)) {
203
            return 2;
204
        } elseif (! $dest_exist || $dest_missing) {
205
            return 0;
206
        }
207
    }
208
209
    /**
210
     * Compare File.
211
     *
212
     * @param  $source_file
213
     * @param  $destination_file
214
     * @return int
215
     */
216
    public function compareFile($source_file, $destination_file)
217
    {
218
        $file_exist = true;
219
        $file_missmatch = false;
220
221
        if (! file_exists($destination_file)) {
222
            $file_exist = false;
223
        } else {
224
            $compare = sha1_file($source_file) === sha1_file($destination_file);
225
            if (! $compare) {
226
                $file_missmatch = true;
227
            }
228
        }
229
230
        if ($file_exist && (! $file_missmatch)) {
231
            return 1;
232
        } elseif ($file_exist && $file_missmatch) {
233
            return 2;
234
        } elseif (! $file_exist) {
235
            return 0;
236
        }
237
    }
238
239
    /**
240
     * Compare Auth Views.
241
     *
242
     * @return int
243
     */
244
    public function compareAuthViews()
245
    {
246
        $install_command = new AdminLteInstallCommand();
247
        $auth_views = $install_command->getProtected('authViews');
248
        $view_exists = true;
0 ignored issues
show
Unused Code introduced by
$view_exists 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...
249
        $view_found = 0;
250
        $view_excepted = count($auth_views);
251
252
        foreach ($auth_views as $file_name => $file_content) {
253
            $file = $install_command->getViewPath($file_name);
254
            $dest_file_content = file_get_contents($file);
255
            if (strpos($dest_file_content, $file_content) !== false) {
256
                $view_found++;
257
            }
258
        }
259
260
        if ($view_found === 0) {
261
            return 0;
262
        } elseif ($view_found === $view_excepted) {
263
            return 1;
264
        } elseif ($view_found !== $view_excepted) {
265
            return 2;
266
        }
267
    }
268
}
269