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 (#6)
by Cees-Jan
02:43
created

IteratePagesService::handleResponseContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 2
cts 3
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1.037
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 RingCentral\Psr7\Request;
10
use Rx\Disposable\CallbackDisposable;
11
use Rx\Observable;
12
use Rx\ObserverInterface;
13
use Rx\SchedulerInterface;
14
use Rx\Subject\Subject;
15
use function React\Promise\all;
16
use function React\Promise\resolve;
17
18
class IteratePagesService implements ServiceInterface
19
{
20
    /**
21
     * @var RequestService
22
     */
23
    private $requestService;
24
25
    /**
26
     * @param RequestService $requestService
27
     */
28 2
    public function __construct(RequestService $requestService)
29
    {
30 2
        $this->requestService = $requestService;
31 2
    }
32
33 2
    public function handle(string $path = null): CancellablePromiseInterface
34
    {
35
        return resolve(Observable::create(function (
36
            ObserverInterface $observer,
37
            SchedulerInterface $scheduler
38
        ) use ($path) {
39
40 2
            $subject = new Subject();
41 2
            $subject->asObservable()->subscribeCallback(
42 2
                [$observer, 'onNext'],
43 2
                [$observer, 'onError'],
44 2
                [$observer, 'onCompleted'],
45
                $scheduler
46
            );
47
48 2
            $this->sendRequest($path, $subject);
49
50
            return new CallbackDisposable(function () use ($subject) {
51 1
                $subject->dispose();
52 2
            });
53 2
        }));
54
    }
55
56 2
    private function sendRequest(string $path, Subject $subject)
57
    {
58 2
        $this->requestService->
59 2
            handle(new Request('GET', $path))->
60 2
            then(
61
                function ($response) use ($subject) {
62 2
                    $this->handleResponse($response, $subject);
63 2
                },
64 2
                function ($error) use ($subject) {
65
                    $subject->onError($error);
66 2
                }
67
            )
68
        ;
69 2
    }
70
71 2
    private function handleResponse(
72
        ResponseInterface $response,
73
        Subject $subject
74
    ) {
75 2
        $subject->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...
76
77 2
        if ($subject->isDisposed() || !$subject->hasObservers()) {
78
            $subject->onCompleted();
79
            return;
80
        }
81
82 2
        if (!$response->hasHeader('link')) {
83
            $subject->onCompleted();
84
            return;
85
        }
86
87
        $links = [
88 2
            'next' => false,
89
            'last' => false,
90
        ];
91 2
        foreach (explode(', ', $response->getHeader('link')[0]) as $link) {
92 2
            list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<'));
93 2
            if (isset($links[$rel])) {
94 2
                $links[$rel] = $url;
95
            }
96
        }
97
98 2
        if ($links['next'] === false || $links['last'] === false) {
99 1
            $subject->onCompleted();
100 1
            return;
101
        }
102
103 2
        $this->sendRequest($links['next'], $subject);
104 2
    }
105
}
106