Passed
Push — master ( f475f6...720950 )
by Florian
03:26
created

AdminLteStatusCommand::checkAsset()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 6
eloc 18
c 2
b 2
f 0
nc 5
nop 2
dl 0
loc 26
ccs 0
cts 18
cp 0
crap 42
rs 9.0444
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'];
0 ignored issues
show
Bug introduced by
Are you sure $package_path of type array can be used in concatenation? ( Ignorable by Annotation )

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

46
        $table_content[] = ['config', 'Default Config', $this->resolveCompare($this->compareFile(/** @scrutinizer ignore-type */ $package_path.'config/adminlte.php', base_path('config/adminlte.php'))), 'true'];
Loading history...
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. ( Ignorable by Annotation )

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

93
    protected function checkAsset($asset_key, /** @scrutinizer ignore-unused */ $include_images)

This check looks for 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
Bug introduced by
$assets_path of type array is incompatible with the type string expected by parameter $path of public_path(). ( Ignorable by Annotation )

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

103
                $compare_multiple += $this->compareFolder($asset['package_path'][$key], $asset['assets_path'][$key], base_path($package_path), public_path(/** @scrutinizer ignore-type */ $assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['ignore_ending'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
Loading history...
Bug introduced by
$package_path of type array is incompatible with the type string expected by parameter $path of base_path(). ( Ignorable by Annotation )

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

103
                $compare_multiple += $this->compareFolder($asset['package_path'][$key], $asset['assets_path'][$key], base_path(/** @scrutinizer ignore-type */ $package_path), public_path($assets_path), $asset['recursive'] ?? true, $asset['ignore'] ?? [], $asset['ignore_ending'] ?? [], $asset['images'] ?? null, $asset['images_path'] ?? null);
Loading history...
104
            }
105
106
            $compare_multiple /= count($asset['package_path']);
107
            if ($compare_multiple == 1) {
0 ignored issues
show
introduced by
The condition $compare_multiple == 1 is always false.
Loading history...
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);
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);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $compare is correct as JeroenNoten\LaravelAdmin...ore_ending, $recursive) targeting JeroenNoten\LaravelAdmin...r::compareDirectories() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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 JeroenNoten\LaravelAdmin...r::compareDirectories() has too many arguments starting with true. ( Ignorable by Annotation )

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

176
                /** @scrutinizer ignore-call */ 
177
                $compare = CommandHelper::compareDirectories($source_base_path.$source_path, $destination_base_path.$destination_path, '', $ignore, $ignore_ending, $recursive, null, 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. Please note the @ignore annotation hint above.

Loading history...
177
                if ($compare === false) {
178
                    $dest_missmatch = true;
179
                } elseif ($compare === null) {
0 ignored issues
show
introduced by
The condition $compare === null is always false.
Loading history...
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
The assignment to $asset_images_path is dead and can be removed.
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
The assignment to $view_exists is dead and can be removed.
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
            if (file_exists($file)) {
255
                $dest_file_content = file_get_contents($file);
256
                if (strpos($dest_file_content, $file_content) !== false) {
257
                    $view_found++;
258
                }
259
            }
260
        }
261
262
        if ($view_found === 0) {
263
            return 0;
264
        } elseif ($view_found === $view_excepted) {
265
            return 1;
266
        } elseif ($view_found !== $view_excepted) {
267
            return 2;
268
        }
269
    }
270
}
271