Completed
Push — master ( ec1f4e...13b05b )
by Iman
01:41
created

CheckEndIf::fix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Commands;
4
5
use Illuminate\Console\Command;
6
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
7
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor;
8
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
9
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath;
10
use Imanghafoori\LaravelMicroscope\Refactors\SyntaxNormalizer;
11
12
class CheckEndIf extends Command
13
{
14
    protected $signature = 'check:endif {--t|test : backup the changed files}';
15
16
    protected $description = 'replaces endif with curly brackets.';
17
18
    public function handle()
19
    {
20
        if (! $this->startWarning()) {
21
            return null;
22
        }
23
24
        $psr4 = ComposerJson::readAutoload();
25
26
        $fixedFilesCount = 0;
27
        foreach ($psr4 as $psr4Path) {
28
            $files = FilePath::getAllPhpFiles($psr4Path);
29
            foreach ($files as $file) {
30
                $path = $file->getRealPath();
31
                $tokens = token_get_all(file_get_contents($path));
32
                if (empty($tokens) || $tokens[0][0] !== T_OPEN_TAG) {
33
                    continue;
34
                }
35
36
                try {
37
                    $tokens = SyntaxNormalizer::normalizeSyntax($tokens, true);
38
                } catch (\Exception $e) {
39
                    self::requestIssue($path);
40
                    continue;
41
                }
42
43
                if (! SyntaxNormalizer::$hasChange || ! $this->getConfirm($path)) {
44
                    continue;
45
                }
46
47
                Refactor::saveTokens($path, $tokens, $this->option('test'));
48
49
                $fixedFilesCount++;
50
            }
51
        }
52
53
        $this->printFinalMsg($fixedFilesCount);
54
55
        return app(ErrorPrinter::class)->hasErrors() ? 1 : 0;
56
    }
57
58
    private function printFinalMsg($fixed)
59
    {
60
        if ($fixed > 0) {
61
            $msg = 'Hooray!, '.$fixed.' files were transformed by the microscope.';
62
        } else {
63
            $msg = 'Congratulations, your code base does not seems to need any fix.';
64
        }
65
        $this->info(PHP_EOL.$msg);
66
        $this->info('     \(^_^)/    You Rock    \(^_^)/    ');
67
    }
68
69
    private function getConfirm($filePath)
70
    {
71
        $filePath = FilePath::getRelativePath($filePath);
72
73
        return $this->output->confirm('Replacing endif in: '.$filePath, true);
74
    }
75
76
    private function startWarning()
77
    {
78
        $this->info('Checking for Early Returns...');
79
        $this->warn('This command is going to make changes to your files!');
80
81
        return $this->output->confirm('Do you have committed everything in git?', true);
82
    }
83
84
    private static function requestIssue(string $path)
85
    {
86
        dump('(O_o)   Well, It seems we had some problem parsing the contents of:   (o_O)');
87
        dump('Submit an issue on github: https://github.com/imanghafoori1/microscope');
88
        dump('Send us the contents of: '.$path);
89
    }
90
}
91