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 ( 61a149...5b9c21 )
by Cees-Jan
9s
created

AsyncClient::profile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Twitter;
4
5
use ApiClients\Foundation\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApiClients\Client\Twitter\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use ApiClients\Foundation\Factory;
7
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
8
use ApiClients\Foundation\Oauth1\Middleware\Oauth1Middleware;
9
use ApiClients\Foundation\Oauth1\Options as Oauth1Options;
10
use ApiClients\Foundation\Options;
11
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
12
use ApiClients\Foundation\Transport\Options as TransportOptions;
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 Rx\Observable;
20
use function React\Promise\resolve;
21
22
final class AsyncClient implements AsyncClientInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $consumerKey;
28
29
    /**
30
     * @var string
31
     */
32
    private $consumerSecret;
33
34
    /**
35
     * @var LoopInterface
36
     */
37
    private $loop;
38
39
    /**
40
     * @var Client
41
     */
42
    protected $client;
43
44
    /**
45
     * @var array
46
     */
47
    protected $options;
48
49
    /**
50
     * @var AsyncStreamingClient
51
     */
52
    protected $streamingClient;
53
54
    /**
55
     * AsyncClient constructor.
56
     * @param string $consumerKey
57
     * @param string $consumerSecret
58
     * @param LoopInterface $loop
59
     * @param array $options
60
     * @param Client|null $client
61
     */
62
    public function __construct(
63
        string $consumerKey,
64
        string $consumerSecret,
65
        LoopInterface $loop,
66
        array $options = [],
67
        Client $client = null
68
    ) {
69
        $this->consumerKey = $consumerKey;
70
        $this->consumerSecret = $consumerSecret;
71
        $this->loop = $loop;
72
73
        if (!($client instanceof Client)) {
74
            $this->options = ApiSettings::getOptions(
75
                $consumerKey,
76
                $consumerSecret,
77
                'Async',
78
                $options
79
            );
80
81
            $client = Factory::create($this->loop, $this->options);
82
        }
83
84
        $this->client = $client;
85
    }
86
87
    public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient
88
    {
89
        $options = $this->options;
90
        // @codingStandardsIgnoreStart
91
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken);
92
        $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret);
93
        // @codingStandardsIgnoreEnd
94
95
        return new self(
96
            $this->consumerKey,
97
            $this->consumerSecret,
98
            $this->loop,
99
            $options
100
        );
101
    }
102
103
    public function withOutAccessToken(): AsyncClient
104
    {
105
        $options = $this->options;
106
        // @codingStandardsIgnoreStart
107 View Code Duplication
        if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]);
109
        }
110 View Code Duplication
        if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
            unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]);
112
        }
113
        // @codingStandardsIgnoreEnd
114
115
        return new self(
116
            $this->consumerKey,
117
            $this->consumerSecret,
118
            $this->loop,
119
            $options
120
        );
121
    }
122
123
    public function getCommandBus(): CommandBusInterface
124
    {
125
        return $this->client->getFromContainer(CommandBusInterface::class);
126
    }
127
128 View Code Duplication
    public function profile(): PromiseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        return $this->client->handle(new RequestCommand(
131
            new Request('GET', 'account/verify_credentials.json')
132
        ))->then(function (ResponseInterface $response) {
133
            return resolve($this->client->handle(new HydrateCommand('Profile', $response->getBody()->getJson())));
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...
134
        });
135
    }
136
137 View Code Duplication
    public function user(string $user): PromiseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        return $this->client->handle(new RequestCommand(
140
            new Request('GET', 'users/show.json?screen_name=' . $user)
141
        ))->then(function (ResponseInterface $response) {
142
            return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getJson())));
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...
143
        });
144
    }
145
146
    public function stream(): AsyncStreamingClient
147
    {
148
        if (!($this->streamingClient instanceof AsyncStreamingClient)) {
149
            $this->streamingClient = new AsyncStreamingClient($this->client);
150
        }
151
152
        return $this->streamingClient;
153
    }
154
}
155