Completed
Pull Request — master (#281)
by Lennart
01:37
created

getAvailableMethodsFromClassDoc()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
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
        if (! $doc = $class->getDocComment()) {
87
            return Collection::make([]);
88
        }
89
90
        $doc = DocBlockFactory::createInstance()->create($doc);
91
92
        $methods = [];
93
        foreach ($doc->getTagsByName('method') as $tag) {
94
            $methods[] = $tag->getMethodName();
95
        }
96
97
        return Collection::make($methods);
98
    }
99
}
100