GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#2)
by Cees-Jan
12:17 queued 02:04
created

Factory::createFromReactHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 5
1
<?php
2
declare(strict_types=1);
3
4
namespace ApiClients\Foundation\Transport;
5
6
use ApiClients\Foundation\Events\CommandLocatorEvent;
7
use Clue\React\Buzz\Browser;
8
use Clue\React\Buzz\Io\Sender;
9
use GuzzleHttp\Client as GuzzleClient;
10
use GuzzleHttp\HandlerStack;
11
use League\Container\ContainerInterface;
12
use League\Event\EmitterInterface;
13
use React\Dns\Resolver\Factory as ResolverFactory;
14
use React\Dns\Resolver\Resolver;
15
use React\EventLoop\Factory as LoopFactory;
16
use React\EventLoop\LoopInterface;
17
use React\HttpClient\Client as HttpClient;
18
use React\HttpClient\Factory as HttpClientFactory;
19
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
20
21
class Factory
22
{
23
    /**
24
     * @param ContainerInterface $container
25
     * @param LoopInterface|null $loop
26
     * @param array $options
27
     * @return Client
28
     */
29
    public static function create(
30
        ContainerInterface $container,
31
        LoopInterface $loop = null,
32
        array $options = []
33
    ): Client {
34
        $container->get(EmitterInterface::class)->
35
            addListener(CommandLocatorEvent::NAME, function (CommandLocatorEvent $event) {
36
                $event->add(
37
                    __DIR__ . DIRECTORY_SEPARATOR . 'CommandBus' . DIRECTORY_SEPARATOR,
38
                    __NAMESPACE__ . '\CommandBus'
39
                );
40
            })
41
        ;
42
43
        if (!($loop instanceof LoopInterface)) {
44
            $loop = LoopFactory::create();
45
        }
46
47
        $container->share(LoopInterface::class, $loop);
48
49
        if (!isset($options[Options::DNS])) {
50
            $options[Options::DNS] = '8.8.8.8';
51
        }
52
53
        $resolver = (new ResolverFactory())->createCached($options[Options::DNS], $loop);
54
        $httpClient = (new HttpClientFactory())->create($loop, $resolver);
55
56
        return self::createFromReactHttpClient(
57
            $container,
58
            $httpClient,
59
            $resolver,
60
            $loop,
61
            $options
62
        );
63
    }
64
65
    /**
66
     * @param ContainerInterface $container
67
     * @param HttpClient $httpClient
68
     * @param Resolver $resolver
69
     * @param LoopInterface|null $loop
70
     * @param array $options
71
     * @return Client
72
     */
73
    public static function createFromReactHttpClient(
74
        ContainerInterface $container,
75
        HttpClient $httpClient,
0 ignored issues
show
Unused Code introduced by
The parameter $httpClient 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...
76
        Resolver $resolver,
77
        LoopInterface $loop = null,
78
        array $options = []
79
    ): Client {
80
        return self::createFromBuzz(
81
            $container,
82
            $loop,
0 ignored issues
show
Bug introduced by
It seems like $loop defined by parameter $loop on line 77 can be null; however, ApiClients\Foundation\Tr...ctory::createFromBuzz() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
83
            (new Browser($loop, Sender::createFromLoopDns($loop, $resolver)))->withOptions([
0 ignored issues
show
Bug introduced by
It seems like $loop defined by parameter $loop on line 77 can be null; however, Clue\React\Buzz\Io\Sender::createFromLoopDns() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
Bug introduced by
It seems like $loop defined by parameter $loop on line 77 can be null; however, Clue\React\Buzz\Browser::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
84
                'streaming' => true,
85
            ]),
86
            $options
87
        );
88
    }
89
90
    /**
91
     * @param ContainerInterface $container
92
     * @param LoopInterface $loop
93
     * @param Browser $buzz
94
     * @param array $options
95
     * @return Client
96
     */
97
    public static function createFromBuzz(
98
        ContainerInterface $container,
99
        LoopInterface $loop,
100
        Browser $buzz,
101
        array $options = []
102
    ): Client {
103
        return new Client(
104
            $loop,
105
            $container,
106
            $buzz,
107
            $options
108
        );
109
    }
110
}
111