Filesystem   B
last analyzed

Coupling/Cohesion

Components 1
Dependencies 12

Complexity

Total Complexity 53

Size/Duplication

Total Lines 390
Duplicated Lines 34.62 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 53
lcom 1
cbo 12
dl 135
loc 390
ccs 148
cts 148
cp 1
rs 7.4757
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAdapter() 0 4 1
A has() 0 6 2
A writeStream() 14 14 2
A put() 0 11 3
A putStream() 0 16 4
A readAndDelete() 0 14 2
A update() 9 9 1
A updateStream() 13 13 2
A rename() 9 9 1
A copy() 9 9 1
A delete() 0 7 1
A deleteDir() 0 10 2
A createDir() 7 7 1
A listContents() 0 7 1
A read() 11 11 2
A readStream() 11 11 2
A getMimetype() 11 11 3
A getTimestamp() 11 11 3
A getVisibility() 11 11 3
A write() 8 8 1
A getMetadata() 0 7 1
A get() 0 14 3
A assertPresent() 0 6 3
A assertAbsent() 0 6 3
A getSize() 11 11 3
A setVisibility() 0 7 1

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 Filesystem 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 Filesystem, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace League\Flysystem;
4
5
use InvalidArgumentException;
6
use League\Flysystem\Adapter\CanOverwriteFiles;
7
use League\Flysystem\Plugin\PluggableTrait;
8
use League\Flysystem\Util\ContentListingFormatter;
9
10
/**
11
 * @method array getWithMetadata(string $path, array $metadata)
12
 * @method bool  forceCopy(string $path, string $newpath)
13
 * @method bool  forceRename(string $path, string $newpath)
14
 * @method array listFiles(string $path = '', boolean $recursive = false)
15
 * @method array listPaths(string $path = '', boolean $recursive = false)
16
 * @method array listWith(array $keys = [], $directory = '', $recursive = false)
17
 */
