| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Xetaravel\Models\Repositories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Support\Facades\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Xetaravel\Events\Discuss\ConversationWasLockedEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Xetaravel\Events\Discuss\ConversationWasPinnedEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Xetaravel\Models\DiscussPost; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Xetaravel\Models\DiscussConversation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class DiscussPostRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Create a new post instance after a valid validation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @param array $data The data used to create the post. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @return \Xetaravel\Models\DiscussPost | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Find the previous post related to the given post. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param \Xetaravel\Models\DiscussPost $post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return \Xetaravel\Models\DiscussPost|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     public static function findPreviousPost(DiscussPost $post) | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         return DiscussPost::where('id', '!=', $post->conversation->solved_post_id) | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |                 ->where('conversation_id', $post->conversation->getKey()) | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |                 ->where('created_at', '<', $post->created_at) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |                 ->orderBy('created_at', 'desc') | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |                 ->first(); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                        
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: