Filesystem::exists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace IQParts\Content;
4
5
use IQParts\Content\Object\File;
6
use IQParts\Content\Object\Folder;
7
use League\Flysystem\AdapterInterface;
8
use League\Flysystem\Config;
9
use League\Flysystem\Directory;
10
use League\Flysystem\FileNotFoundException;
11
use League\Flysystem\Filesystem as FlySystem;
12
use League\Flysystem\Plugin\ListPaths;
13
14
/**
15
 * Class Filesystem
16
 * @package IQParts\Content
17
 */
18
final class Filesystem
19
{
20
    /**
21
     * @var FlySystem
22
     */
23
    private $flySystem;
24
25
    /**
26
     * Filesystem constructor.
27
     * @param AdapterInterface $adapter
28
     * @param Config $config
29
     */
30
    public function __construct(AdapterInterface $adapter, Config $config = null)
31
    {
32
        if (!$config) $config = [];
33
        $flySystem = new FlySystem($adapter, $config);
34
        $flySystem->addPlugin(new ListPaths());
35
        $this->flySystem = $flySystem;
36
    }
37
38
    /**
39
     * @param string $path
40
     * @return bool
41
     */
42
    public function exists(string $path): bool
43
    {
44
        if ($path === '/') $path = '';
45
        return $this->flySystem->has($path);
46
    }
47
48
    /**
49
     * @param string $path
50
     * @return File|Folder|false
51
     * @throws FileNotFoundException
52
     */
53
    public function get(string $path)
54
    {
55
        /**
56
         * If path = '/' -> create a folder object with all dirs and folders as children
57
         */
58
        if ($path === '/' || $path === '') {
59
            $dirs = [];
60
            $files = [];
61
            $paths = $this->flySystem->listPaths('', false);
62
            foreach ($paths as $item) {
63
                /** @var File|Directory $i */
64
                $i = $this->flySystem->get($item);
65
                if ($i->getType() === 'dir') {
0 ignored issues
show
Bug introduced by
The method getType does only exist in League\Flysystem\Directory, but not in IQParts\Content\Object\File.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
                    $dirs[] = base64_encode($i->getPath());
67
                } else {
68
                    $files[] = base64_encode($i->getPath());
69
                }
70
            }
71
72
            return Folder::fromVariables(base64_encode('/'), '/', '', null, $dirs, $files);
73
        } else {
74
            $item = $this->flySystem->get($path);
75
            if ($item instanceof Directory) {
76
                return new Folder($item);
77
            } else {
78
                return new File($item);
0 ignored issues
show
Documentation introduced by
$item is of type object<League\Flysystem\Handler>, but the function expects a null|object<League\Flysystem\File>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param string $path
85
     * @return bool|false|resource
86
     */
87
    public function getStream(string $path)
88
    {
89
        return $this->flySystem->readStream($path);
90
    }
91
92
    /**
93
     * @param string $path
94
     * @param bool $recursive
95
     * @return \Iterator|Folder[]|File[]
96
     */
97
    public function listDirContents(string $path, bool $recursive = false): \Iterator
98
    {
99
        if ($path === '/') $path = '';
100
        foreach ($this->flySystem->listPaths($path, $recursive) as $path) {
101
            $item = $this->flySystem->get($path);
102
            if ($item instanceof Directory) {
103
                yield new Folder($item);
104
            } else {
105
                yield new File($item);
0 ignored issues
show
Documentation introduced by
$item is of type object<League\Flysystem\Handler>, but the function expects a null|object<League\Flysystem\File>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
            }
107
        }
108
    }
109
110
    /**
111
     * @param string $path
112
     * @param bool $recursive
113
     * @return \Iterator|Folder[]
114
     */
115
    public function listDirs(string $path, bool $recursive = false): \Iterator
116
    {
117
        if ($path === '/') $path = '';
118
        foreach ($this->flySystem->listPaths($path, $recursive) as $path) {
119
            $item = $this->flySystem->get($path);
120
            if ($item instanceof Directory) {
121
                yield new Folder($item);
122
            }
123
        }
124
    }
125
126
    /**
127
     * @param string $path
128
     * @param bool $recursive
129
     * @return \Iterator|File[]
130
     */
131
    public function listFiles(string $path, bool $recursive = false): \Iterator
132
    {
133
        if ($path === '/') $path = '';
134
        foreach ($this->flySystem->listContents($path, $recursive) as $i) {
135
            $item = $this->flySystem->get($i['path']);
136
            if ($item instanceof \League\Flysystem\File) yield new File($item);
137
        }
138
    }
139
140
    /**
141
     * @param string $path
142
     * @return bool
143
     */
144
    public function delete(string $path)
145
    {
146
        return $this->flySystem->delete($path);
147
    }
148
149
    /**
150
     * @param string $path
151
     * @return bool
152
     */
153
    public function deleteDir(string $path)
154
    {
155
        return $this->flySystem->deleteDir($path);
156
    }
157
158
    /**
159
     * @param string $path
160
     * @param array $config
161
     * @return bool
162
     */
163
    public function createDir(string $path, array $config = [])
164
    {
165
        return $this->flySystem->createDir($path, $config);
166
    }
167
168
    /**
169
     * @param string $path
170
     * @param $contents
171
     * @param array $config
172
     * @return bool
173
     */
174
    public function put(string $path, $contents, array $config = []): bool
175
    {
176
        return $this->flySystem->put($path, $contents, $config);
177
    }
178
179
    /**
180
     * @param string $path
181
     * @param resource $contents
182
     * @param array $config
183
     * @return bool
184
     */
185
    public function putStream(string $path, $contents, array $config = []): bool
186
    {
187
        return $this->flySystem->putStream($path, $contents, $config);
188
    }
189
190
    /**
191
     * @return FlySystem
192
     */
193
    public function getFlySystem(): FlySystem
194
    {
195
        return $this->flySystem;
196
    }
197
}