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
Push — master ( 2be333...34096d )
by Cees-Jan
10:45
created

src/AsyncClient.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Twitter;
4
5
use ApiClients\Foundation\Client;
6
use ApiClients\Foundation\Factory;
7
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
8
use ApiClients\Foundation\Options;
9
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
10
use ApiClients\Foundation\Transport\Options as TransportOptions;
11
use ApiClients\Middleware\Oauth1\Oauth1Middleware;
12
use ApiClients\Middleware\Oauth1\Options as Oauth1Options;
13
use ApiClients\Tools\CommandBus\CommandBusInterface;
14
use ApiClients\Tools\Psr7\Oauth1\Definition;
15
use GuzzleHttp\Psr7\Request;
16
use Psr\Http\Message\ResponseInterface;
17
use React\EventLoop\LoopInterface;
18
use React\Promise\PromiseInterface;
19
use function React\Promise\resolve;
20
21
final class AsyncClient implements AsyncClientInterface
22
{
23
24
    /**
25
     * @var Client
26
     */
27
    protected $client;
28
29
    /**
30
     * @var array
31
     */
32
    protected $options;
33
34
    /**
35
     * @var AsyncStreamingClient
36
     */
37
    protected $streamingClient;
38
    /**
39
     * @var string
40
     */
41
    private $consumerKey;
42
43
    /**
44
     * @var string
45
     */
46
    private $consumerSecret;
47
48
    /**
49
     * @var LoopInterface
50
     */
51
    private $loop;
52
53
    /**
54
     * AsyncClient constructor.
55
     * @param string        $consumerKey
56
     * @param string        $consumerSecret
57
     * @param LoopInterface $loop
58
     * @param array         $options
59
     * @param Client|null   $client
60
     */
61 2
    public function __construct(
62
        string $consumerKey,
63
        string $consumerSecret,
64
        LoopInterface $loop,
65
        array $options = [],
66
        Client $client = null
67
    ) {
68 2
        $this->consumerKey = $consumerKey;
69 2
        $this->consumerSecret = $consumerSecret;
70 2
        $this->loop = $loop;
71
72 2
        if (!($client instanceof Client)) {
73 2
            $this->options = ApiSettings::getOptions(
74 2
                $consumerKey,
75 2
                $consumerSecret,
76 2
                'Async',
77 2
                $options
78
            );
79
80 2
            $client = Factory::create($this->loop, $this->options);
81
        }
82
83 2
        $this->client = $client;
84 2
    }
85
86 2
    public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient
87
    {
88 2
        $options = $this->options;
89
        // @codingStandardsIgnoreStart
90 2
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken);
91 2
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret);
92
        // @codingStandardsIgnoreEnd
93
94 2
        return new self(
95 2
            $this->consumerKey,
96 2
            $this->consumerSecret,
97 2
            $this->loop,
98 2
            $options
99
        );
100
    }
101
102 1
    public function withOutAccessToken(): AsyncClient
103
    {
104 1
        $options = $this->options;
105
        // @codingStandardsIgnoreStart
106 1 View Code Duplication
        if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) {
107 1
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]);
108
        }
109 1 View Code Duplication
        if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) {
110 1
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]);
111
        }
112
        // @codingStandardsIgnoreEnd
113
114 1
        return new self(
115 1
            $this->consumerKey,
116 1
            $this->consumerSecret,
117 1
            $this->loop,
118 1
            $options
119
        );
120
    }
121
122
    public function getCommandBus(): CommandBusInterface
123
    {
124
        return $this->client->getFromContainer(CommandBusInterface::class);
125
    }
126
127 View Code Duplication
    public function profile(): PromiseInterface
128
    {
129
        return $this->client->handle(new RequestCommand(
130
            new Request('GET', 'account/verify_credentials.json')
131
        ))->then(function (ResponseInterface $response) {
132
            return resolve($this->client->handle(new HydrateCommand('Profile', $response->getBody()->getParsedContents())));
133
        });
134
    }
135
136 View Code Duplication
    public function user(string $user): PromiseInterface
137
    {
138
        return $this->client->handle(new RequestCommand(
139
            new Request('GET', 'users/show.json?screen_name=' . $user)
140
        ))->then(function (ResponseInterface $response) {
141
            return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getParsedContents())));
142
        });
143
    }
144
145
    public function tweet(string $status, array $tweet = []): PromiseInterface
146
    {
147
        $tweet['status'] = $status;
148
149
        return $this->client->handle(new RequestCommand(
150
            new Request('POST', 'statuses/update.json?' . http_build_query($tweet))
151
        ))->then(function (ResponseInterface $response) {
152
            return resolve($this->client->handle(new HydrateCommand('Tweet', $response->getBody()->getParsedContents())));
0 ignored issues
show
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...
153
        });
154
    }
155
156
    public function stream(): AsyncStreamingClientInterface
157
    {
158
        if (!($this->streamingClient instanceof AsyncStreamingClient)) {
159
            $this->streamingClient = new AsyncStreamingClient($this->client);
160
        }
161
162
        return $this->streamingClient;
163
    }
164
}
165