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 ( 3e65ba...cab908 )
by Cees-Jan
08:21
created

IteratePagesHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Github\CommandBus\Handler;
4
5
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand;
6
use ApiClients\Client\Github\CommandBus\Command\IteratePagesCommand;
7
use ApiClients\Tools\CommandBus\CommandBus;
8
use Psr\Http\Message\ResponseInterface;
9
use React\Promise\CancellablePromiseInterface;
10
use React\Promise\FulfilledPromise;
11
use Rx\Disposable\CallbackDisposable;
12
use Rx\Observable;
13
use Rx\ObserverInterface;
14
use Rx\SchedulerInterface;
15
use function React\Promise\all;
16
use function React\Promise\resolve;
17
18
class IteratePagesHandler
19
{
20
    /**
21
     * @var CommandBus
22
     */
23
    private $commandBus;
24
25
    /**
26
     * @param CommandBus $commandBus
27
     */
28
    public function __construct(CommandBus $commandBus)
29
    {
30
        $this->commandBus = $commandBus;
31
    }
32
33
    public function handle(IteratePagesCommand $command): CancellablePromiseInterface
34
    {
35
        return resolve(Observable::create(function (
36
            ObserverInterface $observer,
37
            SchedulerInterface $scheduler
0 ignored issues
show
Unused Code introduced by
The parameter $scheduler 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...
38
        ) use ($command) {
39
            $promise = $this->commandBus->
40
                handle(new SimpleRequestCommand($command->getPath()))->
41
                done(function ($response) use ($observer) {
42
                    return $this->handleResponse($response, $observer);
43
                })
44
            ;
45
46
            return new CallbackDisposable(function () use ($promise) {
47
                $promise->cancel();
48
            });
49
        }));
50
    }
51
52
    private function handleResponse(
53
        ResponseInterface $response,
54
        ObserverInterface $observer
55
    ): CancellablePromiseInterface {
56
        if (!$response->hasHeader('link')) {
57
            return $this->handleResponseContentsComplete($response, $observer);
58
        }
59
60
        $links = [
61
            'next' => false,
62
            'last' => false,
63
        ];
64
        foreach (explode(', ', $response->getHeader('link')[0]) as $link) {
65
            list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<'));
66
            if (isset($links[$rel])) {
67
                $links[$rel] = $url;
68
            }
69
        }
70
71
        if ($links['next'] === false || $links['last'] === false) {
72
            return $this->handleResponseContentsComplete($response, $observer);
73
        }
74
75
        if ($links['next'] == $links['last']) {
76
            return $this->handleResponseContentsComplete($response, $observer);
77
        }
78
79
        $promises = [];
80
81
        $promises[] = $this->commandBus->
82
            handle(new SimpleRequestCommand($links['next']))->
83
            then(function (ResponseInterface $response) use ($observer) {
84
                return $this->handleResponse($response, $observer);
85
            })
86
        ;
87
88
        $promises[] = $this->handleResponseContents($response, $observer);
89
90
        return all($promises);
91
    }
92
93
    private function handleResponseContentsComplete(
94
        ResponseInterface $response,
95
        ObserverInterface $observer
96
    ): CancellablePromiseInterface {
97
        return $this->handleResponseContents($response, $observer)->then(function () use ($observer) {
98
            $observer->onCompleted();
99
            return new FulfilledPromise();
100
        });
101
    }
102
103
    private function handleResponseContents(
104
        ResponseInterface $response,
105
        ObserverInterface $observer
106
    ): CancellablePromiseInterface {
107
        $observer->onNext($response->getBody()->getJson());
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 getJson() does only exist in the following implementations of said interface: ApiClients\Foundation\Transport\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...
108
        return new FulfilledPromise();
109
    }
110
}
111