Conditions | 7 |
Paths | 12 |
Total Lines | 33 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
15 | public static function create($recipient, $message, $actor, $object = null, $url = null) { |
||
16 | $note = new SiteNotification(); |
||
17 | $note->owner_guid = $recipient->guid; |
||
18 | $note->container_guid = $recipient->guid; |
||
19 | $note->access_id = ACCESS_PRIVATE; |
||
20 | $note->description = $message; |
||
21 | |||
22 | if (!isset($url) && $object) { |
||
23 | // TODO Add support for setting an URL for a notification about a new relationship |
||
24 | switch ($object->getType()) { |
||
25 | case 'annotation': |
||
26 | // Annotations do not have an URL so we use the entity URL |
||
27 | $url = $object->getEntity()->getURL(); |
||
|
|||
28 | break; |
||
29 | default: |
||
30 | $url = $object->getURL(); |
||
31 | break; |
||
32 | } |
||
33 | } |
||
34 | |||
35 | if ($url && $url != elgg_get_site_url()) { |
||
36 | $note->setURL($url); |
||
37 | } |
||
38 | |||
39 | $note->setRead(false); |
||
40 | |||
41 | if ($note->save()) { |
||
42 | $note->setActor($actor); |
||
43 | return $note; |
||
44 | } else { |
||
45 | return null; |
||
46 | } |
||
47 | } |
||
48 | } |
||
49 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: