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 |
||
24 | class UserLacksPermission extends \Exception |
||
25 | { |
||
26 | use AccessorTrait; |
||
27 | |||
28 | const DEFAULT_MESSAGE = 'User lacks the required permission.'; |
||
29 | |||
30 | /** |
||
31 | * @var User |
||
32 | */ |
||
33 | private $user; |
||
34 | |||
35 | /** |
||
36 | * @return User |
||
37 | */ |
||
38 | protected function get_user() |
||
42 | |||
43 | /** |
||
44 | * @var mixed |
||
45 | */ |
||
46 | private $permission; |
||
47 | |||
48 | /** |
||
49 | * @return string|int |
||
50 | */ |
||
51 | protected function get_permission() |
||
55 | |||
56 | /** |
||
57 | * @var mixed |
||
58 | */ |
||
59 | private $resource; |
||
60 | |||
61 | /** |
||
62 | * @return mixed |
||
63 | */ |
||
64 | protected function get_resource() |
||
68 | |||
69 | /** |
||
70 | * @param User $user |
||
71 | * @param string|int $permission |
||
72 | * @param mixed $resource |
||
73 | * @param string $message |
||
74 | * @param \Exception|null $previous |
||
75 | */ |
||
76 | View Code Duplication | public function __construct( |
|
89 | } |
||
90 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.