Passed
Push — master ( 34d335...9c4cd1 )
by Pauli
03:25
created

Entity::setter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 11
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2021 - 2025
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IL10N;
16
17
/**
18
 * Base class for all the entities belonging to the data model of the Music app
19
 * 
20
 * @method string getUserId()
21
 * @method void setUserId(string $userId)
22
 * @method ?string getCreated()
23
 * @method void setCreated(?string $timestamp)
24
 * @method ?string getUpdated()
25
 * @method void setUpdated(?string $timestamp)
26
 */
27
class Entity extends \OCP\AppFramework\Db\Entity {
28
	public string $userId = '';
29
	public ?string $created = null;
30
	public ?string $updated = null;
31
32
	/**
33
	 * All entities have a non-empty human-readable name, although the exact name of the
34
	 * corresponding DB column varies and in some cases, the value may be technically
35
	 * empty but replaced with some localized place-holder text.
36
	 *
37
	 * The derived classes may override this as needed.
38
	 */
39
	public function getNameString(IL10N $l10n) : string {
40
		($l10n); // @phpstan-ignore expr.resultUnused (unused in this base implementation)
41
42
		if (\property_exists($this, 'name')) {
43
			return $this->name ?? '';
44
		} elseif (\property_exists($this, 'title')) {
45
			return $this->title ?? '';
46
		} else {
47
			return 'UNIMPLEMENTED';
48
		}
49
	}
50
51
	/**
52
	 * Override the property setter from the platform base class.
53
	 *
54
	 * NOTE: Type declarations should not be used on the parameters because OC and NC < 26
55
	 * don't use them in the parent class. On those platforms, using the declarations in this
56
	 * override method would break the PHP contravariance rules.
57
	 *
58
	 * @param string $name
59
	 * @param mixed[] $args
60
	 */
61
	protected function setter($name, $args) : void {
62
		parent::setter($name, $args);
63
		/**
64
		 * The parent implementation has such a feature that it doesn't mark a field updated
65
		 * if the new value is the same as the previous value. This is problematic in case
66
		 * we have created a new Entity and the new value is the same as the default value.
67
		 * We may still use that new Entity with Mapper::update with the intention to change
68
		 * that field on an existing row to its default value. This is what caused
69
		 * https://github.com/owncloud/music/issues/1251 and probably some other subtle bugs, too.
70
		 */
71
		$this->markFieldUpdated($name);
72
	}
73
}
74