Completed
Push — master ( be6f9f...91f885 )
by David
06:59
created

ConfiguredClientsStrategy::onEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Http\HttplugBundle\Discovery;
4
5
use Http\Client\HttpClient;
6
use Http\Discovery\HttpClientDiscovery;
7
use Http\Discovery\Strategy\DiscoveryStrategy;
8
use Symfony\Component\Console\ConsoleEvents;
9
use Symfony\Component\EventDispatcher\Event;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
/**
14
 * A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
15
 * we can use the web debug toolbar for clients found with the discovery.
16
 *
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface
20
{
21
    /**
22
     * @var HttpClient
23
     */
24
    private static $client;
25
26
    /**
27
     * @param HttpClient $httpClient
28
     */
29
    public function __construct(HttpClient $httpClient)
30
    {
31
        static::$client = $httpClient;
0 ignored issues
show
Bug introduced by
Since $client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public static function getCandidates($type)
38
    {
39
        if (static::$client !== null && $type == HttpClient::class) {
0 ignored issues
show
Bug introduced by
Since $client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
40
            return [['class' => function () {
41
                return static::$client;
0 ignored issues
show
Bug introduced by
Since $client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
42
            }]];
43
        }
44
45
        return [];
46
    }
47
48
    /**
49
     * Make sure to use our custom strategy.
50
     *
51
     * @param Event $e
52
     */
53
    public function onEvent(Event $e)
1 ignored issue
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        HttpClientDiscovery::prependStrategy(self::class);
0 ignored issues
show
Bug introduced by
The method prependStrategy() does not seem to exist on object<Http\Discovery\HttpClientDiscovery>.

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...
56
    }
57
58
    /**
59
     * Whenever these events occur we make sure to add our strategy to the discovery.
60
     *
61
     * {@inheritdoc}
62
     */
63
    public static function getSubscribedEvents()
64
    {
65
        return [
66
            KernelEvents::REQUEST => ['onEvent', 1024],
67
            ConsoleEvents::COMMAND => ['onEvent', 1024],
68
        ];
69
    }
70
}
71