Completed
Push — master ( da89f6...8759f3 )
by recca
01:28
created

Cleanup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Webmozart\Glob\Glob;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Collection;
8
use Illuminate\Filesystem\Filesystem;
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 \Illuminate\Filesystem\Filesystem
30
     */
31
    protected $files;
32
33
    /**
34
     * __construct.
35
     *
36
     * @param \Illuminate\Filesystem\Filesystem $files
37
     */
38 1
    public function __construct(Filesystem $files)
39
    {
40 1
        parent::__construct();
41
42 1
        $this->files = $files;
43 1
    }
44
45
    /**
46
     * Handle the command.
47
     *
48 1
     * @throws \InvalidArgumentException
49
     */
50 1
    public function handle()
51 1
    {
52 1
        set_time_limit(0);
53
        $root = function_exists('base_path') === true ? base_path() : getcwd();
54 1
        $root = rtrim($root, '/').'/';
55 1
56 1
        $docs = ['README*', 'CHANGELOG*', 'FAQ*', 'CONTRIBUTING*', 'HISTORY*', 'UPGRADING*', 'UPGRADE*', 'package*', 'demo', 'example', 'examples', 'doc', 'docs', 'readme*'];
57
        $tests = ['.travis.yml', '.scrutinizer.yml', 'phpunit.xml*', 'phpunit.php', 'test', 'Test', 'tests', 'Tests', 'travis'];
58 1
        $vcs = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'];
59 1
        $others = [
60 1
            'vendor',
61 1
            '.babelrc',
62 1
            '.editorconfig',
63 1
            '.eslintrc.*',
64 1
            '.gitattributes',
65 1
            '.gitignore',
66 1
            '.nitpick.json',
67 1
            '.php_cs',
68 1
            '.scrutinizer.yml',
69 1
            '.styleci.yml',
70 1
            '.travis.yml',
71 1
            'appveyor.yml',
72 1
            'package.json',
73 1
            'phpcs.xml',
74
            'ruleset.xml',
75 1
        ];
76 1
        $common = [
77
            'node_modules',
78 1
        ];
79 1
80
        (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 1
            ->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 1
            $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 1
                $this->info('delete file: '.$item);
99 1
            }
100 1
        });
101
        $this->line('');
102
    }
103
}
104