Completed
Push — master ( f1f5ae...d6cfaa )
by Freek
01:26
created

MergeConflictSolutionProvider::getCurrentBranch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Throwable;
6
use ParseError;
7
use Illuminate\Support\Str;
8
use Facade\IgnitionContracts\BaseSolution;
9
use Facade\IgnitionContracts\HasSolutionsForThrowable;
10
use Symfony\Component\Debug\Exception\FatalThrowableError;
11
12
class MergeConflictSolutionProvider implements HasSolutionsForThrowable
13
{
14
    public function canSolve(Throwable $throwable): bool
15
    {
16
        if (! ($throwable instanceof FatalThrowableError || $throwable instanceof ParseError)) {
17
            return false;
18
        }
19
20
        if (! Str::startsWith($throwable->getMessage(), 'syntax error, unexpected \'<<\'')) {
0 ignored issues
show
Bug introduced by
The method getMessage does only exist in ParseError, but not in Symfony\Component\Debug\...ion\FatalThrowableError.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
21
            return false;
22
        }
23
24
        $file = file_get_contents($throwable->getFile());
0 ignored issues
show
Bug introduced by
The method getFile does only exist in ParseError, but not in Symfony\Component\Debug\...ion\FatalThrowableError.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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($folder)
52
    {
53
        $branch = "'".trim(`cd $folder; git branch | grep \* | cut -d ' ' -f2`)."'";
54
55
        if (! isset($branch) || $branch === "''") {
56
            $branch = 'current branch';
57
        }
58
59
        return $branch;
60
    }
61
}
62