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
08:57
created

Factory::createFromBuzz()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 4
1
<?php
2
declare(strict_types=1);
3
4
namespace ApiClients\Foundation\Transport;
5
6
use ApiClients\Foundation\Events\CommandLocatorEvent;
7
use ApiClients\Foundation\Events\ServiceLocatorEvent;
8
use Clue\React\Buzz\Browser;
9
use Clue\React\Buzz\Io\Sender;
10
use GuzzleHttp\Client as GuzzleClient;
11
use GuzzleHttp\HandlerStack;
12
use League\Container\ContainerInterface;
13
use League\Event\EmitterInterface;
14
use React\Dns\Resolver\Factory as ResolverFactory;
15
use React\Dns\Resolver\Resolver;
16
use React\EventLoop\Factory as LoopFactory;
17
use React\EventLoop\LoopInterface;
18
use React\HttpClient\Client as HttpClient;
19
use React\HttpClient\Factory as HttpClientFactory;
20
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
21
22
class Factory
23
{
24
    /**
25
     * @param ContainerInterface $container
26
     * @param LoopInterface|null $loop
27
     * @param array $options
28
     * @return Client
29
     */
30
    public static function create(
31
        ContainerInterface $container,
32
        LoopInterface $loop = null,
33
        array $options = []
34
    ): Client {
35
        $container->get(EmitterInterface::class)->
36
            addListener(CommandLocatorEvent::NAME, function (CommandLocatorEvent $event) {
37
                $event->add(
38
                    __DIR__ . DIRECTORY_SEPARATOR . 'CommandBus' . DIRECTORY_SEPARATOR,
39
                    __NAMESPACE__ . '\CommandBus'
40
                );
41
            })
42
        ;
43
44
        $container->get(EmitterInterface::class)->
45
            addListener(ServiceLocatorEvent::NAME, function (ServiceLocatorEvent $event) {
46
                $event->add(
47
                    __DIR__ . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR,
48
                    __NAMESPACE__ . '\Service'
49
                );
50
            })
51
        ;
52
53
        if (!($loop instanceof LoopInterface)) {
54
            $loop = LoopFactory::create();
55
        }
56
57
        $container->share(LoopInterface::class, $loop);
58
59
        if (!isset($options[Options::DNS])) {
60
            $options[Options::DNS] = '8.8.8.8';
61
        }
62
63
        $resolver = (new ResolverFactory())->createCached($options[Options::DNS], $loop);
64
        $httpClient = (new HttpClientFactory())->create($loop, $resolver);
65
66
        return self::createFromReactHttpClient(
67
            $container,
68
            $httpClient,
69
            $resolver,
70
            $loop,
71
            $options
72
        );
73
    }
74
75
    /**
76
     * @param ContainerInterface $container
77
     * @param HttpClient $httpClient
78
     * @param Resolver $resolver
79
     * @param LoopInterface|null $loop
80
     * @param array $options
81
     * @return Client
82
     */
83
    public static function createFromReactHttpClient(
84
        ContainerInterface $container,
85
        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...
86
        Resolver $resolver,
87
        LoopInterface $loop = null,
88
        array $options = []
89
    ): Client {
90
        return self::createFromBuzz(
91
            $container,
92
            $loop,
0 ignored issues
show
Bug introduced by
It seems like $loop defined by parameter $loop on line 87 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...
93
            (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 87 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 87 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...
94
                'streaming' => true,
95
            ]),
96
            $options
97
        );
98
    }
99
100
    /**
101
     * @param ContainerInterface $container
102
     * @param LoopInterface $loop
103
     * @param Browser $buzz
104
     * @param array $options
105
     * @return Client
106
     */
107
    public static function createFromBuzz(
108
        ContainerInterface $container,
109
        LoopInterface $loop,
110
        Browser $buzz,
111
        array $options = []
112
    ): Client {
113
        return new Client(
114
            $loop,
115
            $container,
116
            $buzz,
117
            $options
118
        );
119
    }
120
}
121