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 (#1)
by Cees-Jan
03:10
created

FetchAndHydrateService::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 3
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Travis\Service;
4
5
use ApiClients\Foundation\Hydrator\Hydrator;
6
use ApiClients\Foundation\Service\ServiceInterface;
7
use ApiClients\Foundation\Transport\Service\RequestService;
8
use Psr\Http\Message\ResponseInterface;
9
use React\Promise\CancellablePromiseInterface;
10
use RingCentral\Psr7\Request;
11
use Rx\Observable;
12
use function igorw\get_in;
13
use function React\Promise\resolve;
14
use function WyriHaximus\React\futureFunctionPromise;
15
16
class FetchAndHydrateService implements ServiceInterface
17
{
18
    /**
19
     * @var RequestService
20
     */
21
    private $requestService;
22
23
    /**
24
     * @var Hydrator
25
     */
26
    private $hydrator;
27
28
    /**
29
     * @param RequestService $requestService
30
     * @param Hydrator $hydrator
31
     */
32
    public function __construct(RequestService $requestService, Hydrator $hydrator)
33
    {
34
        $this->requestService = $requestService;
35
        $this->hydrator = $hydrator;
36
    }
37
38
    /**
39
     * @param string|null $path
40
     * @param string|null $index
41
     * @param string|null $hydrateClass
42
     * @return CancellablePromiseInterface
43
     */
44
    public function handle(
45
        string $path = null,
46
        string $index = null,
47
        string $hydrateClass = null
48
    ): CancellablePromiseInterface {
49
        return $this->requestService->handle(
50
            new Request('GET', $path)
51
        )->then(function (ResponseInterface $response) use ($hydrateClass, $index) {
52
            return $this->hydrator->hydrate(
53
                $hydrateClass,
54
                get_in($response->getBody()->getJson(), explode('.', $index), [])
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...
55
            );
56
        });
57
    }
58
}
59