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 (#21)
by
unknown
01:20
created

Client   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 544
Duplicated Lines 8.46 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 46
lcom 1
cbo 6
dl 46
loc 544
rs 8.3999
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A copy() 9 9 1
A createFolder() 0 12 1
A createSharedLinkWithSettings() 9 9 1
A listSharedLinks() 0 12 2
A delete() 0 8 1
A download() 0 10 1
A getMetadata() 0 8 1
A getTemporaryLink() 10 10 1
A getThumbnail() 0 12 1
A listFolder() 9 9 1
A listFolderContinue() 0 4 1
A move() 9 9 1
A getMaxChunkSize() 0 4 1
A shouldUploadChunked() 0 6 3
A upload() 0 19 2
B uploadChunked() 0 29 6
A uploadSessionStart() 0 11 1
A uploadSessionAppend() 0 10 1
A uploadSessionFinish() 0 13 2
A readChunk() 0 14 4
A getAccountInfo() 0 4 1
A revokeToken() 0 4 1
A normalizePath() 0 6 2
A contentEndpointRequest() 0 19 3
A rpcEndpointRequest() 0 18 3
A determineException() 0 8 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Spatie\Dropbox;
4
5
use Exception;
6
use GuzzleHttp\Psr7\StreamWrapper;
7
use GuzzleHttp\Client as GuzzleClient;
8
use Psr\Http\Message\ResponseInterface;
9
use GuzzleHttp\Exception\ClientException;
10
use Spatie\Dropbox\Exceptions\BadRequest;
11
12
class Client
13
{
14
    const THUMBNAIL_FORMAT_JPEG = 'jpeg';
15
    const THUMBNAIL_FORMAT_PNG = 'png';
16
17
    const THUMBNAIL_SIZE_XS = 'w32h32';
18
    const THUMBNAIL_SIZE_S = 'w64h64';
19
    const THUMBNAIL_SIZE_M = 'w128h128';
20
    const THUMBNAIL_SIZE_L = 'w640h480';
21
    const THUMBNAIL_SIZE_XL = 'w1024h768';
22
23
    const MAX_CHUNK_SIZE = 157286400;
24
25
    /** @var string */
26
    protected $accessToken;
27
28
    /** @var \GuzzleHttp\Client */
29
    protected $client;
30
31
    public function __construct(string $accessToken, GuzzleClient $client = null)
32
    {
33
        $this->accessToken = $accessToken;
34
35
        $this->client = $client ?? new GuzzleClient([
36
                'headers' => [
37
                    'Authorization' => "Bearer {$this->accessToken}",
38
                ],
39
            ]);
40
    }
41
42
    /**
43
     * Copy a file or folder to a different location in the user's Dropbox.
44
     *
45
     * If the source path is a folder all its contents will be copied.
46
     *
47
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy
48
     */
49 View Code Duplication
    public function copy(string $fromPath, string $toPath): array
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...
50
    {
51
        $parameters = [
52
            'from_path' => $this->normalizePath($fromPath),
53
            'to_path' => $this->normalizePath($toPath),
54
        ];
55
56
        return $this->rpcEndpointRequest('files/copy', $parameters);
57
    }
58
59
    /**
60
     * Create a folder at a given path.
61
     *
62
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder
63
     */
64
    public function createFolder(string $path): array
65
    {
66
        $parameters = [
67
            'path' => $this->normalizePath($path),
68
        ];
69
70
        $object = $this->rpcEndpointRequest('files/create_folder', $parameters);
71
72
        $object['.tag'] = 'folder';
73
74
        return $object;
75
    }
76
77
    /**
78
     * Create a shared link with custom settings.
79
     *
80
     * If no settings are given then the default visibility is RequestedVisibility.public.
81
     * The resolved visibility, though, may depend on other aspects such as team and
82
     * shared folder settings). Only for paid users.
83
     *
84
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings
85
     */
86 View Code Duplication
    public function createSharedLinkWithSettings(string $path, array $settings = [])
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...
87
    {
88
        $parameters = [
89
            'path' => $this->normalizePath($path),
90
            'settings' => $settings,
91
        ];
92
93
        return $this->rpcEndpointRequest('sharing/create_shared_link_with_settings', $parameters);
94
    }
95
96
    /**
97
     * List shared links.
98
     *
99
     * For empty path returns a list of all shared links. For non-empty path
100
     * returns a list of all shared links with access to the given path.
101
     *
102
     * If direct_only is set true, only direct links to the path will be returned, otherwise
103
     * it may return link to the path itself and parent folders as described on docs.
104
     *
105
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links
106
     */
107
    public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array
108
    {
109
        $parameters = [
110
            'path' => $path ? $this->normalizePath($path) : null,
111
            'cursor' => $cursor,
112
            'direct_only' => $direct_only,
113
        ];
114
115
        $body = $this->rpcEndpointRequest('sharing/list_shared_links', $parameters);
116
117
        return $body['links'];
118
    }
119
120
    /**
121
     * Delete the file or folder at a given path.
122
     *
123
     * If the path is a folder, all its contents will be deleted too.
124
     * A successful response indicates that the file or folder was deleted.
125
     *
126
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete
127
     */
128
    public function delete(string $path): array
129
    {
130
        $parameters = [
131
            'path' => $this->normalizePath($path),
132
        ];
133
134
        return $this->rpcEndpointRequest('files/delete', $parameters);
135
    }
136
137
    /**
138
     * Download a file from a user's Dropbox.
139
     *
140
     * @param string $path
141
     *
142
     * @return resource
143
     *
144
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download
145
     */
146
    public function download(string $path)
147
    {
148
        $arguments = [
149
            'path' => $this->normalizePath($path),
150
        ];
151
152
        $response = $this->contentEndpointRequest('files/download', $arguments);
153
154
        return StreamWrapper::getResource($response->getBody());
155
    }
156
157
    /**
158
     * Returns the metadata for a file or folder.
159
     *
160
     * Note: Metadata for the root folder is unsupported.
161
     *
162
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
163
     */
164
    public function getMetadata(string $path): array
165
    {
166
        $parameters = [
167
            'path' => $this->normalizePath($path),
168
        ];
169
170
        return $this->rpcEndpointRequest('files/get_metadata', $parameters);
171
    }
172
173
    /**
174
     * Get a temporary link to stream content of a file.
175
     *
176
     * This link will expire in four hours and afterwards you will get 410 Gone.
177
     * Content-Type of the link is determined automatically by the file's mime type.
178
     *
179
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
180
     */
181 View Code Duplication
    public function getTemporaryLink(string $path): string
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...
182
    {
183
        $parameters = [
184
            'path' => $this->normalizePath($path),
185
        ];
186
187
        $body = $this->rpcEndpointRequest('files/get_temporary_link', $parameters);
188
189
        return $body['link'];
190
    }
191
192
    /**
193
     * Get a thumbnail for an image.
194
     *
195
     * This method currently supports files with the following file extensions:
196
     * jpg, jpeg, png, tiff, tif, gif and bmp.
197
     *
198
     * Photos that are larger than 20MB in size won't be converted to a thumbnail.
199
     *
200
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
201
     */
202
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string
203
    {
204
        $arguments = [
205
            'path' => $this->normalizePath($path),
206
            'format' => $format,
207
            'size' => $size,
208
        ];
209
210
        $response = $this->contentEndpointRequest('files/get_thumbnail', $arguments);
211
212
        return (string) $response->getBody();
213
    }
214
215
    /**
216
     * Starts returning the contents of a folder.
217
     *
218
     * If the result's ListFolderResult.has_more field is true, call
219
     * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries.
220
     *
221
     * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls
222
     * with same parameters are made simultaneously by same API app for same user. If your app implements
223
     * retry logic, please hold off the retry until the previous request finishes.
224
     *
225
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
226
     */
227 View Code Duplication
    public function listFolder(string $path = '', bool $recursive = false): array
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...
228
    {
229
        $parameters = [
230
            'path' => $this->normalizePath($path),
231
            'recursive' => $recursive,
232
        ];
233
234
        return $this->rpcEndpointRequest('files/list_folder', $parameters);
235
    }
236
237
    /**
238
     * Once a cursor has been retrieved from list_folder, use this to paginate through all files and
239
     * retrieve updates to the folder, following the same rules as documented for list_folder.
240
     *
241
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue
242
     */
243
    public function listFolderContinue(string $cursor = ''): array
244
    {
245
        return $this->rpcEndpointRequest('files/list_folder/continue', compact('cursor'));
246
    }
247
248
    /**
249
     * Move a file or folder to a different location in the user's Dropbox.
250
     *
251
     * If the source path is a folder all its contents will be moved.
252
     *
253
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move
254
     */
255 View Code Duplication
    public function move(string $fromPath, string $toPath): array
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...
256
    {
257
        $parameters = [
258
            'from_path' => $this->normalizePath($fromPath),
259
            'to_path' => $this->normalizePath($toPath),
260
        ];
261
262
        return $this->rpcEndpointRequest('files/move', $parameters);
263
    }
264
265
    /**
266
     * Get max chunk size that can be sent to dropbox api.
267
     *
268
     * @return int
269
     */
270
    public function getMaxChunkSize(): int
271
    {
272
        return static::MAX_CHUNK_SIZE;
273
    }
274
275
    /**
276
     * The file should be uploaded in chunks if it size exceeds the 150 MB threshold
277
     * or if the resource size could not be determined (eg. a popen() stream).
278
     *
279
     * @param string|resource $contents
280
     *
281
     * @return bool
282
     */
283
    protected function shouldUploadChunked($contents): bool
284
    {
285
        $size = is_string($contents) ? strlen($contents) : fstat($contents)['size'];
286
287
        return $size === null || $size > $this->getMaxChunkSize();
288
    }
289
290
    /**
291
     * Create a new file with the contents provided in the request.
292
     *
293
     * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start.
294
     *
295
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
296
     *
297
     * @param string $path
298
     * @param string|resource $contents
299
     * @param string|array $mode
300
     *
301
     * @return array
302
     */
303
    public function upload(string $path, $contents, $mode = 'add'): array
304
    {
305
        if ($this->shouldUploadChunked($contents)) {
306
            return $this->uploadChunked($path, $contents, $mode);
0 ignored issues
show
Bug introduced by
It seems like $mode defined by parameter $mode on line 303 can also be of type array; however, Spatie\Dropbox\Client::uploadChunked() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
307
        }
308
309
        $arguments = [
310
            'path' => $this->normalizePath($path),
311
            'mode' => $mode,
312
        ];
313
314
        $response = $this->contentEndpointRequest('files/upload', $arguments, $contents);
315
316
        $metadata = json_decode($response->getBody(), true);
317
318
        $metadata['.tag'] = 'file';
319
320
        return $metadata;
321
    }
322
323
    /**
324
     * Upload file split in chunks. This allows uploading large files, since
325
     * Dropbox API v2 limits the content size to 150MB.
326
     *
327
     * The chunk size will affect directly the memory usage, so be careful.
328
     * Large chunks tends to speed up the upload, while smaller optimizes memory usage.
329
     *
330
     * @param string          $path
331
     * @param string|resource $contents
332
     * @param string          $mode
333
     * @param int             $chunkSize
334
     *
335
     * @return array
336
     */
337
    public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array
338
    {
339
        $chunkSize = $chunkSize ?? $this->getMaxChunkSize();
340
        $stream = $contents;
341
342
        // This method relies on resources, so we need to convert strings to resource
343
        if (is_string($contents)) {
344
            $stream = fopen('php://memory', 'r+');
345
            fwrite($stream, $contents);
346
            rewind($stream);
347
        }
348
349
        $data = self::readChunk($stream, $chunkSize);
0 ignored issues
show
Bug introduced by
It seems like $stream defined by $contents on line 340 can also be of type string; however, Spatie\Dropbox\Client::readChunk() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
350
        $cursor = null;
351
352
        while (! ((strlen($data) < $chunkSize) || feof($stream))) {
353
            // Start upload session on first iteration, then just append on subsequent iterations
354
            $cursor = isset($cursor) ? $this->uploadSessionAppend($data, $cursor) : $this->uploadSessionStart($data);
355
            $data = self::readChunk($stream, $chunkSize);
0 ignored issues
show
Bug introduced by
It seems like $stream defined by $contents on line 340 can also be of type string; however, Spatie\Dropbox\Client::readChunk() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
356
        }
357
358
        // If there's no cursor here, our stream is small enough to a single request
359
        if (! isset($cursor)) {
360
            $cursor = $this->uploadSessionStart($data);
361
            $data = '';
362
        }
363
364
        return $this->uploadSessionFinish($data, $cursor, $path, $mode);
365
    }
366
367
    /**
368
     * Upload sessions allow you to upload a single file in one or more requests,
369
     * for example where the size of the file is greater than 150 MB.
370
     * This call starts a new upload session with the given data.
371
     *
372
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
373
     *
374
     * @param string $contents
375
     * @param bool   $close
376
     *
377
     * @return UploadSessionCursor
378
     */
379
    public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor
380
    {
381
        $arguments = compact('close');
382
383
        $response = json_decode(
384
            $this->contentEndpointRequest('files/upload_session/start', $arguments, $contents)->getBody(),
385
            true
386
        );
387
388
        return new UploadSessionCursor($response['session_id'], strlen($contents));
389
    }
390
391
    /**
392
     * Append more data to an upload session.
393
     * When the parameter close is set, this call will close the session.
394
     * A single request should not upload more than 150 MB.
395
     *
396
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
397
     *
398
     * @param string              $contents
399
     * @param UploadSessionCursor $cursor
400
     * @param bool                $close
401
     *
402
     * @return \Spatie\Dropbox\UploadSessionCursor
403
     */
404
    public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor
405
    {
406
        $arguments = compact('cursor', 'close');
407
408
        $this->contentEndpointRequest('files/upload_session/append_v2', $arguments, $contents);
409
410
        $cursor->offset += strlen($contents);
411
412
        return $cursor;
413
    }
414
415
    /**
416
     * Finish an upload session and save the uploaded data to the given file path.
417
     * A single request should not upload more than 150 MB.
418
     *
419
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
420
     *
421
     * @param string                              $contents
422
     * @param \Spatie\Dropbox\UploadSessionCursor $cursor
423
     * @param string                              $path
424
     * @param string|array                        $mode
425
     * @param bool                                $autorename
426
     * @param bool                                $mute
427
     *
428
     * @return array
429
     */
430
    public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array
431
    {
432
        $arguments = compact('cursor');
433
        $arguments['commit'] = compact('path', 'mode', 'autorename', 'mute');
434
435
        $response = $this->contentEndpointRequest(
436
            'files/upload_session/finish',
437
            $arguments,
438
            ($contents == '') ? null : $contents
439
        );
440
441
        return json_decode($response->getBody(), true);
442
    }
443
444
    /**
445
     * Sometimes fread() returns less than the request number of bytes (for example, when reading
446
     * from network streams).  This function repeatedly calls fread until the requested number of
447
     * bytes have been read or we've reached EOF.
448
     *
449
     * @param resource $stream
450
     * @param int      $chunkSize
451
     *
452
     * @throws Exception
453
     * @return string
454
     */
455
    protected static function readChunk($stream, int $chunkSize)
456
    {
457
        $chunk = '';
458
        while (! feof($stream) && $chunkSize > 0) {
459
            $part = fread($stream, $chunkSize);
460
            if ($part === false) {
461
                throw new Exception('Error reading from $stream.');
462
            }
463
            $chunk .= $part;
464
            $chunkSize -= strlen($part);
465
        }
466
467
        return $chunk;
468
    }
469
470
    /**
471
     * Get Account Info for current authenticated user.
472
     *
473
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account
474
     *
475
     * @return array
476
     */
477
    public function getAccountInfo(): array
478
    {
479
        return $this->rpcEndpointRequest('users/get_current_account');
480
    }
481
482
    /**
483
     * Revoke current access token.
484
     *
485
     * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke
486
     */
487
    public function revokeToken()
488
    {
489
        $this->rpcEndpointRequest('auth/token/revoke');
490
    }
491
492
    protected function normalizePath(string $path): string
493
    {
494
        $path = trim($path, '/');
495
496
        return ($path === '') ? '' : '/'.$path;
497
    }
498
499
    /**
500
     * @param string $endpoint
501
     * @param array $arguments
502
     * @param string|resource $body
503
     *
504
     * @return \Psr\Http\Message\ResponseInterface
505
     *
506
     * @throws \Exception
507
     */
508
    public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface
509
    {
510
        $headers['Dropbox-API-Arg'] = json_encode($arguments);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
511
512
        if ($body !== '') {
513
            $headers['Content-Type'] = 'application/octet-stream';
514
        }
515
516
        try {
517
            $response = $this->client->post("https://content.dropboxapi.com/2/{$endpoint}", [
518
                'headers' => $headers,
519
                'body' => $body,
520
            ]);
521
        } catch (ClientException $exception) {
522
            throw $this->determineException($exception);
523
        }
524
525
        return $response;
526
    }
527
528
    public function rpcEndpointRequest(string $endpoint, array $parameters = null): array
529
    {
530
        try {
531
            $options = [];
532
533
            if ($parameters) {
534
                $options['json'] = $parameters;
535
            }
536
537
            $response = $this->client->post("https://api.dropboxapi.com/2/{$endpoint}", $options);
538
        } catch (ClientException $exception) {
539
            throw $this->determineException($exception);
540
        }
541
542
        $response = json_decode($response->getBody(), true);
543
544
        return $response ?? [];
545
    }
546
547
    protected function determineException(ClientException $exception): Exception
548
    {
549
        if (in_array($exception->getResponse()->getStatusCode(), [400, 409])) {
550
            return new BadRequest($exception->getResponse());
0 ignored issues
show
Bug introduced by
It seems like $exception->getResponse() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
551
        }
552
553
        return $exception;
554
    }
555
}
556