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

MergeConflictSolutionProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canSolve() 0 17 6
A getSolutions() 0 12 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