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:25
created

Repository::isActive()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
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\BuildCommand;
9
use ApiClients\Client\Travis\CommandBus\Command\BuildsCommand;
10
use ApiClients\Client\Travis\CommandBus\Command\CachesCommand;
11
use ApiClients\Client\Travis\CommandBus\Command\CommitsCommand;
12
use ApiClients\Client\Travis\CommandBus\Command\RepositoryCommand;
13
use ApiClients\Client\Travis\CommandBus\Command\RepositoryKeyCommand;
14
use ApiClients\Client\Travis\CommandBus\Command\SettingsCommand;
15
use ApiClients\Client\Travis\CommandBus\Command\VarsCommand;
16
use ApiClients\Client\Travis\Resource\Repository as BaseRepository;
17
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
18
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
19
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand;
20
use ApiClients\Foundation\Transport\JsonStream;
21
use GuzzleHttp\Psr7\Request;
22
use Psr\Http\Message\ResponseInterface;
23
use React\Promise\PromiseInterface;
24
use Rx\Observable;
25
use Rx\ObservableInterface;
26
use Rx\Observer\CallbackObserver;
27
use Rx\ObserverInterface;
28
use Rx\React\Promise;
29
use Rx\SchedulerInterface;
30
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
31
use function React\Promise\reject;
32
use function React\Promise\resolve;
33
34
class Repository extends BaseRepository
35
{
36
    public function builds(): Observable
37
    {
38
        return unwrapObservableFromPromise($this->handleCommand(
39
            new BuildsCommand($this->slug())
40
        ));
41
    }
42
43
    public function jobs(int $buildId): Observable
44
    {
45
        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...
46
            return $build->jobs();
47
        });
48
    }
49
50
    /**
51
     * @param int $id
52
     * @return PromiseInterface
53
     */
54
    public function build(int $id): PromiseInterface
55
    {
56
        return $this->handleCommand(new BuildCommand($id));
57
    }
58
59
    /**
60
     * @return ObservableInterface
61
     */
62
    public function commits(): ObservableInterface
63
    {
64
        return unwrapObservableFromPromise($this->handleCommand(
65
            new CommitsCommand($this->slug())
66
        ));
67
    }
68
69
    public function events(): Observable
70
    {
71
        return Observable::create(function (
72
            ObserverInterface $observer,
73
            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...
74
        ) {
75
            $this->handleCommand(
76
                new SharedAppClientCommand(ApiSettings::PUSHER_KEY)
77
            )->then(function ($pusher) use ($observer) {
78
                $pusher->channel('repo-' . $this->id)->filter(function ($message) {
79
                    return in_array($message->event, [
80
                        'build:created',
81
                        'build:started',
82
                        'build:finished',
83
                    ]);
84
                })->map(function ($message) {
85
                    return json_decode($message->data, true);
86
                })->filter(function ($json) {
87
                    return isset($json['repository']);
88
                })->flatMap(function ($json) {
89
                    return Promise::toObservable(
90
                        $this->handleCommand(
91
                            new HydrateCommand('Repository', $json['repository'])
92
                        )
93
                    );
94
                })->subscribe(new CallbackObserver(function ($repository) use ($observer) {
95
                    $observer->onNext($repository);
96
                }));
97
            });
98
        });
99
    }
100
101
    /**
102
     * @return PromiseInterface
103
     */
104
    public function settings(): PromiseInterface
105
    {
106
        return $this->handleCommand(
107
            new SettingsCommand($this->id())
108
        );
109
    }
110
111
    /**
112
     * @return PromiseInterface
113
     */
114
    public function isActive(): PromiseInterface
115
    {
116
        return $this->handleCommand(new SimpleRequestCommand('hooks'))->then(function (ResponseInterface $response) {
117
            $active = false;
118
            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...
119
                if ($hook['id'] == $this->id()) {
120
                    $active = (bool)$hook['active'];
121
                    break;
122
                }
123
            }
124
125
            if ($active) {
126
                return resolve($active);
127
            }
128
129
            return reject($active);
130
        });
131
    }
132
133
    /**
134
     * @return PromiseInterface
135
     */
136
    public function enable(): PromiseInterface
137
    {
138
        return $this->setActiveStatus(true);
139
    }
140
141
    /**
142
     * @return PromiseInterface
143
     */
144
    public function disable(): PromiseInterface
145
    {
146
        return $this->setActiveStatus(false);
147
    }
148
149
    /**
150
     * @param bool $status
151
     * @return PromiseInterface
152
     */
153
    protected function setActiveStatus(bool $status)
154
    {
155
        return $this->handleCommand(new RequestCommand(
156
            new Request(
157
                'PUT',
158
                'hooks/' . $this->id(),
159
                [],
160
                new JsonStream([
161
                    'hook' => [
162
                        'active' => $status,
163
                    ],
164
                ])
165
            )
166
        ))->then(function () {
167
            return $this->refresh();
168
        });
169
    }
170
171
    /**
172
     * @return ObservableInterface
173
     */
174
    public function branches(): ObservableInterface
175
    {
176
        return unwrapObservableFromPromise($this->handleCommand(
177
            new BranchesCommand($this->id())
178
        ));
179
    }
180
181
    /**
182
     * @return ObservableInterface
183
     */
184
    public function vars(): ObservableInterface
185
    {
186
        return unwrapObservableFromPromise($this->handleCommand(
187
            new VarsCommand($this->id())
188
        ));
189
    }
190
191
    /**
192
     * @return ObservableInterface
193
     */
194
    public function caches(): ObservableInterface
195
    {
196
        return unwrapObservableFromPromise($this->handleCommand(
197
            new CachesCommand($this->id())
198
        ));
199
    }
200
201
    /**
202
     * @return PromiseInterface
203
     */
204
    public function key(): PromiseInterface
205
    {
206
        return $this->handleCommand(
207
            new RepositoryKeyCommand($this->slug())
208
        );
209
    }
210
211
    public function refresh(): PromiseInterface
212
    {
213
        return $this->handleCommand(
214
            new RepositoryCommand($this->slug)
215
        );
216
    }
217
}
218