FetchAndHydrateService   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 14 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Tools\Services\Client;
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