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 |
||
19 | class BridgeChronos implements BridgeInterface |
||
20 | { |
||
21 | const CACHE_TIME_JOB_LIST = 60; |
||
22 | |||
23 | const CACHE_KEY_JOB_LIST = 'jobs.list'; |
||
24 | |||
25 | const API_CALL_ADD = 'addingJob'; |
||
26 | |||
27 | const API_CALL_UPDATE = 'updatingJob'; |
||
28 | |||
29 | /** |
||
30 | * @var ApiClientInterface |
||
31 | */ |
||
32 | private $oApiClient; |
||
33 | |||
34 | /** |
||
35 | * @var CacheInterface |
||
36 | */ |
||
37 | private $oCache; |
||
38 | |||
39 | /** |
||
40 | * @var JobValidatorServiceInterface |
||
41 | */ |
||
42 | private $oJobEntityValidatorService; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $bCacheHasToDelete = false; |
||
48 | |||
49 | /** |
||
50 | * @var LoggerInterface |
||
51 | */ |
||
52 | private $oLogger; |
||
53 | |||
54 | /** |
||
55 | * @param \Chapi\Component\RemoteClients\ApiClientInterface $oApiClient |
||
56 | * @param CacheInterface $oCache |
||
57 | * @param JobValidatorServiceInterface $oJobEntityValidatorService |
||
58 | * @param LoggerInterface $oLogger |
||
59 | */ |
||
60 | 8 | View Code Duplication | public function __construct( |
72 | |||
73 | /** |
||
74 | * delete cache job.list if a job was removed |
||
75 | */ |
||
76 | 8 | public function __destruct() |
|
77 | { |
||
78 | 8 | if ($this->bCacheHasToDelete) |
|
79 | { |
||
80 | 3 | $this->oCache->delete(self::CACHE_KEY_JOB_LIST); |
|
81 | } |
||
82 | 8 | } |
|
83 | |||
84 | |||
85 | /** |
||
86 | * @return ChronosJobEntity[] |
||
87 | */ |
||
88 | 2 | public function getJobs() |
|
89 | { |
||
90 | 2 | $_aReturn = []; |
|
91 | 2 | $_aJobList = $this->getJobList(); |
|
92 | |||
93 | 2 | if (!empty($_aJobList)) |
|
94 | { |
||
95 | // prepare return value |
||
96 | 2 | foreach ($_aJobList as $_aJobData) |
|
97 | { |
||
98 | 2 | $_aReturn[] = new ChronosJobEntity($_aJobData); |
|
99 | } |
||
100 | } |
||
101 | |||
102 | 2 | return $_aReturn; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param ChronosJobEntity|JobEntityInterface $oJobEntity |
||
107 | * @return bool |
||
108 | */ |
||
109 | 2 | public function addJob(JobEntityInterface $oJobEntity) |
|
113 | |||
114 | /** |
||
115 | * @param ChronosJobEntity|JobEntityInterface $oJobEntity |
||
116 | * @return bool |
||
117 | */ |
||
118 | 2 | public function updateJob(JobEntityInterface $oJobEntity) |
|
122 | |||
123 | |||
124 | /** |
||
125 | * @param ChronosJobEntity|JobEntityInterface $oJobEntity |
||
126 | * @return bool |
||
127 | */ |
||
128 | 2 | View Code Duplication | public function removeJob(JobEntityInterface $oJobEntity) |
138 | |||
139 | /** |
||
140 | * @param JobEntityInterface $oJobEntity |
||
141 | * @return bool |
||
142 | */ |
||
143 | 4 | private function hasValidate(JobEntityInterface $oJobEntity) |
|
167 | |||
168 | /** |
||
169 | * @return array |
||
170 | */ |
||
171 | 2 | private function getJobList() |
|
190 | |||
191 | /** |
||
192 | * @param string $sApiMethod |
||
193 | * @param JobEntityInterface $oJobEntity |
||
194 | * @return bool |
||
195 | */ |
||
196 | 4 | private function hasAddOrUpdateJob($sApiMethod, JobEntityInterface $oJobEntity) |
|
209 | } |
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.