Cleanup   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 54 4
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use InvalidArgumentException;
8
use Webmozart\Glob\Glob;
9
10
class Cleanup extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'cleanup';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'cleanup vendor folder';
25
26
    /**
27
     * $files.
28
     *
29
     * @var Filesystem
30
     */
31
    protected $files;
32
33
    /**
34
     * __construct.
35
     *
36
     * @param Filesystem $files
37 1
     */
38
    public function __construct(Filesystem $files)
39 1
    {
40
        parent::__construct();
41 1
42 1
        $this->files = $files;
43
    }
44
45
    /**
46
     * Handle the command.
47
     *
48
     * @throws InvalidArgumentException
49 1
     */
50
    public function handle()
51 1
    {
52 1
        set_time_limit(0);
53 1
        $root = function_exists('base_path') === true ? base_path() : getcwd();
54
        $root = rtrim($root, '/').'/';
55 1
56 1
        $docs = ['README*', 'CHANGELOG*', 'FAQ*', 'CONTRIBUTING*', 'HISTORY*', 'UPGRADING*', 'UPGRADE*', 'package*', 'demo', 'example', 'examples', 'doc', 'docs', 'readme*'];
57 1
        $tests = ['.travis.yml', '.scrutinizer.yml', 'phpunit.xml*', 'phpunit.php', 'test', 'Test', 'tests', 'Tests', 'travis'];
58
        $vcs = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'];
59 1
        $others = [
60
            'vendor',
61
            '.babelrc',
62
            '.editorconfig',
63
            '.eslintrc.*',
64
            '.gitattributes',
65
            '.gitignore',
66
            '.nitpick.json',
67
            '.php_cs',
68
            '.scrutinizer.yml',
69
            '.styleci.yml',
70
            '.travis.yml',
71
            'appveyor.yml',
72
            'package.json',
73
            'phpcs.xml',
74
            'ruleset.xml',
75
        ];
76 1
        $common = [
77
            'node_modules',
78
        ];
79 1
80 1
        (new Collection(
81 1
            Glob::glob($root.'{'.(new Collection(Glob::glob($root.'vendor/*/*')))
82 1
                ->map(function ($item) {
83 1
                    return substr($item, strpos($item, 'vendor'));
84 1
                })
85
                ->implode(',').'}/**/{'.(implode(',', array_merge($vcs, $common, $others, $tests, $docs))).'}')
86 1
            ))
87 1
            ->merge(Glob::glob($root.'{'.(implode(',', array_merge($vcs, $common))).'}'))
88 1
            ->merge([
89
                $root.'vendor/phpunit',
90 1
            ])
91 1
            ->filter()
92 1
            ->each(function ($item) {
93 1
                if ($this->files->isDirectory($item) === true) {
94 1
                    $this->files->deleteDirectory($item);
95 1
                    $this->info('delete directory: '.$item);
96 1
                } elseif ($this->files->isFile($item)) {
97 1
                    $this->files->delete($item);
98
                    $this->info('delete file: '.$item);
99 1
                }
100
            });
101 1
102 1
        $this->line('');
103
    }
104
}
105