Completed
Push — master ( e2aab6...02a6e4 )
by Marcel
01:24
created

UndefinedVariableSolutionProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canSolve() 0 8 2
A getSolutions() 0 15 2
A findCorrectVariableSolutions() 0 17 2
A findOptionalVariableSolution() 0 9 2
A getNameAndView() 0 11 2
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Throwable;
6
use Facade\IgnitionContracts\BaseSolution;
7
use Facade\Ignition\Exceptions\ViewException;
8
use Facade\IgnitionContracts\HasSolutionsForThrowable;
9
use Facade\Ignition\Solutions\MakeViewVariableOptionalSolution;
10
use Facade\Ignition\Solutions\SuggestCorrectVariableNameSolution;
11
12
class UndefinedVariableSolutionProvider implements HasSolutionsForThrowable
13
{
14
    private $variableName;
15
16
    private $viewFile;
17
18
    public function canSolve(Throwable $throwable): bool
19
    {
20
        if (! $throwable instanceof ViewException) {
21
            return false;
22
        }
23
24
        return $this->getNameAndView($throwable) !== null;
25
    }
26
27
    public function getSolutions(Throwable $throwable): array
28
    {
29
        $solutions = [];
0 ignored issues
show
Unused Code introduced by
$solutions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
31
        extract($this->getNameAndView($throwable));
0 ignored issues
show
Bug introduced by
$this->getNameAndView($throwable) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
32
33
        if (! isset($variableName)) {
34
            return [];
35
        }
36
37
        $solutions = $this->findCorrectVariableSolutions($throwable, $variableName, $viewFile);
38
        $solutions[] = $this->findOptionalVariableSolution($variableName, $viewFile);
39
40
        return $solutions;
41
    }
42
43
    protected function findCorrectVariableSolutions(Throwable $throwable, string $variableName, string $viewFile): array
44
    {
45
        return collect($throwable->getViewData())->map(function ($value, $key) use ($variableName) {
46
            similar_text($variableName, $key, $percentage);
47
48
            return ['match' => $percentage, 'value' => $value];
49
        })->sortByDesc('match')->filter(function ($var, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
            return $var['match'] > 40;
51
        })->keys()->map(function ($suggestion) use ($variableName, $viewFile) {
52
            return new SuggestCorrectVariableNameSolution($variableName, $viewFile, $suggestion);
53
        })->map(function ($solution) {
54
            return $solution->isRunnable()
55
                ? $solution
56
                : BaseSolution::create($solution->getSolutionTitle())
57
                    ->setSolutionDescription($solution->getSolutionActionDescription());
58
        })->toArray();
59
    }
60
61
    protected function findOptionalVariableSolution(string $variableName, string $viewFile)
62
    {
63
        $optionalSolution = new MakeViewVariableOptionalSolution($variableName, $viewFile);
64
65
        return $optionalSolution->isRunnable()
66
            ? $optionalSolution
67
            : BaseSolution::create($optionalSolution->getSolutionTitle())
68
                ->setSolutionDescription($optionalSolution->getSolutionActionDescription());
69
    }
70
71
    protected function getNameAndView(Throwable $throwable): ?array
72
    {
73
        $pattern = '/Undefined variable: (.*?) \(View: (.*?)\)/';
74
75
        preg_match($pattern, $throwable->getMessage(), $matches);
76
        if (count($matches) === 3) {
77
            [$string, $variableName, $viewFile] = $matches;
0 ignored issues
show
Bug introduced by
The variable $string does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $variableName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $viewFile does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
78
79
            return compact('variableName', 'viewFile');
80
        }
81
    }
82
}
83