Passed
Push — 6.5.0.0 ( 6fa895...adbd9b )
by Christian
13:56 queued 02:07
created

DeleteAdminFilesAfterBuildCommand   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 81
c 1
b 0
f 0
dl 0
loc 135
rs 10
wmc 21

4 Methods

Rating   Name   Duplication   Size   Complexity  
B removeDirectory() 0 25 8
B deleteEmptyDirectories() 0 24 9
A configure() 0 2 1
A execute() 0 67 3
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Administration\Command;
4
5
use Shopware\Administration\Administration;
6
use Shopware\Core\Framework\Log\Package;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ConfirmationQuestion;
13
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
#[AsCommand(
16
    name: 'administration:delete-files-after-build',
17
    description: 'Deletes all uneccessary files of the administration after the build process.',
18
)]
19
#[Package('admin')]
20
class DeleteAdminFilesAfterBuildCommand extends Command
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure(): void
26
    {
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output): int
30
    {
31
        $helper = $this->getHelper('question');
32
33
        $question = new ConfirmationQuestion('This will delete all files necessary to build the administration. Do you want to continue? (y/n) ', false);
34
35
        if (!$helper->ask($input, $output, $question)) {
36
            $output->writeln('Command aborted!');
37
38
            return 0;
39
        }
40
41
        $adminDir = \dirname((string) (new \ReflectionClass(Administration::class))->getFileName());
42
        $output->writeln('Deleting uneccessary files of the administration after the build process...');
43
        $progressBar = new ProgressBar($output, 100);
44
45
        $finder = new Finder();
46
47
        // Find all files in Administration/Resources/app/administration/src/module except for de-DE.json and en-GB.json
48
        $finder->in($adminDir . '/Resources/app/administration/src/module')
49
            ->notName('de-DE.json')
50
            ->notName('en-GB.json')
51
            ->files();
52
53
        foreach ($finder as $file) {
54
            unlink($file->getRealPath());
55
        }
56
        $progressBar->advance(25);
57
58
        $this->deleteEmptyDirectories($adminDir . '/Resources/app/administration/src/module');
59
        $progressBar->advance(25);
60
61
        // Find all the following directories and files and delete them
62
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/adapter');
63
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/assets');
64
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/asyncComponent');
65
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/component');
66
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/decorator');
67
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/directive');
68
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/filter');
69
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/init');
70
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/init-post');
71
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/init-pre');
72
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/mixin');
73
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/plugin');
74
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/route');
75
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/service');
76
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/app/state');
77
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/core');
78
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/meta');
79
        $this->removeDirectory($adminDir . '/Resources/app/administration/src/scripts');
80
        $this->removeDirectory($adminDir . '/Resources/app/administration/patches');
81
        unlink($adminDir . '/Resources/app/administration/package-lock.json');
82
        $progressBar->advance(25);
83
84
        $this->removeDirectory($adminDir . '/Resources/app/administration/static');
85
        $this->removeDirectory($adminDir . '/Resources/app/administration/build');
86
        $this->removeDirectory($adminDir . '/Resources/app/administration/scripts');
87
        $this->removeDirectory($adminDir . '/Resources/app/administration/eslint-rules');
88
        $this->removeDirectory($adminDir . '/Resources/app/administration/test');
89
        $progressBar->advance(25);
90
        $progressBar->finish();
91
92
        $output->writeln('');
93
        $output->writeln('All uneccessary files of the administration after the build process have been deleted.');
94
95
        return 0;
96
    }
97
98
    private function deleteEmptyDirectories(string $dir): void
99
    {
100
        if (!is_dir($dir)) {
101
            return;
102
        }
103
104
        $files = scandir($dir);
105
        if (!$files) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $files of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
106
            return;
107
        }
108
109
        foreach ($files as $file) {
110
            if ($file === '.' || $file === '..') {
111
                continue;
112
            }
113
114
            $path = $dir . '/' . $file;
115
            if (is_dir($path)) {
116
                $this->deleteEmptyDirectories($path);
117
            }
118
        }
119
120
        if (\count(scandir($dir) ?: []) === 2) {
121
            rmdir($dir);
122
        }
123
    }
124
125
    private function removeDirectory(string $dir): void
126
    {
127
        if (!is_dir($dir) || str_contains('/snippet', $dir)) {
128
            return;
129
        }
130
131
        $files = scandir($dir);
132
        if (!$files) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $files of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133
            return;
134
        }
135
136
        foreach ($files as $file) {
137
            if ($file === '.' || $file === '..') {
138
                continue;
139
            }
140
141
            $path = $dir . '/' . $file;
142
            if (is_dir($path)) {
143
                $this->removeDirectory($path);
144
            } else {
145
                unlink($path);
146
            }
147
        }
148
149
        rmdir($dir);
150
    }
151
}
152