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
Push — master ( 4880e2...f70d4a )
by
unknown
03:34 queued 10s
created

DropboxAdapter   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 285
Duplicated Lines 21.4 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 2
dl 61
loc 285
rs 8.8
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A write() 0 4 1
A writeStream() 0 4 1
A update() 0 4 1
A updateStream() 0 4 1
A rename() 0 13 2
A copy() 13 13 2
A delete() 0 12 2
A deleteDir() 0 4 1
A createDir() 12 12 2
A has() 0 4 1
A read() 0 12 2
A readStream() 12 12 2
A listContents() 0 16 2
A getMetadata() 12 12 2
A getSize() 0 4 1
A getMimetype() 0 4 1
A getTimestamp() 0 4 1
A getTemporaryLink() 0 4 1
A getThumbnail() 0 4 1
A applyPathPrefix() 0 6 1
A getClient() 0 4 1
A upload() 12 12 2
A normalizeResponse() 0 19 4

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace Spatie\FlysystemDropbox;
4
5
use Exception;
6
use LogicException;
7
use Spatie\Dropbox\Client;
8
use League\Flysystem\Config;
9
use League\Flysystem\Adapter\AbstractAdapter;
10
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
11
use Spatie\Dropbox\Exceptions\BadRequest;
12
13
class DropboxAdapter extends AbstractAdapter
14
{
15
    use NotSupportingVisibilityTrait;
16
17
    /** @var \Spatie\FlysystemDropbox\DropboxClient */
18
    protected $client;
19
20
    public function __construct(Client $client, string $prefix = null)
21
    {
22
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<Spatie\Dropbox\Client> is incompatible with the declared type object<Spatie\FlysystemDropbox\DropboxClient> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
24
        $this->setPathPrefix($prefix);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function write($path, $contents, Config $config)
31
    {
32
        return $this->upload($path, $contents, 'add');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function writeStream($path, $resource, Config $config)
39
    {
40
        return $this->upload($path, $resource, 'add');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function update($path, $contents, Config $config)
47
    {
48
        return $this->upload($path, $contents, 'overwrite');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function updateStream($path, $resource, Config $config)
55
    {
56
        return $this->upload($path, $resource, 'overwrite');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function rename($path, $newPath): bool
63
    {
64
        $path = $this->applyPathPrefix($path);
65
        $newPath = $this->applyPathPrefix($newPath);
66
67
        try {
68
            $this->client->move($path, $newPath);
69
        } catch (BadRequest $e) {
0 ignored issues
show
Bug introduced by
The class Spatie\Dropbox\Exceptions\BadRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
70
            return false;
71
        }
72
73
        return true;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 View Code Duplication
    public function copy($path, $newpath): bool
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...
80
    {
81
        $path = $this->applyPathPrefix($path);
82
        $newpath = $this->applyPathPrefix($newpath);
83
84
        try {
85
            $this->client->copy($path, $newpath);
86
        } catch (BadRequest $e) {
0 ignored issues
show
Bug introduced by
The class Spatie\Dropbox\Exceptions\BadRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
87
            return false;
88
        }
89
90
        return true;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function delete($path): bool
97
    {
98
        $location = $this->applyPathPrefix($path);
99
100
        try {
101
            $this->client->delete($location);
102
        } catch (BadRequest $e) {
0 ignored issues
show
Bug introduced by
The class Spatie\Dropbox\Exceptions\BadRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
103
            return false;
104
        }
105
106
        return true;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function deleteDir($dirname): bool
113
    {
114
        return $this->delete($dirname);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 View Code Duplication
    public function createDir($dirname, Config $config)
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...
121
    {
122
        $path = $this->applyPathPrefix($dirname);
123
124
        try {
125
            $object = $this->client->createFolder($path);
126
        } catch (BadRequest $e) {
0 ignored issues
show
Bug introduced by
The class Spatie\Dropbox\Exceptions\BadRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
127
            return false;
128
        }
129
130
        return $this->normalizeResponse($object);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function has($path)
137
    {
138
        return $this->getMetadata($path);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function read($path)
145
    {
146
        if (! $object = $this->readStream($path)) {
147
            return false;
148
        }
149
150
        $object['contents'] = stream_get_contents($object['stream']);
151
        fclose($object['stream']);
152
        unset($object['stream']);
153
154
        return $object;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 View Code Duplication
    public function readStream($path)
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...
161
    {
162
        $path = $this->applyPathPrefix($path);
163
164
        try {
165
            $stream = $this->client->download($path);
166
        } catch (BadRequest $e) {
0 ignored issues
show
Bug introduced by
The class Spatie\Dropbox\Exceptions\BadRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
167
            return false;
168
        }
169
170
        return compact('stream');
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function listContents($directory = '', $recursive = false): array
177
    {
178
        $location = $this->applyPathPrefix($directory);
179
180
        $result = $this->client->listFolder($location, $recursive);
181
182
        if (! count($result['entries'])) {
183
            return [];
184
        }
185
186
        return array_map(function ($entry) {
187
            $path = $this->removePathPrefix($entry['path_display']);
188
189
            return $this->normalizeResponse($entry, $path);
0 ignored issues
show
Unused Code introduced by
The call to DropboxAdapter::normalizeResponse() has too many arguments starting with $path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
190
        }, $result['entries']);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 View Code Duplication
    public function getMetadata($path)
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...
197
    {
198
        $path = $this->applyPathPrefix($path);
199
200
        try {
201
            $object = $this->client->getMetadata($path);
202
        } catch (BadRequest $e) {
0 ignored issues
show
Bug introduced by
The class Spatie\Dropbox\Exceptions\BadRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
203
            return false;
204
        }
205
206
        return $this->normalizeResponse($object);
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function getSize($path)
213
    {
214
        return $this->getMetadata($path);
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function getMimetype($path)
221
    {
222
        throw new LogicException("The Dropbox API v2 does not support mimetypes. Given path: `{$path}`.");
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function getTimestamp($path)
229
    {
230
        return $this->getMetadata($path);
231
    }
232
233
    public function getTemporaryLink(string $path): string
234
    {
235
        return $this->client->getTemporaryLink($path);
236
    }
237
238
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64')
239
    {
240
        return $this->client->getThumbnail($path, $format, $size);
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246
    public function applyPathPrefix($path): string
247
    {
248
        $path = parent::applyPathPrefix($path);
249
250
        return '/'.ltrim(rtrim($path, '/'), '/');
251
    }
252
253
    public function getClient(): Client
254
    {
255
        return $this->client;
256
    }
257
258
    /**
259
     * @param string $path
260
     * @param resource|string $contents
261
     * @param string $mode
262
     *
263
     * @return array|false file metadata
264
     */
265 View Code Duplication
    protected function upload(string $path, $contents, string $mode)
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...
266
    {
267
        $path = $this->applyPathPrefix($path);
268
269
        try {
270
            $object = $this->client->upload($path, $mode, $contents);
271
        } catch (BadRequest $e) {
0 ignored issues
show
Bug introduced by
The class Spatie\Dropbox\Exceptions\BadRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
272
            return false;
273
        }
274
275
        return $this->normalizeResponse($object);
276
    }
277
278
    protected function normalizeResponse(array $response): array
279
    {
280
        $normalizedPath = ltrim($this->removePathPrefix($response['path_display']), '/');
281
282
        $normalizedResponse = ['path' => $normalizedPath];
283
284
        if (isset($response['server_modified'])) {
285
            $normalizedResponse['timestamp'] = strtotime($response['server_modified']);
286
        }
287
288
        if (isset($response['size'])) {
289
            $normalizedResponse['bytes'] = $response['size'];
290
        }
291
292
        $type = ($response['.tag'] === 'folder' ? 'dir' : 'file');
293
        $normalizedResponse['type'] = $type;
294
295
        return $normalizedResponse;
296
    }
297
}
298