aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/bookmark/Bookmark.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-02 17:50:59 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 13:50:11 +0200
commitefb7d21b52eb033530e80e5e49d175e6e3b031f4 (patch)
tree4f34052788a08be1a30cb88c3339ae14e0b7c4da /application/bookmark/Bookmark.php
parent29c31b7ec6ca48ba37b7eb6da650931fd0cb7164 (diff)
downloadShaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.gz
Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.zst
Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.zip
Add strict types for bookmarks management
Parameters typing and using strict types overall increase the codebase quality by enforcing the a given parameter will have the expected type. It also removes the need to unnecessary unit tests checking methods behavior with invalid input.
Diffstat (limited to 'application/bookmark/Bookmark.php')
-rw-r--r--application/bookmark/Bookmark.php121
1 files changed, 61 insertions, 60 deletions
diff --git a/application/bookmark/Bookmark.php b/application/bookmark/Bookmark.php
index 1beb8be2..fa45d2fc 100644
--- a/application/bookmark/Bookmark.php
+++ b/application/bookmark/Bookmark.php
@@ -1,5 +1,7 @@
1<?php 1<?php
2 2
3declare(strict_types=1);
4
3namespace Shaarli\Bookmark; 5namespace Shaarli\Bookmark;
4 6
5use DateTime; 7use DateTime;
@@ -59,25 +61,25 @@ class Bookmark
59 * 61 *
60 * @return $this 62 * @return $this
61 */ 63 */
62 public function fromArray($data) 64 public function fromArray(array $data): Bookmark
63 { 65 {
64 $this->id = $data['id']; 66 $this->id = $data['id'] ?? null;
65 $this->shortUrl = $data['shorturl']; 67 $this->shortUrl = $data['shorturl'] ?? null;
66 $this->url = $data['url']; 68 $this->url = $data['url'] ?? null;
67 $this->title = $data['title']; 69 $this->title = $data['title'] ?? null;
68 $this->description = $data['description']; 70 $this->description = $data['description'] ?? null;
69 $this->thumbnail = isset($data['thumbnail']) ? $data['thumbnail'] : null; 71 $this->thumbnail = $data['thumbnail'] ?? null;
70 $this->sticky = isset($data['sticky']) ? $data['sticky'] : false; 72 $this->sticky = $data['sticky'] ?? false;
71 $this->created = $data['created']; 73 $this->created = $data['created'] ?? null;
72 if (is_array($data['tags'])) { 74 if (is_array($data['tags'])) {
73 $this->tags = $data['tags']; 75 $this->tags = $data['tags'];
74 } else { 76 } else {
75 $this->tags = preg_split('/\s+/', $data['tags'], -1, PREG_SPLIT_NO_EMPTY); 77 $this->tags = preg_split('/\s+/', $data['tags'] ?? '', -1, PREG_SPLIT_NO_EMPTY);
76 } 78 }
77 if (! empty($data['updated'])) { 79 if (! empty($data['updated'])) {
78 $this->updated = $data['updated']; 80 $this->updated = $data['updated'];
79 } 81 }
80 $this->private = $data['private'] ? true : false; 82 $this->private = ($data['private'] ?? false) ? true : false;
81 83
82 return $this; 84 return $this;
83 } 85 }
@@ -95,13 +97,12 @@ class Bookmark
95 * 97 *
96 * @throws InvalidBookmarkException 98 * @throws InvalidBookmarkException
97 */ 99 */
98 public function validate() 100 public function validate(): void
99 { 101 {
100 if ($this->id === null 102 if ($this->id === null
101 || ! is_int($this->id) 103 || ! is_int($this->id)
102 || empty($this->shortUrl) 104 || empty($this->shortUrl)
103 || empty($this->created) 105 || empty($this->created)
104 || ! $this->created instanceof DateTimeInterface
105 ) { 106 ) {
106 throw new InvalidBookmarkException($this); 107 throw new InvalidBookmarkException($this);
107 } 108 }
@@ -119,11 +120,11 @@ class Bookmark
119 * - created: with the current datetime 120 * - created: with the current datetime
120 * - shortUrl: with a generated small hash from the date and the given ID 121 * - shortUrl: with a generated small hash from the date and the given ID
121 * 122 *
122 * @param int $id 123 * @param int|null $id
123 * 124 *
124 * @return Bookmark 125 * @return Bookmark
125 */ 126 */
126 public function setId($id) 127 public function setId(?int $id): Bookmark
127 { 128 {
128 $this->id = $id; 129 $this->id = $id;
129 if (empty($this->created)) { 130 if (empty($this->created)) {
@@ -139,9 +140,9 @@ class Bookmark
139 /** 140 /**
140 * Get the Id. 141 * Get the Id.
141 * 142 *
142 * @return int 143 * @return int|null
143 */ 144 */
144 public function getId() 145 public function getId(): ?int
145 { 146 {
146 return $this->id; 147 return $this->id;
147 } 148 }
@@ -149,9 +150,9 @@ class Bookmark
149 /** 150 /**
150 * Get the ShortUrl. 151 * Get the ShortUrl.
151 * 152 *
152 * @return string 153 * @return string|null
153 */ 154 */
154 public function getShortUrl() 155 public function getShortUrl(): ?string
155 { 156 {
156 return $this->shortUrl; 157 return $this->shortUrl;
157 } 158 }
@@ -159,9 +160,9 @@ class Bookmark
159 /** 160 /**
160 * Get the Url. 161 * Get the Url.
161 * 162 *
162 * @return string 163 * @return string|null
163 */ 164 */
164 public function getUrl() 165 public function getUrl(): ?string
165 { 166 {
166 return $this->url; 167 return $this->url;
167 } 168 }
@@ -171,7 +172,7 @@ class Bookmark
171 * 172 *
172 * @return string 173 * @return string
173 */ 174 */
174 public function getTitle() 175 public function getTitle(): ?string
175 { 176 {
176 return $this->title; 177 return $this->title;
177 } 178 }
@@ -181,7 +182,7 @@ class Bookmark
181 * 182 *
182 * @return string 183 * @return string
183 */ 184 */
184 public function getDescription() 185 public function getDescription(): string
185 { 186 {
186 return ! empty($this->description) ? $this->description : ''; 187 return ! empty($this->description) ? $this->description : '';
187 } 188 }
@@ -191,7 +192,7 @@ class Bookmark
191 * 192 *
192 * @return DateTimeInterface 193 * @return DateTimeInterface
193 */ 194 */
194 public function getCreated() 195 public function getCreated(): ?DateTimeInterface
195 { 196 {
196 return $this->created; 197 return $this->created;
197 } 198 }
@@ -201,7 +202,7 @@ class Bookmark
201 * 202 *
202 * @return DateTimeInterface 203 * @return DateTimeInterface
203 */ 204 */
204 public function getUpdated() 205 public function getUpdated(): ?DateTimeInterface
205 { 206 {
206 return $this->updated; 207 return $this->updated;
207 } 208 }
@@ -209,11 +210,11 @@ class Bookmark
209 /** 210 /**
210 * Set the ShortUrl. 211 * Set the ShortUrl.
211 * 212 *
212 * @param string $shortUrl 213 * @param string|null $shortUrl
213 * 214 *
214 * @return Bookmark 215 * @return Bookmark
215 */ 216 */
216 public function setShortUrl($shortUrl) 217 public function setShortUrl(?string $shortUrl): Bookmark
217 { 218 {
218 $this->shortUrl = $shortUrl; 219 $this->shortUrl = $shortUrl;
219 220
@@ -223,14 +224,14 @@ class Bookmark
223 /** 224 /**
224 * Set the Url. 225 * Set the Url.
225 * 226 *
226 * @param string $url 227 * @param string|null $url
227 * @param array $allowedProtocols 228 * @param string[] $allowedProtocols
228 * 229 *
229 * @return Bookmark 230 * @return Bookmark
230 */ 231 */
231 public function setUrl($url, $allowedProtocols = []) 232 public function setUrl(?string $url, array $allowedProtocols = []): Bookmark
232 { 233 {
233 $url = trim($url); 234 $url = $url !== null ? trim($url) : '';
234 if (! empty($url)) { 235 if (! empty($url)) {
235 $url = whitelist_protocols($url, $allowedProtocols); 236 $url = whitelist_protocols($url, $allowedProtocols);
236 } 237 }
@@ -242,13 +243,13 @@ class Bookmark
242 /** 243 /**
243 * Set the Title. 244 * Set the Title.
244 * 245 *
245 * @param string $title 246 * @param string|null $title
246 * 247 *
247 * @return Bookmark 248 * @return Bookmark
248 */ 249 */
249 public function setTitle($title) 250 public function setTitle(?string $title): Bookmark
250 { 251 {
251 $this->title = trim($title); 252 $this->title = $title !== null ? trim($title) : '';
252 253
253 return $this; 254 return $this;
254 } 255 }
@@ -256,11 +257,11 @@ class Bookmark
256 /** 257 /**
257 * Set the Description. 258 * Set the Description.
258 * 259 *
259 * @param string $description 260 * @param string|null $description
260 * 261 *
261 * @return Bookmark 262 * @return Bookmark
262 */ 263 */
263 public function setDescription($description) 264 public function setDescription(?string $description): Bookmark
264 { 265 {
265 $this->description = $description; 266 $this->description = $description;
266 267
@@ -271,11 +272,11 @@ class Bookmark
271 * Set the Created. 272 * Set the Created.
272 * Note: you shouldn't set this manually except for special cases (like bookmark import) 273 * Note: you shouldn't set this manually except for special cases (like bookmark import)
273 * 274 *
274 * @param DateTimeInterface $created 275 * @param DateTimeInterface|null $created
275 * 276 *
276 * @return Bookmark 277 * @return Bookmark
277 */ 278 */
278 public function setCreated($created) 279 public function setCreated(?DateTimeInterface $created): Bookmark
279 { 280 {
280 $this->created = $created; 281 $this->created = $created;
281 282
@@ -285,11 +286,11 @@ class Bookmark
285 /** 286 /**
286 * Set the Updated. 287 * Set the Updated.
287 * 288 *
288 * @param DateTimeInterface $updated 289 * @param DateTimeInterface|null $updated
289 * 290 *
290 * @return Bookmark 291 * @return Bookmark
291 */ 292 */
292 public function setUpdated($updated) 293 public function setUpdated(?DateTimeInterface $updated): Bookmark
293 { 294 {
294 $this->updated = $updated; 295 $this->updated = $updated;
295 296
@@ -301,7 +302,7 @@ class Bookmark
301 * 302 *
302 * @return bool 303 * @return bool
303 */ 304 */
304 public function isPrivate() 305 public function isPrivate(): bool
305 { 306 {
306 return $this->private ? true : false; 307 return $this->private ? true : false;
307 } 308 }
@@ -309,11 +310,11 @@ class Bookmark
309 /** 310 /**
310 * Set the Private. 311 * Set the Private.
311 * 312 *
312 * @param bool $private 313 * @param bool|null $private
313 * 314 *
314 * @return Bookmark 315 * @return Bookmark
315 */ 316 */
316 public function setPrivate($private) 317 public function setPrivate(?bool $private): Bookmark
317 { 318 {
318 $this->private = $private ? true : false; 319 $this->private = $private ? true : false;
319 320
@@ -323,9 +324,9 @@ class Bookmark
323 /** 324 /**
324 * Get the Tags. 325 * Get the Tags.
325 * 326 *
326 * @return array 327 * @return string[]
327 */ 328 */
328 public function getTags() 329 public function getTags(): array
329 { 330 {
330 return is_array($this->tags) ? $this->tags : []; 331 return is_array($this->tags) ? $this->tags : [];
331 } 332 }
@@ -333,13 +334,13 @@ class Bookmark
333 /** 334 /**
334 * Set the Tags. 335 * Set the Tags.
335 * 336 *
336 * @param array $tags 337 * @param string[]|null $tags
337 * 338 *
338 * @return Bookmark 339 * @return Bookmark
339 */ 340 */
340 public function setTags($tags) 341 public function setTags(?array $tags): Bookmark
341 { 342 {
342 $this->setTagsString(implode(' ', $tags)); 343 $this->setTagsString(implode(' ', $tags ?? []));
343 344
344 return $this; 345 return $this;
345 } 346 }
@@ -357,11 +358,11 @@ class Bookmark
357 /** 358 /**
358 * Set the Thumbnail. 359 * Set the Thumbnail.
359 * 360 *
360 * @param string|bool $thumbnail Thumbnail's URL - false if no thumbnail could be found 361 * @param string|bool|null $thumbnail Thumbnail's URL - false if no thumbnail could be found
361 * 362 *
362 * @return Bookmark 363 * @return Bookmark
363 */ 364 */
364 public function setThumbnail($thumbnail) 365 public function setThumbnail($thumbnail): Bookmark
365 { 366 {
366 $this->thumbnail = $thumbnail; 367 $this->thumbnail = $thumbnail;
367 368
@@ -373,7 +374,7 @@ class Bookmark
373 * 374 *
374 * @return bool 375 * @return bool
375 */ 376 */
376 public function isSticky() 377 public function isSticky(): bool
377 { 378 {
378 return $this->sticky ? true : false; 379 return $this->sticky ? true : false;
379 } 380 }
@@ -381,11 +382,11 @@ class Bookmark
381 /** 382 /**
382 * Set the Sticky. 383 * Set the Sticky.
383 * 384 *
384 * @param bool $sticky 385 * @param bool|null $sticky
385 * 386 *
386 * @return Bookmark 387 * @return Bookmark
387 */ 388 */
388 public function setSticky($sticky) 389 public function setSticky(?bool $sticky): Bookmark
389 { 390 {
390 $this->sticky = $sticky ? true : false; 391 $this->sticky = $sticky ? true : false;
391 392
@@ -395,7 +396,7 @@ class Bookmark
395 /** 396 /**
396 * @return string Bookmark's tags as a string, separated by a space 397 * @return string Bookmark's tags as a string, separated by a space
397 */ 398 */
398 public function getTagsString() 399 public function getTagsString(): string
399 { 400 {
400 return implode(' ', $this->getTags()); 401 return implode(' ', $this->getTags());
401 } 402 }
@@ -403,7 +404,7 @@ class Bookmark
403 /** 404 /**
404 * @return bool 405 * @return bool
405 */ 406 */
406 public function isNote() 407 public function isNote(): bool
407 { 408 {
408 // We check empty value to get a valid result if the link has not been saved yet 409 // We check empty value to get a valid result if the link has not been saved yet
409 return empty($this->url) || startsWith($this->url, '/shaare/') || $this->url[0] === '?'; 410 return empty($this->url) || startsWith($this->url, '/shaare/') || $this->url[0] === '?';
@@ -416,14 +417,14 @@ class Bookmark
416 * - multiple spaces will be removed 417 * - multiple spaces will be removed
417 * - trailing dash in tags will be removed 418 * - trailing dash in tags will be removed
418 * 419 *
419 * @param string $tags 420 * @param string|null $tags
420 * 421 *
421 * @return $this 422 * @return $this
422 */ 423 */
423 public function setTagsString($tags) 424 public function setTagsString(?string $tags): Bookmark
424 { 425 {
425 // Remove first '-' char in tags. 426 // Remove first '-' char in tags.
426 $tags = preg_replace('/(^| )\-/', '$1', $tags); 427 $tags = preg_replace('/(^| )\-/', '$1', $tags ?? '');
427 // Explode all tags separted by spaces or commas 428 // Explode all tags separted by spaces or commas
428 $tags = preg_split('/[\s,]+/', $tags); 429 $tags = preg_split('/[\s,]+/', $tags);
429 // Remove eventual empty values 430 // Remove eventual empty values
@@ -440,7 +441,7 @@ class Bookmark
440 * @param string $fromTag 441 * @param string $fromTag
441 * @param string $toTag 442 * @param string $toTag
442 */ 443 */
443 public function renameTag($fromTag, $toTag) 444 public function renameTag(string $fromTag, string $toTag): void
444 { 445 {
445 if (($pos = array_search($fromTag, $this->tags)) !== false) { 446 if (($pos = array_search($fromTag, $this->tags)) !== false) {
446 $this->tags[$pos] = trim($toTag); 447 $this->tags[$pos] = trim($toTag);
@@ -452,7 +453,7 @@ class Bookmark
452 * 453 *
453 * @param string $tag 454 * @param string $tag
454 */ 455 */
455 public function deleteTag($tag) 456 public function deleteTag(string $tag): void
456 { 457 {
457 if (($pos = array_search($tag, $this->tags)) !== false) { 458 if (($pos = array_search($tag, $this->tags)) !== false) {
458 unset($this->tags[$pos]); 459 unset($this->tags[$pos]);