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