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.

AsyncClient::connections()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace ApiClients\Client\RabbitMQ\Management;
5
6
use ApiClients\Foundation\ClientInterface as FoundationClientInterface;
7
use ApiClients\Foundation\Factory;
8
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
9
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand;
10
use function ApiClients\Tools\Rx\observableFromArray;
11
use Psr\Http\Message\ResponseInterface;
12
use React\EventLoop\LoopInterface;
13
use React\Promise\PromiseInterface;
14
use function React\Promise\resolve;
15
use Rx\Observable;
16
use Rx\ObservableInterface;
17
use Rx\React\Promise;
18
use Rx\Scheduler\EventLoopScheduler;
19
20
final class AsyncClient implements AsyncClientInterface
21
{
22
    /**
23
     * @var FoundationClientInterface
24
     */
25
    private $client;
26
27
    /**
28
     * @param FoundationClientInterface $client
29
     */
30
    private function __construct(FoundationClientInterface $client)
31
    {
32
        $this->client = $client;
33
    }
34
35
    /**
36
     * Create a new AsyncClient based on the loop and other options pass.
37
     *
38
     * @param  LoopInterface $loop
39
     * @param  string        $baseUrl
40
     * @param  string        $username
41
     * @param  string        $password
42
     * @param  array         $options
43
     * @return AsyncClient
44
     */
45
    public static function create(
46
        LoopInterface $loop,
47
        string $baseUrl,
48
        string $username,
49
        string $password,
50
        array $options = []
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
51
    ): self {
52
        $options = ApiSettings::getOptions($baseUrl, $username, $password, 'Async');
53
        $client = Factory::create($loop, $options);
54
55
        return new self($client);
56
    }
57
58
    /**
59
     * Create an AsyncClient from a ApiClients\Foundation\ClientInterface.
60
     * Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix.
61
     *
62
     * @param  FoundationClientInterface $client
63
     * @return AsyncClient
64
     */
65
    public static function createFromClient(FoundationClientInterface $client): self
66
    {
67
        return new self($client);
68
    }
69
70
    /**
71
     * @return PromiseInterface
72
     */
73
    public function overview(): PromiseInterface
74
    {
75
        return $this->client->handle(
76
            new SimpleRequestCommand('overview')
77
        )->then(function (ResponseInterface $response) {
78
            return resolve($this->client->handle(
79
                new HydrateCommand('Overview', $response->getBody()->getParsedContents())
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\StreamInterface as the method getParsedContents() does only exist in the following implementations of said interface: ApiClients\Middleware\Json\JsonStream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
80
            ));
81
        });
82
    }
83
84
    /**
85
     * @param  int|null            $interval
86
     * @return ObservableInterface
87
     */
88
    public function queues(int $interval = null): ObservableInterface
89
    {
90
        if ($interval === null) {
91
            return Promise::toObservable($this->client->handle(
92
                new SimpleRequestCommand('queues')
93
            ))->flatMap(function (ResponseInterface $response) {
94
                return observableFromArray($response->getBody()->getParsedContents());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\StreamInterface as the method getParsedContents() does only exist in the following implementations of said interface: ApiClients\Middleware\Json\JsonStream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
95
            })->flatMap(function ($queue) {
96
                return Promise::toObservable($this->client->handle(
97
                    new HydrateCommand('Queue', $queue)
98
                ));
99
            });
100
        }
101
102
        $scheduler = new EventLoopScheduler($this->client->getFromContainer(LoopInterface::class));
0 ignored issues
show
Documentation introduced by
$this->client->getFromCo...p\LoopInterface::class) is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
104
        return Observable::interval($interval * 1000, $scheduler)->flatMap(function () {
105
            return $this->queues();
106
        });
107
    }
108
109
    /**
110
     * @return ObservableInterface
111
     */
112
    public function connections(): ObservableInterface
113
    {
114
        return Promise::toObservable($this->client->handle(
115
            new SimpleRequestCommand('connections')
116
        ))->flatMap(function (ResponseInterface $response) {
117
            return observableFromArray($response->getBody()->getParsedContents());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\StreamInterface as the method getParsedContents() does only exist in the following implementations of said interface: ApiClients\Middleware\Json\JsonStream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
118
        })->flatMap(function ($connection) {
119
            return Promise::toObservable($this->client->handle(
120
                new HydrateCommand('Connection', $connection)
121
            ));
122
        });
123
    }
124
}
125