18
class Filesystem implements FilesystemInterface
19
{
20
    use PluggableTrait;
21
    use ConfigAwareTrait;
22
23
    /**
24
     * @var AdapterInterface
25
     */
26
    protected $adapter;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param AdapterInterface $adapter
32
     * @param Config|array     $config
33
     */
34 246
    public function __construct(AdapterInterface $adapter, $config = null)
35
    {
36 246
        $this->adapter = $adapter;
37 246
        $this->setConfig($config);
38 246
    }
39
40
    /**
41
     * Get the Adapter.
42
     *
43
     * @return AdapterInterface adapter
44
     */
45 210
    public function getAdapter()
46
    {
47 210
        return $this->adapter;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 177
    public function has($path)
54
    {
55 177
        $path = Util::normalizePath($path);
56
57 177
        return strlen($path) === 0 ? false : (bool) $this->getAdapter()->has($path);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 15 View Code Duplication
    public function write($path, $contents, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
64
    {
65 15
        $path = Util::normalizePath($path);
66 15
        $this->assertAbsent($path);
67 12
        $config = $this->prepareConfig($config);
68
69 12
        return (bool) $this->getAdapter()->write($path, $contents, $config);
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 15 View Code Duplication
    public function writeStream($path, $resource, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
76
    {
77 15
        if ( ! is_resource($resource)) {
78 6
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
79
        }
80
81 9
        $path = Util::normalizePath($path);
82 9
        $this->assertAbsent($path);
83 9
        $config = $this->prepareConfig($config);
84
85 9
        Util::rewindStream($resource);
86
87 9
        return (bool) $this->getAdapter()->writeStream($path, $resource, $config);
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 12
    public function put($path, $contents, array $config = [])
94
    {
95 12
        $path = Util::normalizePath($path);
96 12
        $config = $this->prepareConfig($config);
97
98 12
        if ( ! $this->getAdapter() instanceof CanOverwriteFiles && $this->has($path)) {
99 6
            return (bool) $this->getAdapter()->update($path, $contents, $config);
100
        }
101
102 9
        return (bool) $this->getAdapter()->write($path, $contents, $config);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 15
    public function putStream($path, $resource, array $config = [])
109
    {
110 15
        if ( ! is_resource($resource)) {
111 3
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
112
        }
113
114 12
        $path = Util::normalizePath($path);
115 12
        $config = $this->prepareConfig($config);
116 12
        Util::rewindStream($resource);
117
118 12
        if ( ! $this->getAdapter() instanceof CanOverwriteFiles &&$this->has($path)) {
119 6
            return (bool) $this->getAdapter()->updateStream($path, $resource, $config);
120
        }
121
122 9
        return (bool) $this->getAdapter()->writeStream($path, $resource, $config);
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 6
    public function readAndDelete($path)
129
    {
130 6
        $path = Util::normalizePath($path);
131 6
        $this->assertPresent($path);
132 6
        $contents = $this->read($path);
133
134 6
        if ($contents === false) {
135 3
            return false;
136
        }
137
138 3
        $this->delete($path);
139
140 3
        return $contents;
141
    }
142
143
    /**
144
     * @inheritdoc
145
     */
146 6 View Code Duplication
    public function update($path, $contents, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
147
    {
148 6
        $path = Util::normalizePath($path);
149 6
        $config = $this->prepareConfig($config);
150
151 6
        $this->assertPresent($path);
152
153 6
        return (bool) $this->getAdapter()->update($path, $contents, $config);
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159 15 View Code Duplication
    public function updateStream($path, $resource, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
160
    {
161 15
        if ( ! is_resource($resource)) {
162 6
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
163
        }
164
165 9
        $path = Util::normalizePath($path);
166 9
        $config = $this->prepareConfig($config);
167 9
        $this->assertPresent($path);
168 9
        Util::rewindStream($resource);
169
170 9
        return (bool) $this->getAdapter()->updateStream($path, $resource, $config);
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176 36 View Code Duplication
    public function read($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
177
    {
178 36
        $path = Util::normalizePath($path);
179 36
        $this->assertPresent($path);
180
181 30
        if ( ! ($object = $this->getAdapter()->read($path))) {
182 3
            return false;
183
        }
184
185 27
        return $object['contents'];
186
    }
187
188
    /**
189
     * @inheritdoc
190
     */
191 12 View Code Duplication
    public function readStream($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
192
    {
193 12
        $path = Util::normalizePath($path);
194 12
        $this->assertPresent($path);
195
196 12
        if ( ! $object = $this->getAdapter()->readStream($path)) {
197 6
            return false;
198
        }
199
200 9
        return $object['stream'];
201
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206 9 View Code Duplication
    public function rename($path, $newpath)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
207
    {
208 9
        $path = Util::normalizePath($path);
209 9
        $newpath = Util::normalizePath($newpath);
210 9
        $this->assertPresent($path);
211 9
        $this->assertAbsent($newpath);
212
213 9
        return (bool) $this->getAdapter()->rename($path, $newpath);
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219 12 View Code Duplication
    public function copy($path, $newpath)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
220
    {
221 12
        $path = Util::normalizePath($path);
222 12
        $newpath = Util::normalizePath($newpath);
223 12
        $this->assertPresent($path);
224 12
        $this->assertAbsent($newpath);
225
226 12
        return $this->getAdapter()->copy($path, $newpath);
227
    }
228
229
    /**
230
     * @inheritdoc
231
     */
232 60
    public function delete($path)
233
    {
234 60
        $path = Util::normalizePath($path);
235 60
        $this->assertPresent($path);
236
237 57
        return $this->getAdapter()->delete($path);
238
    }
239
240
    /**
241
     * @inheritdoc
242
     */
243 63
    public function deleteDir($dirname)
244
    {
245 63
        $dirname = Util::normalizePath($dirname);
246
247 63
        if ($dirname === '') {
248 3
            throw new RootViolationException('Root directories can not be deleted.');
249
        }
250
251 60
        return (bool) $this->getAdapter()->deleteDir($dirname);
252
    }
253
254
    /**
255
     * @inheritdoc
256
     */
257 3 View Code Duplication
    public function createDir($dirname, array $config = [])
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
258
    {
259 3
        $dirname = Util::normalizePath($dirname);
260 3
        $config = $this->prepareConfig($config);
261
262 3
        return (bool) $this->getAdapter()->createDir($dirname, $config);
263
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268 12
    public function listContents($directory = '', $recursive = false)
269
    {
270 12
        $directory = Util::normalizePath($directory);
271 12
        $contents = $this->getAdapter()->listContents($directory, $recursive);
272
273 12
        return (new ContentListingFormatter($directory, $recursive))->formatListing($contents);
274
    }
275
276
    /**
277
     * @inheritdoc
278
     */
279 9 View Code Duplication
    public function getMimetype($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
280
    {
281 9
        $path = Util::normalizePath($path);
282 9
        $this->assertPresent($path);
283
284 9
        if (( ! $object = $this->getAdapter()->getMimetype($path)) || ! array_key_exists('mimetype', $object)) {
285 3
            return false;
286
        }
287
288 6
        return $object['mimetype'];
289
    }
290
291
    /**
292
     * @inheritdoc
293
     */
294 9 View Code Duplication
    public function getTimestamp($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
295
    {
296 9
        $path = Util::normalizePath($path);
297 9
        $this->assertPresent($path);
298
299 9
        if (( ! $object = $this->getAdapter()->getTimestamp($path)) || ! array_key_exists('timestamp', $object)) {
300 3
            return false;
301
        }
302
303 6
        return $object['timestamp'];
304
    }
305
306
    /**
307
     * @inheritdoc
308
     */
309 9 View Code Duplication
    public function getVisibility($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
310
    {
311 9
        $path = Util::normalizePath($path);
312 9
        $this->assertPresent($path);
313
314 9
        if (( ! $object = $this->getAdapter()->getVisibility($path)) || ! array_key_exists('visibility', $object)) {
315 3
            return false;
316
        }
317
318 6
        return $object['visibility'];
319
    }
320
321
    /**
322
     * @inheritdoc
323
     */
324 12 View Code Duplication
    public function getSize($path)
0 ignored issues
show
Duplication introduced by Frank de Jonge
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...
325
    {
326 12
        $path = Util::normalizePath($path);
327 12
        $this->assertPresent($path);
328
329 9
        if (( ! $object = $this->getAdapter()->getSize($path)) || ! array_key_exists('size', $object)) {
330 3
            return false;
331
        }
332
333 6
        return (int) $object['size'];
334
    }
335
336
    /**
337
     * @inheritdoc
338
     */
339 9
    public function setVisibility($path, $visibility)
340
    {
341 9
        $path = Util::normalizePath($path);
342 9
        $this->assertPresent($path);
343
344 6
        return (bool) $this->getAdapter()->setVisibility($path, $visibility);
345
    }
346
347
    /**
348
     * @inheritdoc
349
     */
350 51
    public function getMetadata($path)
351
    {
352 51
        $path = Util::normalizePath($path);
353 51
        $this->assertPresent($path);
354
355 51
        return $this->getAdapter()->getMetadata($path);
356
    }
357
358
    /**
359
     * @inheritdoc
360
     */
361 69
    public function get($path, Handler $handler = null)
362
    {
363 69
        $path = Util::normalizePath($path);
364
365 69
        if ( ! $handler) {
366 45
            $metadata = $this->getMetadata($path);
367 45
            $handler = $metadata['type'] === 'file' ? new File($this, $path) : new Directory($this, $path);
0 ignored issues
show
Deprecated Code introduced by Frank de Jonge
The class League\Flysystem\File has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
Deprecated Code introduced by Frank de Jonge
The class League\Flysystem\Directory has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
368 15
        }
369
370 69
        $handler->setPath($path);
371 69
        $handler->setFilesystem($this);
372
373 69
        return $handler;
374
    }
375
376
    /**
377
     * Assert a file is present.
378
     *
379
     * @param string $path path to file
380
     *
381
     * @throws FileNotFoundException
382
     *
383
     * @return void
384
     */
385 147
    public function assertPresent($path)
386
    {
387 147
        if ($this->config->get('disable_asserts', false) === false && ! $this->has($path)) {
388 18
            throw new FileNotFoundException($path);
389
        }
390 135
    }
391
392
    /**
393
     * Assert a file is absent.
394
     *
395
     * @param string $path path to file
396
     *
397
     * @throws FileExistsException
398
     *
399
     * @return void
400
     */
401 45
    public function assertAbsent($path)
402
    {
403 45
        if ($this->config->get('disable_asserts', false) === false && $this->has($path)) {
404 3
            throw new FileExistsException($path);
405
        }
406 42
    }
407
}
408