Completed
Push — master ( 8f38ad...4035d6 )
by Joas
13:12 queued 06:02
created

NullStorage::file_exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (c) 2016, Robin Appelman <[email protected]>
5
 *
6
 * This code is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License, version 3,
8
 * as published by the Free Software Foundation.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License, version 3,
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
17
 *
18
 */
19
20
namespace OC\Lockdown\Filesystem;
21
22
use Icewind\Streams\IteratorDirectory;
23
use OC\Files\Storage\Common;
24
25
class NullStorage extends Common {
26
	public function __construct($parameters) {
27
		parent::__construct($parameters);
28
	}
29
30
	public function getId() {
31
		return 'null';
32
	}
33
34
	public function mkdir($path) {
35
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
36
	}
37
38
	public function rmdir($path) {
39
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
40
	}
41
42
	public function opendir($path) {
43
		return new IteratorDirectory([]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Icewind\Stre...atorDirectory(array()); (Icewind\Streams\IteratorDirectory) is incompatible with the return type declared by the interface OCP\Files\Storage::opendir of type resource|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...
44
	}
45
46
	public function is_dir($path) {
47
		return $path === '';
48
	}
49
50
	public function is_file($path) {
51
		return false;
52
	}
53
54
	public function stat($path) {
55
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
56
	}
57
58
	public function filetype($path) {
59
		return ($path === '') ? 'dir' : false;
60
	}
61
62
	public function filesize($path) {
63
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
64
	}
65
66
	public function isCreatable($path) {
67
		return false;
68
	}
69
70
	public function isReadable($path) {
71
		return $path === '';
72
	}
73
74
	public function isUpdatable($path) {
75
		return false;
76
	}
77
78
	public function isDeletable($path) {
79
		return false;
80
	}
81
82
	public function isSharable($path) {
83
		return false;
84
	}
85
86
	public function getPermissions($path) {
87
		return null;
88
	}
89
90
	public function file_exists($path) {
91
		return $path === '';
92
	}
93
94
	public function filemtime($path) {
95
		return ($path === '') ? time() : false;
96
	}
97
98
	public function file_get_contents($path) {
99
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
100
	}
101
102
	public function file_put_contents($path, $data) {
103
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
104
	}
105
106
	public function unlink($path) {
107
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
108
	}
109
110
	public function rename($path1, $path2) {
111
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
112
	}
113
114
	public function copy($path1, $path2) {
115
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
116
	}
117
118
	public function fopen($path, $mode) {
119
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
120
	}
121
122
	public function getMimeType($path) {
123
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
124
	}
125
126
	public function hash($type, $path, $raw = false) {
127
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
128
	}
129
130
	public function free_space($path) {
131
		return 0;
132
	}
133
134
	public function touch($path, $mtime = null) {
135
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
136
	}
137
138
	public function getLocalFile($path) {
139
		return false;
140
	}
141
142
	public function hasUpdated($path, $time) {
143
		return false;
144
	}
145
146
	public function getETag($path) {
147
		return '';
148
	}
149
150
	public function isLocal() {
151
		return false;
152
	}
153
154
	public function getDirectDownload($path) {
155
		return false;
156
	}
157
158
	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
159
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
160
	}
161
162
	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
163
		throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
164
	}
165
166
	public function test() {
167
		return true;
168
	}
169
170
	public function getOwner($path) {
171
		return null;
172
	}
173
174
	public function getCache($path = '', $storage = null) {
175
		return new NullCache();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OC\Lockdown\Filesystem\NullCache(); (OC\Lockdown\Filesystem\NullCache) is incompatible with the return type declared by the interface OC\Files\Storage\Storage::getCache of type OC\Files\Cache\Cache.

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...
176
	}
177
}
178