Chain::write()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Brofist\Flysystem\Adapter;
4
5
use League\Flysystem\AdapterInterface;
6
use League\Flysystem\Config;
7
8
/**
9
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
10
 */
11
class Chain implements AdapterInterface
12
{
13
    /**
14
     * @var array[AdapterInterface]
15
     */
16
    private $adapters = [];
17
18 5
    public function __construct(array $adapters = [])
19
    {
20 5
        foreach ($adapters as $adapter) {
21 5
            $this->append($adapter);
22 5
        }
23 5
    }
24
25
    /**
26
     * @return Chain
27
     */
28 5
    public function append(AdapterInterface $adapter)
29
    {
30 5
        $this->adapters[] = $adapter;
31
32 5
        return $this;
33
    }
34
35 2
    public function write($path, $contents, Config $config)
36
    {
37 2
        $results = [];
38
39 2
        foreach ($this->adapters as $adapter) {
40
            /* @var AdapterInterface $adapter */
41 2
            $results[] = $adapter->write($path, $contents, $config);
42 2
        }
43
44 2
        return !in_array(false, $results);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return !in_array(false, $results); (boolean) is incompatible with the return type declared by the interface League\Flysystem\AdapterInterface::write 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...
45
    }
46
47
    /**
48
     * @SuppressWarnings(PHPMD)
49
     */
50
    public function writeStream($path, $resource, Config $config)
51
    {
52
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
53
    }
54
55
    /**
56
     * @SuppressWarnings(PHPMD)
57
     */
58
    public function update($path, $contents, Config $config)
59
    {
60
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
61
    }
62
63
    /**
64
     * @SuppressWarnings(PHPMD)
65
     */
66
    public function updateStream($path, $resource, Config $config)
67
    {
68
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
69
    }
70
71
    /**
72
     * @SuppressWarnings(PHPMD)
73
     */
74
    public function rename($path, $newpath)
75
    {
76
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
77
    }
78
79
    /**
80
     * @SuppressWarnings(PHPMD)
81
     */
82
    public function copy($path, $newpath)
83
    {
84
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
85
    }
86
87
    /**
88
     * @SuppressWarnings(PHPMD)
89
     */
90
    public function delete($path)
91
    {
92
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
93
    }
94
95
    /**
96
     * @SuppressWarnings(PHPMD)
97
     */
98
    public function deleteDir($dirname)
99
    {
100
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
101
    }
102
103
    /**
104
     * @SuppressWarnings(PHPMD)
105
     */
106
    public function createDir($dirname, Config $config)
107
    {
108
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
109
    }
110
111
    /**
112
     * @SuppressWarnings(PHPMD)
113
     */
114
    public function setVisibility($path, $visibility)
115
    {
116
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
117
    }
118
119 2
    public function has($path)
120
    {
121 2
        $results = [];
122
123 2
        foreach ($this->adapters as $adapter) {
124 2
            $results[] = $adapter->has($path);
125 2
        }
126
127 2
        return array_unique($results) === [true];
128
    }
129
130
    /**
131
     * @SuppressWarnings(PHPMD)
132
     */
133
    public function read($path)
134
    {
135
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
136
    }
137
138
    /**
139
     * @SuppressWarnings(PHPMD)
140
     */
141
    public function readStream($path)
142
    {
143
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
144
    }
145
146
    /**
147
     * @SuppressWarnings(PHPMD)
148
     */
149
    public function listContents($directory = '', $recursive = false)
150
    {
151
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
152
    }
153
154
    /**
155
     * @SuppressWarnings(PHPMD)
156
     */
157
    public function getMetadata($path)
158
    {
159
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
160
    }
161
162
    /**
163
     * @SuppressWarnings(PHPMD)
164
     */
165
    public function getSize($path)
166
    {
167
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
168
    }
169
170
    /**
171
     * @SuppressWarnings(PHPMD)
172
     */
173
    public function getMimetype($path)
174
    {
175
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
176
    }
177
178
    /**
179
     * @SuppressWarnings(PHPMD)
180
     */
181
    public function getTimestamp($path)
182
    {
183
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
184
    }
185
186
    /**
187
     * @SuppressWarnings(PHPMD)
188
     */
189
    public function getVisibility($path)
190
    {
191
        throw new \RuntimeException(sprintf('"%s" not implemented', __METHOD__));
192
    }
193
}
194