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

MergeConflictSolutionProvider::getSolutions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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