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
Push — master ( 3e9d6c...cd2816 )
by Cees-Jan
14:54 queued 13:31
created

ObservableBunny::onError()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 1
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\ObservableBunny;
4
5
use Bunny\Async\Client;
6
use Bunny\Channel;
7
use Bunny\Message as BunnyMessage;
8
use Bunny\Protocol\MethodBasicConsumeOkFrame;
9
use React\EventLoop\LoopInterface;
10
use React\EventLoop\Timer\TimerInterface;
11
use React\Promise\PromiseInterface;
12
use Rx\Subject\Subject;
13
use Throwable;
14
15
final class ObservableBunny
16
{
17
    /**
18
     * @var LoopInterface
19
     */
20
    private $loop;
21
22
    /**
23
     * @var Client
24
     */
25
    private $bunny;
26
27
    /**
28
     * @param LoopInterface $loop
29
     * @param Client        $bunny
30
     */
31 3
    public function __construct(LoopInterface $loop, Client $bunny)
32
    {
33 3
        $this->loop = $loop;
34 3
        $this->bunny = $bunny;
35 3
    }
36
37 3
    public function consume(
38
        string $queue = '',
39
        string $consumerTag = '',
40
        bool $noLocal = false,
41
        bool $noAck = false,
42
        bool $exclusive = false,
43
        bool $nowait = false,
44
        array $arguments = []
45
    ): Subject {
46 3
        $subject = new Subject();
47 3
        $consumeArgs = [$queue, $consumerTag, $noLocal, $noAck, $exclusive, $nowait, $arguments];
48
49 3
        $channel = $this->bunny->channel();
50 3
        $channel->then(function (Channel $channel) use ($subject, $consumeArgs) {
0 ignored issues
show
Bug introduced by
The method then does only exist in React\Promise\PromiseInterface, but not in Bunny\Channel.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
51
            /** @var string $consumerTag */
52 2
            $consumerTag = null;
53 2
            $timer = $this->loop->addPeriodicTimer(1, function () use ($channel, $subject, &$timer, &$consumerTag) {
54 1
                if (!$subject->isDisposed()) {
55
                    return;
56
                }
57
58 1
                $this->cancelSubscription(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface React\Promise\PromiseInterface as the method done() does only exist in the following implementations of said interface: React\Promise\FulfilledPromise, React\Promise\LazyPromise, React\Promise\Promise, React\Promise\RejectedPromise.

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...
59 1
                    $timer,
60 1
                    $channel,
61 1
                    $consumerTag
62 1
                )->done([$subject, 'onComplete'], $this->onError($subject, $timer));
63 2
            });
64 2
            $channel->consume(
0 ignored issues
show
Bug introduced by
The method then does only exist in React\Promise\PromiseInterface, but not in Bunny\Protocol\MethodBasicConsumeOkFrame.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65 2
                function (BunnyMessage $message, Channel $channel) use ($subject, &$timer, &$consumerTag) {
66 1
                    if ($subject->isDisposed()) {
67
                        $channel->nack($message);
68
                        $this->cancelSubscription(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface React\Promise\PromiseInterface as the method done() does only exist in the following implementations of said interface: React\Promise\FulfilledPromise, React\Promise\LazyPromise, React\Promise\Promise, React\Promise\RejectedPromise.

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...
69
                            $timer,
70
                            $channel,
71
                            $consumerTag
72
                        )->done([$subject, 'onComplete'], $this->onError($subject, $timer));
73
74
                        return;
75
                    }
76
77 1
                    $subject->onNext(new Message($message, $channel));
78 2
                },
79 2
                ...$consumeArgs
0 ignored issues
show
Documentation introduced by
$consumeArgs is of type array<integer,string|boo..."boolean","6":"array"}>, but the function expects a string.

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...
80 2
            )->then(function (MethodBasicConsumeOkFrame $response) use (&$consumerTag) {
81 1
                $consumerTag = $response->consumerTag;
82 2
            })->done(null, $this->onError($subject, $timer));
83 3
        })->done(null, [$subject, 'onError']);
84
85 3
        return $subject;
86
    }
87
88 1
    private function cancelSubscription(TimerInterface $timer, Channel $channel, string $consumerTag): PromiseInterface
89
    {
90 1
        $this->loop->cancelTimer($timer);
91
92 1
        return $channel->cancel($consumerTag)->then(function () use ($channel) {
0 ignored issues
show
Bug introduced by
The method then does only exist in React\Promise\PromiseInterface, but not in Bunny\Protocol\MethodBasicCancelOkFrame.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
93 1
            return $channel->close();
94 1
        });
95
    }
96
97
    private function onError(Subject $subject, TimerInterface $timer): callable
98
    {
99 2
        return function (Throwable $et) use ($subject, $timer) {
100 1
            if ($this->loop->isTimerActive($timer)) {
101 1
                $this->loop->cancelTimer($timer);
102
            }
103 1
            $subject->onError($et);
104 2
        };
105
    }
106
}
107