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 |
||
23 | class AirlockAuthenticationKeyUserProvider implements UserProviderInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var \Graviton\RestBundle\Model\ModelInterface |
||
27 | */ |
||
28 | private $documentModel; |
||
29 | |||
30 | /** |
||
31 | * @param \Graviton\RestBundle\Model\ModelInterface $contract contract to use as documentModel |
||
32 | */ |
||
33 | public function __construct(ModelInterface $contract) |
||
37 | |||
38 | /** |
||
39 | * Finds a contract based on the provided ApiKey. |
||
40 | * |
||
41 | * @param string $apiKey key from airlock |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | View Code Duplication | public function getUsernameForApiKey($apiKey) |
|
58 | |||
59 | /** |
||
60 | * Loads the user for the given username. |
||
61 | * |
||
62 | * This method must throw UsernameNotFoundException if the user is not |
||
63 | * found. |
||
64 | * |
||
65 | * @param string $contractId contract id we need a username for |
||
66 | * |
||
67 | * @return \Symfony\Component\Security\Core\User\UserInterface |
||
68 | * |
||
69 | * @see \Symfony\Component\Security\Core\Exception\UsernameNotFoundException |
||
70 | * |
||
71 | * @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException if the user is not found |
||
72 | */ |
||
73 | public function loadUserByUsername($contractId) |
||
87 | |||
88 | /** |
||
89 | * Refreshes the user for the account interface. |
||
90 | * |
||
91 | * It is up to the implementation to decide if the user data should be |
||
92 | * totally reloaded (e.g. from the database), or if the UserInterface |
||
93 | * object can just be merged into some internal array of users / identity |
||
94 | * map. |
||
95 | * |
||
96 | * @param \Symfony\Component\Security\Core\User\UserInterface $user user to refresh |
||
97 | * |
||
98 | * @return \Symfony\Component\Security\Core\User\UserInterface |
||
99 | * |
||
100 | * @throws \Symfony\Component\Security\Core\Exception\UnsupportedUserException if the account is not supported |
||
101 | */ |
||
102 | public function refreshUser(UserInterface $user) |
||
110 | |||
111 | /** |
||
112 | * Whether this provider supports the given user class. |
||
113 | * |
||
114 | * @param string $class class to check for support |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function supportsClass($class) |
||
122 | |||
123 | /** |
||
124 | * Decides the role set the provided contract has. |
||
125 | * |
||
126 | * @param Contract $contract provided contract |
||
127 | * |
||
128 | * @return string[] |
||
129 | */ |
||
130 | private function getContractRoles(Contract $contract) |
||
136 | } |
||
137 |
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.