Completed
Push — master ( c24153...6c11c5 )
by Lukas
21:09 queued 13:25
created

Cache::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christopher Schäpers <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Michael Gapczynski <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OCA\Files_Sharing;
29
30
use OC\Files\Cache\Wrapper\CacheJail;
31
use OCP\Files\Cache\ICacheEntry;
32
use OCP\Files\Storage\IStorage;
33
34
/**
35
 * Metadata cache for shared files
36
 *
37
 * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
38
 */
39
class Cache extends CacheJail {
40
	/**
41
	 * @var \OCA\Files_Sharing\SharedStorage
42
	 */
43
	private $storage;
44
45
	/**
46
	 * @var ICacheEntry
47
	 */
48
	private $sourceRootInfo;
49
50
	private $rootUnchanged = true;
51
52
	private $ownerDisplayName;
53
54
	/**
55
	 * @param \OCA\Files_Sharing\SharedStorage $storage
56
	 * @param ICacheEntry $sourceRootInfo
57
	 */
58
	public function __construct($storage, ICacheEntry $sourceRootInfo) {
59
		$this->storage = $storage;
60
		$this->sourceRootInfo = $sourceRootInfo;
61
		parent::__construct(
62
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<OCP\Files\Cache\ICache>.

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...
63
			$this->sourceRootInfo->getPath()
64
		);
65
	}
66
67
	public function getCache() {
68
		if (is_null($this->cache)) {
69
			$this->cache = $this->storage->getSourceStorage()->getCache();
70
		}
71
		return $this->cache;
72
	}
73
74
	public function getNumericStorageId() {
75
		if (isset($this->numericId)) {
76
			return $this->numericId;
0 ignored issues
show
Bug introduced by
The property numericId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77
		} else {
78
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface OCP\Files\Cache\ICache::getNumericStorageId of type integer.

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...
79
		}
80
	}
81
82
	public function get($file) {
83
		if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
84
			return $this->formatCacheEntry(clone $this->sourceRootInfo);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->formatCacheEntry(...$this->sourceRootInfo); of type OCP\Files\Cache\ICacheEntry|array adds the type array to the return on line 84 which is incompatible with the return type declared by the interface OCP\Files\Cache\ICache::get of type OCP\Files\Cache\ICacheEntry|false.
Loading history...
85
		}
86
		return parent::get($file);
87
	}
88
89
	public function update($id, array $data) {
90
		$this->rootUnchanged = false;
91
		parent::update($id, $data);
92
	}
93
94
	public function insert($file, array $data) {
95
		$this->rootUnchanged = false;
96
		return parent::insert($file, $data);
97
	}
98
99
	public function remove($file) {
100
		$this->rootUnchanged = false;
101
		parent::remove($file);
102
	}
103
104
	public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
105
		$this->rootUnchanged = false;
106
		return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
107
	}
108
109
	protected function formatCacheEntry($entry) {
110
		$path = isset($entry['path']) ? $entry['path'] : '';
111
		$entry = parent::formatCacheEntry($entry);
112
		$sharePermissions = $this->storage->getPermissions($path);
113
		if (isset($entry['permissions'])) {
114
			$entry['permissions'] &= $sharePermissions;
115
		} else {
116
			$entry['permissions'] = $sharePermissions;
117
		}
118
		$entry['uid_owner'] = $this->storage->getOwner($path);
119
		$entry['displayname_owner'] = $this->getOwnerDisplayName();
120
		if ($path === '') {
121
			$entry['is_share_mount_point'] = true;
122
		}
123
		return $entry;
124
	}
125
126
	private function getOwnerDisplayName() {
127
		if (!$this->ownerDisplayName) {
128
			$this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner(''));
129
		}
130
		return $this->ownerDisplayName;
131
	}
132
133
	/**
134
	 * remove all entries for files that are stored on the storage from the cache
135
	 */
136
	public function clear() {
137
		// Not a valid action for Shared Cache
138
	}
139
}
140