Completed
Pull Request — master (#87)
by Tobias
10:23
created

ConfiguredClientsStrategy::getCandidates()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 16
rs 8.8571
ccs 0
cts 0
cp 0
cc 5
eloc 8
nc 3
nop 1
crap 30
1
<?php
2
3
namespace Http\HttplugBundle\Discovery;
4
5
use Http\Client\HttpClient;
6
use Http\Client\HttpAsyncClient;
7
use Http\Discovery\HttpClientDiscovery;
8
use Http\Discovery\Strategy\DiscoveryStrategy;
9
use Symfony\Component\Console\ConsoleEvents;
10
use Symfony\Component\EventDispatcher\Event;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
use Symfony\Component\HttpKernel\KernelEvents;
13
14
/**
15
 * A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
16
 * we can use the web debug toolbar for clients found with the discovery.
17
 *
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface
21
{
22
    /**
23
     * @var HttpClient
24
     */
25
    private static $client;
26
27
    /**
28
     * @var HttpAsyncClient
29
     */
30
    private static $asyncClient;
31
32
    /**
33
     *
34
     * @param HttpClient $httpClient
35
     * @param HttpAsyncClient $asyncClient
36
     */
37
    public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
38
    {
39
        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...
40
        static::$asyncClient = $asyncClient;
0 ignored issues
show
Bug introduced by
Since $asyncClient is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $asyncClient 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...
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function getCandidates($type)
47
    {
48
        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...
49
            return [['class' => function () {
50
                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...
51
            }]];
52
        }
53
54
        if (static::$asyncClient !== null && $type == HttpAsyncClient::class) {
0 ignored issues
show
Bug introduced by
Since $asyncClient is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $asyncClient 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...
55
            return [['class' => function () {
56
                return static::$asyncClient;
0 ignored issues
show
Bug introduced by
Since $asyncClient is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $asyncClient 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...
57
            }]];
58
        }
59
60
        return [];
61
    }
62
63
    /**
64
     * Make sure to use our custom strategy.
65
     *
66
     * @param Event $e
67
     */
68
    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...
69
    {
70
        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...
71
    }
72
73
    /**
74
     * Whenever these events occur we make sure to add our strategy to the discovery.
75
     *
76
     * {@inheritdoc}
77
     */
78
    public static function getSubscribedEvents()
79
    {
80
        return [
81
            KernelEvents::REQUEST => ['onEvent', 1024],
82
            ConsoleEvents::COMMAND => ['onEvent', 1024],
83
        ];
84
    }
85
}
86