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