Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php  | 
            ||
| 29 | trait ManageComments  | 
            ||
| 30 | { | 
            ||
| 31 | /**  | 
            ||
| 32 | * Open article comment.  | 
            ||
| 33 | *  | 
            ||
| 34 | * @return \EasyWeChat\Support\Collection  | 
            ||
| 35 | */  | 
            ||
| 36 | 1 | public function openComment()  | 
            |
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * Close comment.  | 
            ||
| 43 | *  | 
            ||
| 44 | * @return \EasyWeChat\Support\Collection  | 
            ||
| 45 | */  | 
            ||
| 46 | 1 | public function closeComment()  | 
            |
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * Get article comments.  | 
            ||
| 53 | *  | 
            ||
| 54 | * @param int $begin  | 
            ||
| 55 | * @param int $count  | 
            ||
| 56 | * @param int $type  | 
            ||
| 57 | *  | 
            ||
| 58 | * @return \EasyWeChat\Support\Collection  | 
            ||
| 59 | */  | 
            ||
| 60 | 1 | View Code Duplication | public function comments($begin, $count, $type = 0)  | 
            
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * Mark elect comment.  | 
            ||
| 73 | *  | 
            ||
| 74 | * @param int $commentId  | 
            ||
| 75 | *  | 
            ||
| 76 | * @return \EasyWeChat\Support\Collection  | 
            ||
| 77 | */  | 
            ||
| 78 | 1 | View Code Duplication | public function markElectComment($commentId)  | 
            
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * Unmark comment elect.  | 
            ||
| 89 | *  | 
            ||
| 90 | * @param int $commentId  | 
            ||
| 91 | *  | 
            ||
| 92 | * @return \EasyWeChat\Support\Collection  | 
            ||
| 93 | */  | 
            ||
| 94 | 1 | View Code Duplication | public function unmarkElectComment($commentId)  | 
            
| 102 | |||
| 103 | /**  | 
            ||
| 104 | * Delete comment.  | 
            ||
| 105 | *  | 
            ||
| 106 | * @param int $commentId  | 
            ||
| 107 | *  | 
            ||
| 108 | * @return \EasyWeChat\Support\Collection  | 
            ||
| 109 | */  | 
            ||
| 110 | 1 | View Code Duplication | public function deleteComment($commentId)  | 
            
| 118 | }  | 
            ||
| 119 | 
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.