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 |
||
11 | final class SecurityConfigurationFactory |
||
12 | { |
||
13 | /** |
||
14 | * @var bool |
||
15 | */ |
||
16 | private $forceIdentified; |
||
17 | |||
18 | /** |
||
19 | * Security constructor. |
||
20 | * |
||
21 | * @param bool $forceIdentified |
||
22 | */ |
||
23 | public function __construct($forceIdentified) |
||
27 | |||
28 | /** |
||
29 | * Returns a pre-built security configuration for an internal page. |
||
30 | * |
||
31 | * @category Security-Critical |
||
32 | * @return SecurityConfiguration |
||
33 | */ |
||
34 | public function asInternalPage() |
||
44 | |||
45 | /** |
||
46 | * Returns a pre-built security configuration for a tool admin only page. |
||
47 | * |
||
48 | * @category Security-Critical |
||
49 | * @return SecurityConfiguration |
||
50 | */ |
||
51 | public function asAdminPage() |
||
60 | |||
61 | /** |
||
62 | * Returns a pre-built security configuration for a page accessible to *ALL* logged in users, including suspended |
||
63 | * and new users. This probably isn't the setting you want. |
||
64 | * |
||
65 | * @category Security-Critical |
||
66 | * @return SecurityConfiguration |
||
67 | */ |
||
68 | public function asAllLoggedInUsersPage() |
||
81 | |||
82 | /** |
||
83 | * @return SecurityConfiguration |
||
84 | * @category Security-Critical |
||
85 | */ |
||
86 | public function asCheckUserData() |
||
99 | |||
100 | /** |
||
101 | * Returns a pre-built security configuration for a public page. |
||
102 | * |
||
103 | * @category Security-Critical |
||
104 | * @return SecurityConfiguration |
||
105 | */ |
||
106 | View Code Duplication | public function asPublicPage() |
|
123 | |||
124 | /** |
||
125 | * Special case for zoom page private data. |
||
126 | * |
||
127 | * This will only return true if you are either a checkuser or a tool admin, taking special note of disabled |
||
128 | * accounts which happen to be check users |
||
129 | * |
||
130 | * @return SecurityConfiguration |
||
131 | */ |
||
132 | View Code Duplication | public function asGeneralPrivateDataAccess() |
|
150 | |||
151 | /** |
||
152 | * @category Security-Critical |
||
153 | * @return SecurityConfiguration |
||
154 | */ |
||
155 | public function asNone() |
||
161 | } |
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.