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

MergeConflictSolutionProvider::canSolve()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
cc 7
nc 4
nop 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
    public function canSolve(Throwable $throwable): bool
14
    {
15
        if (! ($throwable instanceof FatalThrowableError || $throwable instanceof \ParseError)) {
16
            return false;
17
        }
18
19
        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...
20
            $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...
21
            if (strpos($file, '<<<<<<<') !== false &&
22
                strpos($file, '=======') !== false &&
23
                strpos($file, '>>>>>>>') !== false
24
            ) {
25
                return true;
26
            }
27
        }
28
29
        return false;
30
    }
31
32
    public function getSolutions(Throwable $throwable): array
33
    {
34
        $file = file_get_contents($throwable->getFile());
35
        preg_match('/\>\>\>\>\>\>\> (.*?)\n/', $file, $matches);
36
        $source = $matches[1];
37
        $folder = basename($throwable->getFile());
38
        $target = trim(`cd $folder; git branch | grep \* | cut -d ' ' -f2`);
39
40
        return [
41
            BaseSolution::create("Merge conflict from branch '$source' into '$target'")
42
                ->setSolutionDescription('You have a Git merge conflict. To undo your merge do `git reset --hard HEAD`'),
43
        ];
44
    }
45
}
46