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.

IteratePagesService::handleResponse()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8177
c 0
b 0
f 0
ccs 0
cts 15
cp 0
cc 6
nc 4
nop 2
crap 42
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Github\Service;
4
5
use ApiClients\Foundation\Transport\Service\RequestService;
6
use function Kelunik\LinkHeaderRfc5988\parseLinks;
7
use Psr\Http\Message\ResponseInterface;
8
use RingCentral\Psr7\Request;
9
use Rx\AsyncSchedulerInterface;
10
use Rx\Observable;
11
use Rx\Scheduler;
12
use Rx\Subject\Subject;
13
14
class IteratePagesService
15
{
16
    /**
17
     * @var RequestService
18
     */
19
    private $requestService;
20
21
    /**
22
     * @var AsyncSchedulerInterface
23
     */
24
    private $scheduler;
25
26
    /**
27
     * @param RequestService          $requestService
28
     * @param AsyncSchedulerInterface $scheduler
29
     */
30 1
    public function __construct(RequestService $requestService, AsyncSchedulerInterface $scheduler = null)
31
    {
32 1
        $this->scheduler      = $scheduler ?: Scheduler::getAsync();
33 1
        $this->requestService = $requestService;
34 1
    }
35
36 1
    public function iterate(string $path): Observable
37
    {
38 1
        $paths = new Subject();
39
40 1
        return Observable::of($path, $this->scheduler)
41 1
            ->merge($paths)
42 1
            ->flatMap(function ($path) {
43 1
                return Observable::fromPromise($this->requestService->request(new Request('GET', $path)));
44 1
            })
45 1
            ->do(function (ResponseInterface $response) use ($paths) {
46 1
                if (!$response->hasHeader('link')) {
47
                    $paths->onCompleted();
48
49
                    return;
50
                }
51
52 1
                $parsedLinks = parseLinks($response->getHeaderLine('link'));
53
                $links = [
54 1
                    'next' => $parsedLinks->getByRel('next'),
55 1
                    'last' => $parsedLinks->getByRel('last'),
56
                ];
57
58 1
                if ($links['next'] === null || $links['last'] === null) {
59 1
                    $paths->onCompleted();
60
61 1
                    return;
62
                }
63
64 1
                $this->scheduler->schedule(function () use ($paths, $links) {
65 1
                    $paths->onNext($links['next']->getUri());
66 1
                });
67 1
            })
68 1
            ->map(function (ResponseInterface $response) {
69 1
                return $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...
70 1
            });
71
    }
72
73
    private function sendRequest(string $path, Subject $subject)
74
    {
75
        $this->requestService->
76
            request(new Request('GET', $path))->
77
            then(
78
                function ($response) use ($subject) {
79
                    $this->handleResponse($response, $subject);
80
                },
81
                function ($error) use ($subject) {
82
                    $subject->onError($error);
83
                }
84
            )
85
        ;
86
    }
87
88
    private function handleResponse(
89
        ResponseInterface $response,
90
        Subject $subject
91
    ) {
92
        $subject->onNext($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...
93
94
        if ($subject->isDisposed() || !$subject->hasObservers()) {
95
            $subject->onCompleted();
96
97
            return;
98
        }
99
100
        if (!$response->hasHeader('link')) {
101
            $subject->onCompleted();
102
103
            return;
104
        }
105
106
        $parsedLinks = parseLinks($response->getHeaderLine('link'));
107
        $links = [
108
            'next' => $parsedLinks->getByRel('next'),
109
            'last' => $parsedLinks->getByRel('last'),
110
        ];
111
112
        if ($links['next'] === null || $links['last'] === null) {
113
            $subject->onCompleted();
114
        }
115
116
        $this->sendRequest($links['next']->getUri(), $subject);
117
    }
118
}
119