ClosureParser::fetchContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TypeHints\Unused\Parser\Action;
4
5
use Illuminate\Routing\Route;
6
use ReflectionFunction;
7
8
class ClosureParser implements ParserActionInterface
9
{
10
    /**
11
     * @var Illuminate\Routing\Route
0 ignored issues
show
Bug introduced by
The type TypeHints\Unused\Parser\...lluminate\Routing\Route was not found. Did you mean Illuminate\Routing\Route? If so, make sure to prefix the type with \.
Loading history...
12
     */
13
    protected $route;
14
15
    /**
16
     * @var array
17
     */
18
    protected $content = [];
19
20
    /**
21
     * @var array
22
     */
23
    protected $errors = [];
24
25
    /**
26
     * @param Route $route
27
     */
28
    public function __construct(Route $route)
29
    {
30
        $this->route = $route;
0 ignored issues
show
Documentation Bug introduced by
It seems like $route of type Illuminate\Routing\Route is incompatible with the declared type TypeHints\Unused\Parser\...lluminate\Routing\Route of property $route.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
    }
32
33
    /**
34
     * @return TypeHints\Unused\Parser\Action\ParserActionInterface
0 ignored issues
show
Bug introduced by
The type TypeHints\Unused\Parser\...n\ParserActionInterface was not found. Did you mean TypeHints\Unused\Parser\...n\ParserActionInterface? If so, make sure to prefix the type with \.
Loading history...
35
     */
36
    public function parse(): ParserActionInterface
37
    {
38
        $this->content = $this->fetchContent();
39
40
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type TypeHints\Unused\Parser\Action\ClosureParser which is incompatible with the documented return type TypeHints\Unused\Parser\...n\ParserActionInterface.
Loading history...
41
    }
42
43
    /**
44
     * @return ReflectionFunction
45
     */
46
    protected function resolveMethod(): ReflectionFunction
47
    {
48
        return new ReflectionFunction($this->getClosure());
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    protected function fetchContent(): array
55
    {
56
        $method = $this->resolveMethod();
57
58
        return array_slice(
59
            file($method->getFileName()),
0 ignored issues
show
Bug introduced by
It seems like file($method->getFileName()) can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            /** @scrutinizer ignore-type */ file($method->getFileName()),
Loading history...
60
            $method->getStartLine() - 1,
61
            $method->getEndLine() - $method->getStartLine() + 1
62
        );
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    public function getContent(): array
69
    {
70
        return $this->content;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getErrors(): array
77
    {
78
        return $this->errors;
79
    }
80
81
    /**
82
     * @return callable
83
     */
84
    public function getClosure(): callable
85
    {
86
        return $this->route->getAction('uses');
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getRoute(): array
93
    {
94
        return array_merge($this->route->getAction(), [
95
            'methods' => $this->route->methods(),
96
            'uri'     => $this->route->uri,
97
        ]);
98
    }
99
}
100