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
Pull Request — master (#1)
by Cees-Jan
03:14
created

Repository::commits()   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
namespace ApiClients\Client\Travis\Resource\Async;
4
5
use ApiClients\Client\Pusher\CommandBus\Command\SharedAppClientCommand;
6
use ApiClients\Client\Travis\ApiSettings;
7
use ApiClients\Client\Travis\CommandBus\Command\BranchesCommand;
8
use ApiClients\Client\Travis\CommandBus\Command\CachesCommand;
9
use ApiClients\Client\Travis\CommandBus\Command\CommitsCommand;
10
use ApiClients\Client\Travis\CommandBus\Command\RepositoryCommand;
11
use ApiClients\Client\Travis\CommandBus\Command\RepositoryKeyCommand;
12
use ApiClients\Client\Travis\CommandBus\Command\SettingsCommand;
13
use ApiClients\Client\Travis\CommandBus\Command\VarsCommand;
14
use ApiClients\Client\Travis\Resource\Repository as BaseRepository;
15
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
16
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
17
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand;
18
use ApiClients\Foundation\Transport\JsonStream;
19
use GuzzleHttp\Psr7\Request;
20
use Psr\Http\Message\ResponseInterface;
21
use React\Promise\PromiseInterface;
22
use Rx\Observable;
23
use Rx\ObservableInterface;
24
use Rx\Observer\CallbackObserver;
25
use Rx\ObserverInterface;
26
use Rx\React\Promise;
27
use Rx\SchedulerInterface;
28
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
29
use function React\Promise\reject;
30
use function React\Promise\resolve;
31
32
class Repository extends BaseRepository
33
{
34 View Code Duplication
    public function builds(): Observable
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...
35
    {
36
        return Promise::toObservable(
37
            $this->handleCommand(new SimpleRequestCommand('repos/' . $this->slug() . '/builds'))
38
        )->flatMap(function (ResponseInterface $response) {
39
            return Observable::fromArray($response->getBody()->getJson()['builds']);
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...
40
        })->flatMap(function (array $build) {
41
            return Promise::toObservable($this->handleCommand(new HydrateCommand('Build', $build)));
42
        });
43
    }
44
45
    public function jobs(int $buildId): Observable
46
    {
47
        return Promise::toObservable($this->build($buildId))->flatMap(function (Build $build) {
0 ignored issues
show
Compatibility introduced by
$this->build($buildId) of type object<React\Promise\PromiseInterface> is not a sub-type of object<React\Promise\CancellablePromiseInterface>. It seems like you assume a child interface of the interface React\Promise\PromiseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48
            return $build->jobs();
49
        });
50
    }
51
52
    /**
53
     * @param int $id
54
     * @return PromiseInterface
55
     */
56 View Code Duplication
    public function build(int $id): 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...
57
    {
58
        return $this->handleCommand(
59
            new SimpleRequestCommand('repos/' . $this->slug() . '/builds/' . $id)
60
        )->then(function (ResponseInterface $response) {
61
            return resolve($this->handleCommand(
62
                new HydrateCommand('Build', $response->getBody()->getJson()['build'])
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...
63
            ));
64
        });
65
    }
66
67
    /**
68
     * @return ObservableInterface
69
     */
70
    public function commits(): ObservableInterface
71
    {
72
        return unwrapObservableFromPromise($this->handleCommand(
73
            new CommitsCommand($this->slug())
74
        ));
75
    }
76
77
    public function events(): Observable
78
    {
79
        return Observable::create(function (
80
            ObserverInterface $observer,
81
            SchedulerInterface $scheduler
0 ignored issues
show
Unused Code introduced by
The parameter $scheduler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
        ) {
83
            $this->handleCommand(
84
                new SharedAppClientCommand(ApiSettings::PUSHER_KEY)
85
            )->then(function ($pusher) use ($observer) {
86
                $pusher->channel('repo-' . $this->id)->filter(function ($message) {
87
                    return in_array($message->event, [
88
                        'build:created',
89
                        'build:started',
90
                        'build:finished',
91
                    ]);
92
                })->map(function ($message) {
93
                    return json_decode($message->data, true);
94
                })->filter(function ($json) {
95
                    return isset($json['repository']);
96
                })->flatMap(function ($json) {
97
                    return Promise::toObservable(
98
                        $this->handleCommand(
99
                            new HydrateCommand('Repository', $json['repository'])
100
                        )
101
                    );
102
                })->subscribe(new CallbackObserver(function ($repository) use ($observer) {
103
                    $observer->onNext($repository);
104
                }));
105
            });
106
        });
107
    }
108
109
    /**
110
     * @return PromiseInterface
111
     */
112
    public function settings(): PromiseInterface
113
    {
114
        return $this->handleCommand(
115
            new SettingsCommand($this->id())
116
        );
117
    }
118
119
    /**
120
     * @return PromiseInterface
121
     */
122
    public function isActive(): PromiseInterface
123
    {
124
        return $this->handleCommand(new SimpleRequestCommand('hooks'))->then(function (ResponseInterface $response) {
125
            $active = false;
126
            foreach ($response->getBody()->getJson()['hooks'] as $hook) {
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...
127
                if ($hook['id'] == $this->id()) {
128
                    $active = (bool)$hook['active'];
129
                    break;
130
                }
131
            }
132
133
            if ($active) {
134
                return resolve($active);
135
            }
136
137
            return reject($active);
138
        });
139
    }
140
141
    /**
142
     * @return PromiseInterface
143
     */
144
    public function enable(): PromiseInterface
145
    {
146
        return $this->setActiveStatus(true);
147
    }
148
149
    /**
150
     * @return PromiseInterface
151
     */
152
    public function disable(): PromiseInterface
153
    {
154
        return $this->setActiveStatus(false);
155
    }
156
157
    /**
158
     * @param bool $status
159
     * @return PromiseInterface
160
     */
161
    protected function setActiveStatus(bool $status)
162
    {
163
        return $this->handleCommand(new RequestCommand(
164
            new Request(
165
                'PUT',
166
                'hooks/' . $this->id(),
167
                [],
168
                new JsonStream([
169
                    'hook' => [
170
                        'active' => $status,
171
                    ],
172
                ])
173
            )
174
        ))->then(function () {
175
            return $this->refresh();
176
        });
177
    }
178
179
    /**
180
     * @return ObservableInterface
181
     */
182
    public function branches(): ObservableInterface
183
    {
184
        return unwrapObservableFromPromise($this->handleCommand(
185
            new BranchesCommand($this->id())
186
        ));
187
    }
188
189
    /**
190
     * @return ObservableInterface
191
     */
192
    public function vars(): ObservableInterface
193
    {
194
        return unwrapObservableFromPromise($this->handleCommand(
195
            new VarsCommand($this->id())
196
        ));
197
    }
198
199
    /**
200
     * @return ObservableInterface
201
     */
202
    public function caches(): ObservableInterface
203
    {
204
        return unwrapObservableFromPromise($this->handleCommand(
205
            new CachesCommand($this->id())
206
        ));
207
    }
208
209
    /**
210
     * @return PromiseInterface
211
     */
212
    public function key(): PromiseInterface
213
    {
214
        return $this->handleCommand(
215
            new RepositoryKeyCommand($this->slug())
216
        );
217
    }
218
219
    public function refresh(): PromiseInterface
220
    {
221
        return $this->handleCommand(
222
            new RepositoryCommand($this->slug)
223
        );
224
    }
225
}
226