NullAdapter::setVisibility()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace League\Flysystem\Adapter;
4
5
use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait;
6
use League\Flysystem\Adapter\Polyfill\StreamedTrait;
7
use League\Flysystem\Config;
8
use League\Flysystem\Util;
9
10
class NullAdapter extends AbstractAdapter
11
{
12
    /**
13
     * Reads a file as a stream.
14
     *
15
     * @param string $path
16
     *
17
     * @return array|false
18
     *
19
     * @see League\Flysystem\ReadInterface::readStream()
20
     */
21 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...
22
    {
23
        if ( ! $data = $this->read($path)) {
24
            return false;
25
        }
26
27
        $stream = fopen('php://temp', 'w+b');
28
        fwrite($stream, $data['contents']);
29
        rewind($stream);
30
        $data['stream'] = $stream;
31
        unset($data['contents']);
32
33
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $data; (boolean) is incompatible with the return type declared by the interface League\Flysystem\ReadInterface::readStream of type array|false.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
34
    }
35
    /**
36
     * Stream fallback delegator.
37
     *
38
     * @param string   $path
39
     * @param resource $resource
40
     * @param Config   $config
41
     * @param string   $fallback
42
     *
43
     * @return mixed fallback result
44
     */
45 View Code Duplication
    protected function stream($path, $resource, Config $config, $fallback)
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...
46
    {
47
        Util::rewindStream($resource);
48
        $contents = stream_get_contents($resource);
49
        $fallbackCall = array($this, $fallback);
50
51
        return call_user_func($fallbackCall, $path, $contents, $config);
52
    }
53
54
    /**
55
     * Write using a stream.
56
     *
57
     * @param string   $path
58
     * @param resource $resource
59
     * @param Config   $config
60
     *
61
     * @return mixed false or file metadata
62
     */
63
    public function writeStream($path, $resource, Config $config)
64
    {
65
        return $this->stream($path, $resource, $config, 'write');
66
    }
67
68
    /**
69
     * Update a file using a stream.
70
     *
71
     * @param string   $path
72
     * @param resource $resource
73
     * @param Config   $config   Config object or visibility setting
74
     *
75
     * @return mixed false of file metadata
76
     */
77
    public function updateStream($path, $resource, Config $config)
78
    {
79
        return $this->stream($path, $resource, $config, 'update');
80
    }
81
82 View Code Duplication
    public function copy($path, $newpath)
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...
83
    {
84
        $response = $this->readStream($path);
85
86
        if ($response === false || ! is_resource($response['stream'])) {
87
            return false;
88
        }
89
90
        $result = $this->writeStream($newpath, $response['stream'], new Config());
91
92
        if ($result !== false && is_resource($response['stream'])) {
93
            fclose($response['stream']);
94
        }
95
96
        return $result !== false;
97
    }
98
99
    /**
100
     * Check whether a file is present.
101
     *
102
     * @param string $path
103
     *
104
     * @return bool
105
     */
106 9
    public function has($path)
107
    {
108 9
        return false;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114 6
    public function write($path, $contents, Config $config)
115
    {
116 6
        $type = 'file';
117 6
        $result = compact('contents', 'type', 'path');
118
119 6
        if ($visibility = $config->get('visibility')) {
120 3
            $result['visibility'] = $visibility;
121 3
        }
122
123 6
        return $result;
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 3
    public function update($path, $contents, Config $config)
130
    {
131 3
        return false;
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137 6
    public function read($path)
138
    {
139 6
        return false;
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145 3
    public function rename($path, $newpath)
146
    {
147 3
        return false;
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 6
    public function delete($path)
154
    {
155 6
        return false;
156
    }
157
158
    /**
159
     * @inheritdoc
160
     */
161 3
    public function listContents($directory = '', $recursive = false)
162
    {
163 3
        return array();
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169 3
    public function getMetadata($path)
170
    {
171 3
        return false;
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177 3
    public function getSize($path)
178
    {
179 3
        return false;
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185 3
    public function getMimetype($path)
186
    {
187 3
        return false;
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193 3
    public function getTimestamp($path)
194
    {
195 3
        return false;
196
    }
197
198
    /**
199
     * @inheritdoc
200
     */
201 3
    public function getVisibility($path)
202
    {
203 3
        return false;
204
    }
205
206
    /**
207
     * @inheritdoc
208
     */
209 3
    public function setVisibility($path, $visibility)
210
    {
211 3
        return compact('visibility');
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217 3
    public function createDir($dirname, Config $config)
218
    {
219 3
        return array('path' => $dirname, 'type' => 'dir');
220
    }
221
222
    /**
223
     * @inheritdoc
224
     */
225 3
    public function deleteDir($dirname)
226
    {
227 3
        return false;
228
    }
229
}
230