Issues (947)

engine/classes/ElggAnnotation.php (1 issue)

1
<?php
2
3
/**
4
 * Entity Annotation
5
 *
6
 * Annotations allow you to attach bits of information to entities.
7
 * Unlike entity metadata, annotation is access controlled and has owners.
8
 */
9
class ElggAnnotation extends \ElggExtender {
10
11
	/**
12
	 * {@inheritdoc}
13
	 */
14 159
	protected function initializeAttributes() {
15 159
		parent::initializeAttributes();
16
17 159
		$this->attributes['type'] = 'annotation';
18 159
	}
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param stdClass $row Database row
24
	 */
25 159
	public function __construct(stdClass $row = null) {
26 159
		$this->initializeAttributes();
27
28 159
		if ($row) {
29 87
			foreach ((array) $row as $key => $value) {
30 87
				$this->$key = $value;
31
			}
32
		}
33 159
	}
34
35
	/**
36
	 * Save this instance and returns an annotation ID
37
	 *
38
	 * @return int|bool
39
	 */
40 153
	public function save() {
41 153
		if (!isset($this->access_id)) {
42
			$this->access_id = ACCESS_PRIVATE;
43
		}
44
45 153
		if (!isset($this->owner_guid)) {
46
			$this->owner_guid = _elgg_services()->session->getLoggedInUserGuid();
47
		}
48
49 153
		if ($this->id) {
50
			return _elgg_services()->annotationsTable->update($this);
51
		}
52
53 153
		$entity = get_entity($this->entity_guid);
54 153
		if (!$entity) {
55
			return false;
56
		}
57
58 153
		if (_elgg_services()->annotationsTable->create($this, $entity)) {
59 153
			return $this->id;
0 ignored issues
show
Bug Best Practice introduced by Brett Profitt
The expression return $this->id returns the type integer which is incompatible with the return type mandated by ElggData::save() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
60
		}
61
62
		return false;
63
	}
64
65
	/**
66
	 * Delete the annotation.
67
	 *
68
	 * @return bool
69
	 */
70 56
	public function delete() {
71 56
		return _elgg_services()->annotationsTable->delete($this);
72
	}
73
74
	/**
75
	 * Disable the annotation.
76
	 *
77
	 * @return bool
78
	 * @since 1.8
79
	 */
80 9
	public function disable() {
81 9
		return _elgg_services()->annotationsTable->disable($this);
82
	}
83
84
	/**
85
	 * Enable the annotation.
86
	 *
87
	 * @return bool
88
	 * @since 1.8
89
	 */
90 4
	public function enable() {
91 4
		return _elgg_services()->annotationsTable->enable($this);
92
	}
93
94
	/**
95
	 * Determines whether or not the user can edit this annotation
96
	 *
97
	 * @param int $user_guid The GUID of the user (defaults to currently logged in user)
98
	 *
99
	 * @return bool
100
	 */
101 61
	public function canEdit($user_guid = 0) {
102 61
		$entity = $this->getEntity();
103
104 61
		return _elgg_services()->userCapabilities->canEditAnnotation($entity, $user_guid, $this);
105
	}
106
107
	/**
108
	 * {@inheritdoc}
109
	 */
110
	public function getObjectFromID($id) {
111
		return elgg_get_annotation_from_id($id);
112
	}
113
}
114