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 |
||
33 | trait ClientPoolerTrait |
||
34 | { |
||
35 | private $session; |
||
36 | |||
37 | /** |
||
38 | * getPoolerType |
||
39 | * |
||
40 | * @see ClientPoolerInterface |
||
41 | */ |
||
42 | abstract public function getPoolerType(); |
||
43 | |||
44 | /** |
||
45 | * createClient |
||
46 | * |
||
47 | * Create a new client. |
||
48 | * |
||
49 | * @param string $identifier |
||
50 | * @return Client |
||
51 | */ |
||
52 | abstract protected function createClient($identifier); |
||
53 | |||
54 | /** |
||
55 | * register |
||
56 | * |
||
57 | * @see ClientPoolerInterface |
||
58 | */ |
||
59 | public function register(Session $session) |
||
65 | |||
66 | /** |
||
67 | * getClient |
||
68 | * |
||
69 | * Basic getClient method. |
||
70 | * |
||
71 | * @param string $identifier |
||
72 | * @return Client |
||
73 | * @see ClientInterface |
||
74 | */ |
||
75 | public function getClient($identifier) |
||
86 | |||
87 | /** |
||
88 | * getClientFromPool |
||
89 | * |
||
90 | * How the pooler fetch a client from the pool. |
||
91 | * |
||
92 | * @param string $identifier |
||
93 | * @return Client|null |
||
94 | */ |
||
95 | protected function getClientFromPool($identifier) |
||
102 | |||
103 | /** |
||
104 | * getSession |
||
105 | * |
||
106 | * Check if the session is set and return it. |
||
107 | * |
||
108 | * @return Session |
||
109 | * @throws FoundationException |
||
110 | */ |
||
111 | View Code Duplication | protected function getSession() |
|
124 | } |
||
125 |
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.