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 |
||
20 | class SecurityUtils |
||
21 | { |
||
22 | /** @var SecurityUser */ |
||
23 | private $securityUser; |
||
24 | |||
25 | /** @var string */ |
||
26 | private $requestId; |
||
27 | |||
28 | /** |
||
29 | * StoreManager constructor. |
||
30 | * @param TokenStorage $tokenStorage Sf Auth token storage |
||
31 | */ |
||
32 | 4 | public function __construct( |
|
37 | |||
38 | /** |
||
39 | * Check if there is a security user |
||
40 | * |
||
41 | * @return bool |
||
42 | */ |
||
43 | public function isSecurityUser() |
||
57 | |||
58 | /** |
||
59 | * Find current user |
||
60 | * |
||
61 | * @return string|bool |
||
62 | * @throws UsernameNotFoundException |
||
63 | */ |
||
64 | public function getSecurityUser() |
||
71 | |||
72 | /** |
||
73 | * Return users username |
||
74 | * |
||
75 | * @return string |
||
76 | * @throws UsernameNotFoundException |
||
77 | */ |
||
78 | public function getSecurityUsername() |
||
85 | |||
86 | /** |
||
87 | * Check if current user is in Role |
||
88 | * |
||
89 | * @param string $role User role expected |
||
90 | * @return bool |
||
91 | * @throws UsernameNotFoundException |
||
92 | */ |
||
93 | public function hasRole($role) |
||
100 | |||
101 | /** |
||
102 | * Get current unique request ID |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 2 | public function getRequestId() |
|
113 | |||
114 | /** |
||
115 | * Generate a unique UUID. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | 4 | private function generateUuid() |
|
134 | } |
||
135 |
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.