Completed
Push — master ( 0e9818...d1eb6b )
by Pascal
01:12
created

QualifiedRoute::replaceParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Protonemedia\LaravelTracer;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Route;
7
use Illuminate\Support\Collection;
8
9
class QualifiedRoute
10
{
11
    /**
12
     * Name of the qualified route.
13
     *
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * Seconds between the logs of the qualified route.
20
     *
21
     * @var bool|int|null
22
     */
23
    private $secondsBetweenLogs;
24
25
    /**
26
     * Setups this instance.
27
     *
28
     * @param \Illuminate\Routing\Route  $route
29
     * @param string $name
30
     * @param bool|int|null $secondsBetweenLogs
31
     */
32
    public function __construct(Route $route, string $name, $secondsBetweenLogs = null)
33
    {
34
        $this->name               = $this->replaceParameters($name, $route->parameters());
35
        $this->secondsBetweenLogs = is_null($secondsBetweenLogs) ? config('laravel-tracer.seconds_between_logs') : $secondsBetweenLogs;
36
    }
37
38
    /**
39
     * Replaces the parameters within the qualified route with
40
     * the actual given parameters (of the route).
41
     *
42
     * @param  string $name
43
     * @param  array  $parameters
44
     *
45
     * @return string
46
     */
47
    private function replaceParameters(string $name, array $parameters): string
48
    {
49
        Collection::make($parameters)->map(function ($value, $parameter) use (&$name) {
50
            $name = str_replace("{{$parameter}}", $value, $name);
51
        });
52
53
        return $name;
54
    }
55
56
    /**
57
     * Returns a new instance based on the given request.
58
     *
59
     * @param  \Illuminate\Http\Request $request
60
     * @return $this
61
     */
62
    public static function fromRequest(Request $request)
63
    {
64
        return new static(
65
            $route = $request->route(),
0 ignored issues
show
Documentation introduced by
$route = $request->route() is of type object|string, but the function expects a object<Illuminate\Routing\Route>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
            $name = $route->getName() ?: $request->path()
67
        );
68
    }
69
70
    /**
71
     * Getter for the name
72
     *
73
     * @return string
74
     */
75
    public function name(): string
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     * Getter fot the seconds between logs.
82
     *
83
     * @return mixed
84
     */
85
    public function secondsBetweenLogs()
86
    {
87
        return $this->secondsBetweenLogs;
88
    }
89
}
90