CSFixer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 37
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A paths() 0 16 4
1
<?php
2
3
namespace PrismX\Generators\Support;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\File;
8
use Symfony\Component\Process\Process;
9
use Illuminate\Support\Facades\Storage;
10
use Symfony\Component\Process\Exception\ProcessFailedException;
11
12
class CSFixer
13
{
14
    public function __construct()
15
    {
16
        if (File::exists(base_path('vendor/bin/php-cs-fixer')) && File::exists(base_path('.php_cs'))) {
17
            $process = new Process(array_merge([
18
                base_path('vendor/bin/php-cs-fixer'),
19
                'fix',
20
                '--config='.base_path('.php_cs'),
21
            ], $this->paths()->toArray()));
22
23
            $process->run();
24
25
            if (! $process->isSuccessful()) {
26
                throw new ProcessFailedException($process);
27
            }
28
29
            echo $process->getOutput();
30
        }
31
    }
32
33
    public function paths(): Collection
34
    {
35
        $modelsPath = Str::camel(str_replace('\\', '/', config('generators.model_namespace')));
36
37
        $models = glob(base_path($modelsPath.'/*.php'));
38
        $caches = glob(base_path(Storage::path('generators/cache').'/*.php'));
39
40
        return collect([
41
            base_path('database/factories'),
42
            base_path('database/migrations'),
43
            base_path('database/seeds'),
44
            File::isDirectory('app/Nova') ? base_path('app/nova') : null,
45
        ])
46
            ->concat($models ?: [])
47
            ->concat($caches ?: [])
48
            ->filter();
49
    }
50
}
51