AbstractResource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of GitterApi package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Gitter\Resources;
11
12
use Gitter\Adapters\AdapterInterface;
13
use Gitter\Adapters\HttpAdapter;
14
use Gitter\Adapters\StreamAdapter;
15
use Gitter\Route;
16
use Gitter\Client;
17
use Gitter\Support\Observer;
18
use GuzzleHttp\Exception\ClientException;
19
use Serafim\Evacuator\Evacuator;
20
21
/**
22
 * Class AbstractResource
23
 * @package Gitter\Resources
24
 */
25
abstract class AbstractResource implements ResourceInterface
26
{
27
    /**
28
     * @var null
29
     */
30
    private $client;
31
32
    /**
33
     * AbstractResource constructor.
34
     * @param Client $client
35
     */
36
    public function __construct(Client $client)
37
    {
38
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<Gitter\Client> is incompatible with the declared type null of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
    }
40
41
    /**
42
     * @return Client
43
     */
44
    protected function client(): Client
45
    {
46
        return $this->client;
47
    }
48
49
    /**
50
     * @param Route $route
51
     * @return array|mixed
52
     * @throws \Exception
53
     * @throws \Throwable
54
     * @throws \InvalidArgumentException
55
     */
56
    protected function fetch(Route $route): array
57
    {
58
        $rescue = (new Evacuator(function () use ($route) {
59
            return (array)$this->viaHttp()->request($route);
60
        }))
61
            // If response has status code 4xx
62
            ->onError(function (ClientException $e) {
63
                $this->client->logger->error(\get_class($e) . '  ' . $e->getMessage());
64
65
                switch ($e->getResponse()->getStatusCode()) {
66
                    case 429: // 429 Too Many Requests
67
                        sleep(2);
68
                        return null;
69
                }
70
71
                throw $e;
72
            })
73
            // Other
74
            ->onError(function (\Exception $e) {
75
                $this->client->logger->error($e->getMessage());
76
                throw $e;
77
            })
78
            ->retries($this->client->getRetriesCount())
0 ignored issues
show
Bug introduced by
The method getRetriesCount cannot be called on $this->client (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
79
            ->invoke();
80
81
        return $rescue;
82
    }
83
84
    /**
85
     * @param Route $route
86
     * @return Observer
87
     * @throws \Throwable
88
     * @throws \InvalidArgumentException
89
     */
90
    protected function stream(Route $route): Observer
91
    {
92
        return (new Evacuator(function() use ($route) {
93
            return $this->viaStream()->request($route);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Gitter\Adapters\AdapterInterface as the method request() does only exist in the following implementations of said interface: Gitter\Adapters\HttpAdapter, Gitter\Adapters\StreamAdapter.

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...
94
        }))
95
            ->onError(function (\Exception $e) {
96
                $this->client->logger->error($e->getMessage());
97
            })
98
            ->retries($this->client->getRetriesCount())
0 ignored issues
show
Bug introduced by
The method getRetriesCount cannot be called on $this->client (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
99
            ->invoke();
100
    }
101
102
    /**
103
     * @return AdapterInterface|HttpAdapter
104
     * @throws \InvalidArgumentException
105
     */
106
    protected function viaHttp(): HttpAdapter
107
    {
108
        return $this->client->viaHttp();
0 ignored issues
show
Bug introduced by
The method viaHttp cannot be called on $this->client (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109
    }
110
111
    /**
112
     * @return AdapterInterface|StreamAdapter
113
     */
114
    protected function viaStream(): StreamAdapter
115
    {
116
        return $this->client->viaStream();
0 ignored issues
show
Bug introduced by
The method viaStream cannot be called on $this->client (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
117
    }
118
}
119