| Conditions | 8 |
| Paths | 17 |
| Total Lines | 30 |
| Code Lines | 17 |
| Lines | 6 |
| Ratio | 20 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 19 | public static function create(array $data): DiscussPost |
||
| 20 | { |
||
| 21 | $post = DiscussPost::create([ |
||
| 22 | 'conversation_id' => $data['conversation_id'], |
||
| 23 | 'content' => $data['content'] |
||
| 24 | ]); |
||
| 25 | |||
| 26 | if (Auth::user()->hasPermission('manage.discuss.conversations')) { |
||
|
|
|||
| 27 | $conversation = DiscussConversation::find($data['conversation_id']); |
||
| 28 | |||
| 29 | $data['is_pinned'] = isset($data['is_pinned']) ? true : false; |
||
| 30 | $data['is_locked'] = isset($data['is_locked']) ? true : false; |
||
| 31 | |||
| 32 | View Code Duplication | if ($conversation->is_pinned != $data['is_pinned'] && $data['is_pinned'] == true) { |
|
| 33 | event(new ConversationWasPinnedEvent($conversation)); |
||
| 34 | } |
||
| 35 | |||
| 36 | View Code Duplication | if ($conversation->is_locked != $data['is_locked'] && $data['is_locked'] == true) { |
|
| 37 | event(new ConversationWasLockedEvent($conversation)); |
||
| 38 | } |
||
| 39 | |||
| 40 | $conversation->is_locked = $data['is_locked']; |
||
| 41 | $conversation->is_pinned = $data['is_pinned']; |
||
| 42 | $conversation->last_post_id = $post->getKey(); |
||
| 43 | |||
| 44 | $conversation->save(); |
||
| 45 | } |
||
| 46 | |||
| 47 | return $post; |
||
| 48 | } |
||
| 49 | } |
||
| 50 |
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 implementation 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 interface: