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 $apiClient; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var CacheInterface |
||
| 36 | */ |
||
| 37 | private $cache; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var JobValidatorServiceInterface |
||
| 41 | */ |
||
| 42 | private $jobEntityValidatorService; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $cacheHasToDelete = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var LoggerInterface |
||
| 51 | */ |
||
| 52 | private $logger; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param \Chapi\Component\RemoteClients\ApiClientInterface $apiClient |
||
| 56 | * @param CacheInterface $cache |
||
| 57 | * @param JobValidatorServiceInterface $jobEntityValidatorService |
||
| 58 | * @param LoggerInterface $logger |
||
| 59 | */ |
||
| 60 | 8 | View Code Duplication | public function __construct( |
| 71 | |||
| 72 | /** |
||
| 73 | * delete cache job.list if a job was removed |
||
| 74 | */ |
||
| 75 | 8 | public function __destruct() |
|
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * @return ChronosJobEntity[] |
||
| 85 | */ |
||
| 86 | 2 | public function getJobs() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param ChronosJobEntity|JobEntityInterface $jobEntity |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | 2 | public function addJob(JobEntityInterface $jobEntity) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param ChronosJobEntity|JobEntityInterface $jobEntity |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 2 | public function updateJob(JobEntityInterface $jobEntity) |
|
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @param ChronosJobEntity|JobEntityInterface $jobEntity |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | 2 | View Code Duplication | public function removeJob(JobEntityInterface $jobEntity) |
| 133 | |||
| 134 | /** |
||
| 135 | * @param JobEntityInterface $jobEntity |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 4 | private function hasValidate(JobEntityInterface $jobEntity) |
|
| 139 | { |
||
| 140 | 4 | $invalidProperties = $this->jobEntityValidatorService->getInvalidProperties($jobEntity); |
|
| 141 | 4 | if (empty($invalidProperties)) { |
|
| 142 | 2 | return true; |
|
| 143 | } |
||
| 144 | |||
| 145 | 2 | $this->logger->warning( |
|
| 146 | 2 | sprintf( |
|
| 147 | 2 | "Can't update job '%s'", |
|
| 148 | 2 | $jobEntity->getKey() |
|
| 149 | ) |
||
| 150 | ); |
||
| 151 | 2 | $this->logger->warning( |
|
| 152 | 2 | sprintf( |
|
| 153 | 2 | "The following job entity properties are not valid:\n%s", |
|
| 154 | 2 | implode(', ', $invalidProperties) |
|
| 155 | ) |
||
| 156 | ); |
||
| 157 | |||
| 158 | 2 | return false; |
|
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 2 | private function getJobList() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $apiMethod |
||
| 184 | * @param JobEntityInterface $jobEntity |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 4 | private function hasAddOrUpdateJob($apiMethod, JobEntityInterface $jobEntity) |
|
| 198 | } |
||
| 199 |