Completed
Pull Request — master (#85)
by Tobias
02:16
created

ConfiguredClientsStrategy::getCandidates()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Http\HttplugBundle;
4
5
use Http\Client\HttpClient;
6
use Http\Discovery\Exception\StrategyUnavailableException;
7
use Http\Discovery\HttpClientDiscovery;
8
use Http\Discovery\Strategy\DiscoveryStrategy;
9
10
/**
11
 * A strategy that provide clients configured with HTTPlug bundle.
12
 *
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class ConfiguredClientsStrategy implements DiscoveryStrategy
16
{
17
    /**
18
     * @var HttpClient
19
     */
20
    private static $client;
21
22
    /**
23
     * @param HttpClient $httpClient
24
     */
25
    public function __construct(HttpClient $httpClient)
26
    {
27
        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...
28
29
        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...
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public static function getCandidates($type)
36
    {
37
        if (static::$client !== null && $type == 'Http\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...
38
            return [['class' => function() { 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...
39
        }
40
41
        return [];
42
    }
43
}
44