MergeConflictSolutionProvider::canSolve()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Facade\IgnitionContracts\BaseSolution;
6
use Facade\IgnitionContracts\HasSolutionsForThrowable;
7
use Illuminate\Support\Str;
8
use ParseError;
9
use Symfony\Component\Debug\Exception\FatalThrowableError;
10
use Throwable;
11
12
class MergeConflictSolutionProvider implements HasSolutionsForThrowable
13
{
14
    public function canSolve(Throwable $throwable): bool
15
    {
16
        if (! ($throwable instanceof FatalThrowableError || $throwable instanceof ParseError)) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Debug\...ion\FatalThrowableError does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
17
            return false;
18
        }
19
20
        if (! Str::startsWith($throwable->getMessage(), 'syntax error, unexpected \'<<\'')) {
21
            return false;
22
        }
23
24
        $file = file_get_contents($throwable->getFile());
25
26
        if (strpos($file, '=======') === false) {
27
            return false;
28
        }
29
30
        if (strpos($file, '>>>>>>>') === false) {
31
            return false;
32
        }
33
34
        return true;
35
    }
36
37
    public function getSolutions(Throwable $throwable): array
38
    {
39
        $file = file_get_contents($throwable->getFile());
40
        preg_match('/\>\>\>\>\>\>\> (.*?)\n/', $file, $matches);
41
        $source = $matches[1];
42
43
        $target = $this->getCurrentBranch(basename($throwable->getFile()));
44
45
        return [
46
            BaseSolution::create("Merge conflict from branch '$source' into $target")
47
                ->setSolutionDescription('You have a Git merge conflict. To undo your merge do `git reset --hard HEAD`'),
48
        ];
49
    }
50
51
    private function getCurrentBranch(string $directory): string
52
    {
53
        $branch = "'".trim(shell_exec("cd ${directory}; git branch | grep \\* | cut -d ' ' -f2"))."'";
54
55
        if (! isset($branch) || $branch === "''") {
56
            $branch = 'current branch';
57
        }
58
59
        return $branch;
60
    }
61
}
62