Completed
Pull Request — master (#31951)
by Jörn Friedrich
15:52
created

RemoteUser::getUserName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\User;
23
24
use OCP\IUser;
25
26
/**
27
 * Class RemoteUser - an implementation of IUser for user on a remote/federated server
28
 *
29
 * @package OC\User
30
 */
31
class RemoteUser implements IUser {
32
33
	/** @var string */
34
	private $userId;
35
36
	/**
37
	 * RemoteUser constructor.
38
	 *
39
	 * @param string $userId
40
	 */
41
	public function __construct($userId) {
42
		$this->userId = $userId;
43
	}
44
45
	/**
46
	 * @inheritdoc
47
	 */
48
	public function getUID() {
49
		return $this->userId;
50
	}
51
52
	/**
53
	 * @inheritdoc
54
	 */
55
	public function getUserName() {
56
		return $this->getUID();
57
	}
58
59
	/**
60
	 * @inheritdoc
61
	 */
62
	public function setUserName($userName) {}
63
64
	/**
65
	 * @inheritdoc
66
	 */
67
	public function getDisplayName() {
68
		return $this->userId;
69
	}
70
71
	/**
72
	 * @inheritdoc
73
	 */
74
	public function setDisplayName($displayName) {
75
		return false;
76
	}
77
78
	/**
79
	 * @inheritdoc
80
	 */
81
	public function getLastLogin() {
82
		return 0;
83
	}
84
85
	/**
86
	 * @inheritdoc
87
	 */
88
	public function updateLastLoginTimestamp() {
89
	}
90
91
	/**
92
	 * @inheritdoc
93
	 */
94
	public function delete() {
95
		return false;
96
	}
97
98
	/**
99
	 * @inheritdoc
100
	 */
101
	public function setPassword($password, $recoveryPassword = null) {
102
		return false;
103
	}
104
105
	/**
106
	 * @inheritdoc
107
	 */
108
	public function getHome() {
109
	}
110
111
	/**
112
	 * @inheritdoc
113
	 */
114
	public function getBackendClassName() {
115
		return 'Remote';
116
	}
117
118
	/**
119
	 * @inheritdoc
120
	 */
121
	public function canChangeAvatar() {
122
		return false;
123
	}
124
125
	/**
126
	 * @inheritdoc
127
	 */
128
	public function canChangePassword() {
129
		return false;
130
	}
131
132
	/**
133
	 * @inheritdoc
134
	 */
135
	public function canChangeDisplayName() {
136
		return false;
137
	}
138
139
	/**
140
	 * @inheritdoc
141
	 */
142
	public function isEnabled() {
143
		return true;
144
	}
145
146
	/**
147
	 * @inheritdoc
148
	 */
149
	public function setEnabled($enabled) {
150
		return false;
151
	}
152
153
	/**
154
	 * @inheritdoc
155
	 */
156
	public function getEMailAddress() {
157
		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\IUser::getEMailAddress of type string|null.

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...
158
	}
159
160
	/**
161
	 * @inheritdoc
162
	 */
163
	public function getAvatarImage($size) {
164
		return null;
165
	}
166
167
	/**
168
	 * @inheritdoc
169
	 */
170
	public function getCloudId() {
171
		$uid = $this->getUID();
172
		$server = \OC::$server->getURLGenerator()->getAbsoluteURL('/');
173
		return $uid . '@' . \rtrim($this->removeProtocolFromUrl($server), '/');
174
	}
175
176
	/**
177
	 * @param string $url
178
	 * @return string
179
	 */
180 View Code Duplication
	private function removeProtocolFromUrl($url) {
181
		if (\strpos($url, 'https://') === 0) {
182
			return \substr($url, \strlen('https://'));
183
		} elseif (\strpos($url, 'http://') === 0) {
184
			return \substr($url, \strlen('http://'));
185
		}
186
187
		return $url;
188
	}
189
190
	/**
191
	 * @inheritdoc
192
	 */
193
	public function setEMailAddress($mailAddress) {
194
	}
195
196
	/**
197
	 * @inheritdoc
198
	 */
199
	public function getQuota() {
200
		return 'none';
201
	}
202
203
	/**
204
	 * @inheritdoc
205
	 */
206
	public function setQuota($quota) {
207
	}
208
209
	/**
210
	 * @inheritdoc
211
	 */
212
	public function getSearchTerms() {
213
		return [];
214
	}
215
216
	/**
217
	 * @inheritdoc
218
	 */
219
	public function setSearchTerms(array $terms) {
220
	}
221
}
222