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 |
||
34 | 1 | final class Repository implements IRepository |
|
35 | { |
||
36 | /** |
||
37 | * Implement nette smart magic |
||
38 | */ |
||
39 | 1 | use Nette\SmartObject; |
|
40 | |||
41 | /** |
||
42 | * @var WebSocketsClients\IStorage |
||
43 | */ |
||
44 | private $storage; |
||
45 | |||
46 | /** |
||
47 | * @param WebSocketsClients\IStorage $storage |
||
48 | */ |
||
49 | public function __construct(WebSocketsClients\IStorage $storage) |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getUser(WebSocketsEntities\Clients\IClient $client) |
||
66 | |||
67 | /** |
||
68 | * @param string $username |
||
69 | * |
||
70 | * @return array|bool |
||
71 | */ |
||
72 | View Code Duplication | public function findByUsername(string $username) |
|
92 | |||
93 | /** |
||
94 | * @param mixed $userId |
||
95 | * |
||
96 | * @return array|bool |
||
97 | */ |
||
98 | View Code Duplication | public function findById($userId) |
|
118 | |||
119 | /** |
||
120 | * @param bool $anonymous |
||
121 | * |
||
122 | * @return array|bool |
||
123 | */ |
||
124 | public function findAll(bool $anonymous = FALSE) |
||
144 | |||
145 | /** |
||
146 | * @param array $roles |
||
147 | * |
||
148 | * @return array|bool |
||
149 | */ |
||
150 | public function findByRoles(array $roles) |
||
172 | } |
||
173 |
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.