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

AdminLteStatusCommand::checkAsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
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
100
        $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...
101
102
        return $compare;
103
    }
104
105
    /**
106
     * Compare Folder.
107
     *
108
     * @param  $source_path
109
     * @param  $destination_path
110
     * @param  $source_base_path
111
     * @param  $destination_base_path
112
     * @param  $recursive
113
     * @param  $ignore
114
     * @param  $ignore_ending
115
     * @param  $images
116
     * @param  $images_path
117
     * @return int
118
     */
119
    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)
120
    {
121
        $dest_exist = true;
122
        $dest_missing = false;
123
        $dest_missmatch = false;
124
        $dest_child_exist = true;
125
        $dest_child_missmatch = false;
126
127
        if (! $source_base_path) {
128
            $source_base_path = base_path();
129
        }
130
        if (substr($source_base_path, -1) !== '/') {
131
            $source_base_path .= '/';
132
        }
133
134
        if (! $destination_base_path) {
135
            $destination_base_path = public_path();
136
        }
137
        if (substr($destination_base_path, -1) !== '/') {
138
            $destination_base_path .= '/';
139
        }
140
141
        if (is_array($source_path)) {
142
            foreach ($source_path as $key => $destination_child_path) {
143
                if (! file_exists($destination_base_path.$destination_child_path)) {
144
                    $dest_exist = false;
145
                    $dest_child_exist = false;
146
                } else {
147
                    $compare = CommandHelper::compareDirectories($source_base_path.$source_path[$key], $destination_base_path.$destination_child_path, '', $ignore, $ignore_ending, $recursive);
148
149
                    if (! $dest_child_missmatch && $compare) {
150
                        $dest_child_missmatch = false;
151
                    } else {
152
                        $dest_child_missmatch = true;
153
                    }
154
                }
155
            }
156
        } else {
157
            if (! file_exists($destination_base_path.$destination_path)) {
158
                $dest_exist = false;
159
            } else {
160
                $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...
161
                if ($compare === false) {
162
                    $dest_missmatch = true;
163
                } elseif ($compare === null) {
164
                    $dest_missing = true;
165
                }
166
            }
167
        }
168
169
        if ($images_path && $images) {
170
            $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...
171
172
            foreach ($images as $image_destination_path => $image_asset_path) {
173
                $compareFile = $this->compareFile($source_base_path.$image_destination_path, $destination_base_path.$images_path.$image_asset_path);
174
                if ($compareFile === 0) {
175
                    $dest_child_exist = false;
176
                } elseif ($compareFile == 1) {
177
                    $dest_child_missmatch = false;
178
                } elseif ($compareFile == 2) {
179
                    $dest_child_missmatch = true;
180
                }
181
            }
182
        }
183
184
        if ($dest_exist && $dest_child_exist && ! $ignore_base_folder && (! $dest_missmatch && ! $dest_child_missmatch) && ! $dest_missing) {
185
            return 1;
186
        } elseif ($dest_exist && (($dest_missmatch || $dest_child_missmatch) || ! $dest_child_exist)) {
187
            return 2;
188
        } elseif (! $dest_exist || $dest_missing) {
189
            return 0;
190
        }
191
    }
192
193
    /**
194
     * Compare File.
195
     *
196
     * @param  $source_file
197
     * @param  $destination_file
198
     * @return int
199
     */
200
    public function compareFile($source_file, $destination_file)
201
    {
202
        $file_exist = true;
203
        $file_missmatch = false;
204
205
        if (! file_exists($destination_file)) {
206
            $file_exist = false;
207
        } else {
208
            $compare = sha1_file($source_file) === sha1_file($destination_file);
209
            if (! $compare) {
210
                $file_missmatch = true;
211
            }
212
        }
213
214
        if ($file_exist && (! $file_missmatch)) {
215
            return 1;
216
        } elseif ($file_exist && $file_missmatch) {
217
            return 2;
218
        } elseif (! $file_exist) {
219
            return 0;
220
        }
221
    }
222
223
    /**
224
     * Compare Auth Views.
225
     *
226
     * @return int
227
     */
228
    public function compareAuthViews()
229
    {
230
        $install_command = new AdminLteInstallCommand();
231
        $auth_views = $install_command->getProtected('authViews');
232
        $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...
233
        $view_found = 0;
234
        $view_excepted = count($auth_views);
235
236
        foreach ($auth_views as $file_name => $file_content) {
237
            $file = $install_command->getViewPath($file_name);
238
            $dest_file_content = file_get_contents($file);
239
            if (strpos($dest_file_content, $file_content) !== false) {
240
                $view_found++;
241
            }
242
        }
243
244
        if ($view_found === 0) {
245
            return 0;
246
        } elseif ($view_found === $view_excepted) {
247
            return 1;
248
        } elseif ($view_found !== $view_excepted) {
249
            return 2;
250
        }
251
    }
252
}
253