Completed
Push — master ( 623824...728a12 )
by Hannes
02:28
created

ServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 9.52%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 64
ccs 2
cts 21
cp 0.0952
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A provides() 0 8 1
A register() 0 41 3
1
<?php
2
namespace GuzzleHttp\Profiling\Debugbar\Support\Laravel;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\HandlerStack;
7
use GuzzleHttp\MessageFormatter;
8
use GuzzleHttp\Middleware as GuzzleMiddleware;
9
use GuzzleHttp\Profiling\Debugbar\ExceptionMiddleware;
10
use GuzzleHttp\Profiling\Debugbar\Profiler;
11
use GuzzleHttp\Profiling\Middleware;
12
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
13
14
class ServiceProvider extends BaseServiceProvider
15
{
16
    /**
17
     * @var bool
18
     */
19
    protected $defer = true;
20
21
    /**
22
     * @return array
23
     */
24 1
    public function provides()
25
    {
26
        return [
27 1
            Client::class,
28
            ClientInterface::class,
29
            HandlerStack::class,
30
        ];
31
    }
32
33
    /**
34
     * Register method.
35
     */
36
    public function register()
37
    {
38
        // Configuring all guzzle clients.
39
        $this->app->bind(ClientInterface::class, function () {
0 ignored issues
show
Bug introduced by
The method bind() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
            // Guzzle client
41
            return new Client(['handler' => $this->app->make(HandlerStack::class)]);
0 ignored issues
show
Bug introduced by
The method make() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        });
43
44
        $this->app->alias(ClientInterface::class, Client::class);
0 ignored issues
show
Bug introduced by
The method alias() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
46
        // Bind if needed.
47
        $this->app->bind(HandlerStack::class, function () {
0 ignored issues
show
Bug introduced by
The method bind() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
            return HandlerStack::create();
49
        });
50
51
        // If resolved, by this SP or another, add some layers.
52
        $this->app->resolving(HandlerStack::class, function (HandlerStack $stack) {
0 ignored issues
show
Bug introduced by
The method resolving() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            /** @var \DebugBar\DebugBar $debugBar */
54
            $debugBar = $this->app->make('debugbar');
0 ignored issues
show
Bug introduced by
The method make() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
56
            $stack->push(new Middleware(new Profiler($timeline = $debugBar->getCollector('time'))));
0 ignored issues
show
Compatibility introduced by
$timeline = $debugBar->getCollector('time') of type object<DebugBar\DataColl...DataCollectorInterface> is not a sub-type of object<DebugBar\DataCollector\TimeDataCollector>. It seems like you assume a concrete implementation of the interface DebugBar\DataCollector\DataCollectorInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
57
            $stack->unshift(new ExceptionMiddleware($debugBar->getCollector('exceptions')));
0 ignored issues
show
Compatibility introduced by
$debugBar->getCollector('exceptions') of type object<DebugBar\DataColl...DataCollectorInterface> is not a sub-type of object<DebugBar\DataColl...or\ExceptionsCollector>. It seems like you assume a concrete implementation of the interface DebugBar\DataCollector\DataCollectorInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58
59
            /** @var \GuzzleHttp\MessageFormatter $formatter */
60
            $formatter = $this->app->make(MessageFormatter::class);
0 ignored issues
show
Bug introduced by
The method make() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
            $stack->unshift(GuzzleMiddleware::log($debugBar->getCollector('messages'), $formatter));
0 ignored issues
show
Documentation introduced by
$debugBar->getCollector('messages') is of type object<DebugBar\DataColl...DataCollectorInterface>, but the function expects a object<Psr\Log\LoggerInterface>.

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...
62
63
            // Also log to the default PSR logger.
64
            if ($this->app->bound(LoggerInterface::class)) {
0 ignored issues
show
Bug introduced by
The method bound() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
                $logger = $this->app->make(LoggerInterface::class);
0 ignored issues
show
Bug introduced by
The method make() does not seem to exist on object<App>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
67
                // Don't log to the same logger twice.
68
                if ($logger === $debugBar->getCollector('messages')) {
69
                    return;
70
                }
71
72
                // Push the middleware on the stack.
73
                $stack->unshift(GuzzleMiddleware::log($logger, $formatter));
74
            }
75
        });
76
    }
77
}
78