Completed
Pull Request — master (#58)
by James
01:21
created

MergeConflictSolutionProvider::canSolve()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.0444
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Throwable;
6
use Illuminate\Support\Str;
7
use Facade\IgnitionContracts\BaseSolution;
8
use Facade\IgnitionContracts\HasSolutionsForThrowable;
9
use Symfony\Component\Debug\Exception\FatalThrowableError;
10
11
class MergeConflictSolutionProvider implements HasSolutionsForThrowable
12
{
13
    public function canSolve(Throwable $throwable): bool
14
    {
15
        if (! $throwable instanceof FatalThrowableError) {
16
            return false;
17
        }
18
19
        if (Str::startsWith($throwable->getMessage(), 'syntax error, unexpected \'<<\'')) {
20
            $file = file_get_contents($throwable->getFile());
21
            if (strpos($file, '<<<<<<<') !== false &&
22
                strpos($file, '=======') !== false &&
23
                strpos($file, '>>>>>>>') !== false
24
            ) {
25
                return true;
26
            }
27
        }
28
29
        return false;
30
    }
31
32
    public function getSolutions(Throwable $throwable): array
33
    {
34
        $file = file_get_contents($throwable->getFile());
35
        preg_match('/\>\>\>\>\>\>\> (.*?)\n/', $file, $matches);
36
        $source = $matches[1];
37
        $folder = basename($throwable->getFile());
38
        $target = trim(`cd $folder; git branch | grep \* | cut -d ' ' -f2`);
39
40
        return [
41
            BaseSolution::create("Merge conflict from branch '$source' into '$target'")
42
                ->setSolutionDescription('You have a Git merge conflict. To undo your merge do `git reset --hard HEAD`'),
43
        ];
44
    }
45
}
46