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
07:26 queued 10s
created

Repository::commits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

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