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 ( ee1be5...1e72ef )
by Cees-Jan
03:09
created

IteratePagesService::handleResponse()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
ccs 0
cts 28
cp 0
cc 6
eloc 22
nc 7
nop 2
crap 42
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Github\Service;
4
5
use ApiClients\Foundation\Service\ServiceInterface;
6
use ApiClients\Foundation\Transport\Service\RequestService;
7
use Psr\Http\Message\ResponseInterface;
8
use React\Promise\CancellablePromiseInterface;
9
use React\Promise\FulfilledPromise;
10
use RingCentral\Psr7\Request;
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
final class IteratePagesService implements ServiceInterface
19
{
20
    /**
21
     * @var RequestService
22
     */
23
    private $requestService;
24
25
    /**
26
     * @param RequestService $requestService
27
     */
28
    public function __construct(RequestService $requestService)
29
    {
30
        $this->requestService = $requestService;
31
    }
32
33
    public function handle(string $path = null): 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 ($path) {
39
            $promise = $this->requestService->
40
                handle(new Request('GET', $path))->
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
        $promises = [];
76
77
        $promises[] = $this->requestService->
78
            handle(new Request('GET', $links['next']))->
79
            then(function (ResponseInterface $response) use ($observer) {
80
                return $this->handleResponse($response, $observer);
81
            })
82
        ;
83
84
        $promises[] = $this->handleResponseContents($response, $observer);
85
86
        return all($promises);
87
    }
88
89
    private function handleResponseContentsComplete(
90
        ResponseInterface $response,
91
        ObserverInterface $observer
92
    ): CancellablePromiseInterface {
93
        return $this->handleResponseContents($response, $observer)->then(function () use ($observer) {
94
            $observer->onCompleted();
95
            return new FulfilledPromise();
96
        });
97
    }
98
99
    private function handleResponseContents(
100
        ResponseInterface $response,
101
        ObserverInterface $observer
102
    ): CancellablePromiseInterface {
103
        $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...
104
        return new FulfilledPromise();
105
    }
106
}
107