Completed
Pull Request — master (#281)
by Lennart
24:31 queued 15:38
created

BadMethodCallSolutionProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 82
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A canSolve() 0 12 3
A getSolutions() 0 7 1
A getSolutionDescription() 0 12 2
A getClassAndMethodFromExceptionMessage() 0 11 2
A findPossibleMethod() 0 9 1
A getAvailableMethods() 0 9 1
A getAvailableMethodsFromClassDoc() 0 11 2
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use BadMethodCallException;
6
use Facade\IgnitionContracts\BaseSolution;
7
use Facade\IgnitionContracts\HasSolutionsForThrowable;
8
use Illuminate\Support\Collection;
9
use phpDocumentor\Reflection\DocBlockFactory;
10
use ReflectionClass;
11
use ReflectionMethod;
12
use Throwable;
13
14
class BadMethodCallSolutionProvider implements HasSolutionsForThrowable
15
{
16
    protected const REGEX = '/([a-zA-Z\\\\]+)::([a-zA-Z]+)/m';
17
18
    public function canSolve(Throwable $throwable): bool
19
    {
20
        if (! $throwable instanceof BadMethodCallException) {
21
            return false;
22
        }
23
24
        if (is_null($this->getClassAndMethodFromExceptionMessage($throwable->getMessage()))) {
25
            return false;
26
        }
27
28
        return true;
29
    }
30
31
    public function getSolutions(Throwable $throwable): array
32
    {
33
        return [
34
            BaseSolution::create('Bad Method Call')
35
            ->setSolutionDescription($this->getSolutionDescription($throwable)),
36
        ];
37
    }
38
39
    public function getSolutionDescription(Throwable $throwable): string
40
    {
41
        if (! $this->canSolve($throwable)) {
42
            return '';
43
        }
44
45
        extract($this->getClassAndMethodFromExceptionMessage($throwable->getMessage()), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
$this->getClassAndMethod...hrowable->getMessage()) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
46
47
        $possibleMethod = $this->findPossibleMethod($class, $method);
48
49
        return "Did you mean {$class}::{$possibleMethod}() ?";
50
    }
51
52
    protected function getClassAndMethodFromExceptionMessage(string $message): ?array
53
    {
54
        if (! preg_match(self::REGEX, $message, $matches)) {
55
            return null;
56
        }
57
58
        return [
59
            'class'  => $matches[1],
60
            'method' => $matches[2],
61
        ];
62
    }
63
64
    protected function findPossibleMethod(string $class, string $invalidMethodName)
65
    {
66
        return $this->getAvailableMethods($class)
67
            ->sortByDesc(function ($method) use ($invalidMethodName) {
68
                similar_text($invalidMethodName, $method, $percentage);
69
70
                return $percentage;
71
            })->first();
72
    }
73
74
    protected function getAvailableMethods($class): Collection
75
    {
76
        $class = new ReflectionClass($class);
77
78
        return Collection::make($class->getMethods())
79
        ->map(function (ReflectionMethod $method) {
80
            return $method->name;
81
        })->merge($this->getAvailableMethodsFromClassDoc($class));
82
    }
83
84
    public function getAvailableMethodsFromClassDoc(ReflectionClass $class): Collection
85
    {
86
        $doc = DocBlockFactory::createInstance()->create($class->getDocComment());
87
88
        $methods = [];
89
        foreach ($doc->getTagsByName('method') as $tag) {
90
            $methods[] = $tag->getMethodName();
91
        }
92
93
        return Collection::make($methods);
94
    }
95
}
96