Completed
Push — master ( 5bf22f...cf1632 )
by Kirill
06:39
created

AbstractResource::getAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php declare(strict_types=1);
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
namespace Gitter\Resources;
9
10
use Gitter\Adapters\AdapterInterface;
11
use Gitter\Adapters\HttpAdapter;
12
use Gitter\Adapters\StreamAdapter;
13
use Gitter\Route;
14
use Gitter\Client;
15
use Gitter\Support\Observer;
16
17
/**
18
 * Class AbstractResource
19
 * @package Gitter\Resources
20
 */
21
abstract class AbstractResource implements ResourceInterface
22
{
23
    /**
24
     * @var null
25
     */
26
    private $client;
27
28
    /**
29
     * AbstractResource constructor.
30
     * @param Client $client
31
     */
32
    public function __construct(Client $client)
33
    {
34
        $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...
35
    }
36
37
    /**
38
     * @return Client
39
     */
40
    protected function client(): Client
41
    {
42
        return $this->client;
43
    }
44
45
    /**
46
     * @param Route $route
47
     * @return array|mixed
48
     * @throws \RuntimeException
49
     * @throws \InvalidArgumentException
50
     */
51
    protected function fetch(Route $route): array
52
    {
53
        return (array)$this->viaHttp()->request($route);
54
    }
55
56
    /**
57
     * @param Route $route
58
     * @return Observer
59
     * @throws \InvalidArgumentException
60
     */
61
    protected function stream(Route $route): Observer
62
    {
63
        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...
64
    }
65
66
    /**
67
     * @return AdapterInterface|HttpAdapter
68
     */
69
    protected function viaHttp(): HttpAdapter
70
    {
71
        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...
72
    }
73
74
    /**
75
     * @return AdapterInterface|StreamAdapter
76
     */
77
    protected function viaStream(): StreamAdapter
78
    {
79
        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...
80
    }
81
}
82