Completed
Pull Request — master (#94)
by James
01:25
created

MissingSemicolonSolutionProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSolutions() 0 11 2
A canSolve() 0 9 3
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Facade\Ignition\Solutions\FixMissingSemicolonSolution;
6
use Facade\IgnitionContracts\HasSolutionsForThrowable;
7
use ParseError;
8
use Symfony\Component\Debug\Exception\FatalThrowableError;
9
use Throwable;
10
11
class MissingSemicolonSolutionProvider implements HasSolutionsForThrowable
12
{
13
    protected const REGEX = "/syntax error, unexpected \'(.*?)\'(.*?);/";
14
15
    public function canSolve(Throwable $throwable): bool
16
    {
17
        if (! $throwable instanceof FatalThrowableError && ! $throwable instanceof ParseError) {
18
            return false;
19
        }
20
        preg_match(self::REGEX, $throwable->getMessage(), $matches);
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
22
        return isset($matches[1]);
23
    }
24
25
    public function getSolutions(Throwable $throwable): array
26
    {
27
        preg_match(self::REGEX, $throwable->getMessage(), $matches);
28
29
        $filePath = str_replace(base_path(), '', $throwable->getFile());
30
        $solution = new FixMissingSemicolonSolution($filePath, $throwable->getLine(), $matches[1]);
31
32
        return $solution->isRunnable()
33
            ? [$solution]
34
            : [];
35
    }
36
}
37