Completed
Pull Request — master (#38)
by James
01:20
created

UndefinedVariableSolutionProvider::getSolutions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
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 RuntimeException;
7
use Facade\IgnitionContracts\Solution;
8
use Facade\IgnitionContracts\BaseSolution;
9
use Facade\Ignition\Exceptions\ViewException;
10
use Facade\IgnitionContracts\HasSolutionsForThrowable;
11
use Facade\Ignition\Solutions\MakeViewVariableOptionalSolution;
12
use Facade\Ignition\Solutions\SuggestCorrectVariableNameSolution;
13
14
class UndefinedVariableSolutionProvider implements HasSolutionsForThrowable
15
{
16
    private $variableName;
17
18
    private $viewFile;
19
20
    public function canSolve(Throwable $throwable): bool
21
    {
22
        if (! $throwable instanceof ViewException) {
23
            return false;
24
        }
25
        return $this->getNameAndView($throwable) !== null;
26
    }
27
28
    public function getSolutions(Throwable $throwable): array
29
    {
30
        $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...
31
32
        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...
33
        $solutions = collect($throwable->getViewData())->map(function ($value, $key) use ($variableName) {
34
            similar_text($variableName, $key, $percentage);
35
            return ['match' => $percentage, 'value' => $value ];
36
        })->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...
37
            return $var['match'] > 40;
38
        })->keys()->map(function($suggestion) use ($variableName, $viewFile) {
39
            return new SuggestCorrectVariableNameSolution($variableName, $viewFile, $suggestion);
40
        })->map(function($solution) {
41
            // If the solution isn't runnable, then just return the suggestions without the fix
42
            if ($solution->isRunnable()) {
43
                return $solution;
44
            } else {
45
                return BaseSolution::create($solution->getSolutionTitle())
46
                    ->setSolutionDescription($solution->getSolutionActionDescription());
47
            }
48
        })->toArray();
49
50
        $optionalSolution = new MakeViewVariableOptionalSolution($variableName, $viewFile);
51
        if ($optionalSolution->isRunnable()) {
52
            $solutions[] = $optionalSolution;
53
        } else {
54
            $solutions[] = BaseSolution::create($optionalSolution->getSolutionTitle())
55
                ->setSolutionDescription($optionalSolution->getSolutionActionDescription());
56
        }
57
        return $solutions;
58
    }
59
60
    private function getNameAndView(Throwable $throwable)
61
    {
62
        $pattern = '/Undefined variable: (.*?) \(View: (.*?)\)/';
63
64
        preg_match($pattern, $throwable->getMessage(), $matches);
65
        if (count($matches) === 3) {
66
            list($string, $variableName, $viewFile) = $matches;
0 ignored issues
show
Unused Code introduced by
The assignment to $string is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
67
            return compact('variableName', 'viewFile');
68
        }
69
        return null;
70
    }
71
}
72