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

MergeConflictSolutionProvider::canSolve()   B

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 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
            return false;
21
        }
22
23
        $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...
24
25
        if (strpos($file, '=======') === false) {
26
            return false;
27
        }
28
29
        if (strpos($file, '>>>>>>>') === false) {
30
            return false;
31
        }
32
33
        return true;
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
44
        return [
45
            BaseSolution::create("Merge conflict from branch '$source' into '$target'")
46
                ->setSolutionDescription('You have a Git merge conflict. To undo your merge do `git reset --hard HEAD`'),
47
        ];
48
    }
49
}
50