Completed
Pull Request — master (#58)
by James
02:02 queued 42s
created

MergeConflictSolutionProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canSolve() 0 18 6
A getSolutions() 0 13 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
    protected const REGEX = '/\[([a-zA-Z\\\\]+)\]/m';
14
15
    public function canSolve(Throwable $throwable): bool
16
    {
17
        if (! $throwable instanceof FatalThrowableError) {
18
            return false;
19
        }
20
21
        if (Str::startsWith($throwable->getMessage(), 'syntax error, unexpected \'<<\'')) {
22
            $file = file_get_contents($throwable->getFile());
23
            if (strpos($file, '<<<<<<<') !== false &&
24
                strpos($file, '=======') !== false &&
25
                strpos($file, '>>>>>>>') !== false
26
            ) {
27
                return true;
28
            }
29
        }
30
31
        return false;
32
    }
33
34
    public function getSolutions(Throwable $throwable): array
35
    {
36
        $file = file_get_contents($throwable->getFile());
37
        preg_match('/\>\>\>\>\>\>\> (.*?)\n/', $file, $matches);
38
        $source = $matches[1];
39
        $folder = basename($throwable->getFile());
40
        $target = trim(`cd $folder; git branch | grep \* | cut -d ' ' -f2`);
41
42
        return [
43
            BaseSolution::create("Merge conflict from branch '$source' into '$target'")
44
                ->setSolutionDescription('You have a Git merge conflict. To undo your merge do `git reset --hard HEAD`'),
45
        ];
46
    }
47
}
48