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 |
||
21 | class ChronosJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase |
||
22 | { |
||
23 | /** |
||
24 | * @var DatePeriodFactoryInterface |
||
25 | */ |
||
26 | private $oDatePeriodFactory; |
||
27 | |||
28 | /** |
||
29 | * @var LoggerInterface |
||
30 | */ |
||
31 | private $oLogger; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @param JobRepositoryInterface $oJobRepositoryLocalChronos |
||
36 | * @param JobRepositoryInterface $oJobRepositoryChronos |
||
37 | * @param DiffCompareInterface $oDiffCompare |
||
38 | * @param DatePeriodFactoryInterface $oDatePeriodFactory |
||
39 | * @param LoggerInterface $oLogger |
||
40 | */ |
||
41 | 7 | public function __construct( |
|
42 | JobRepositoryInterface $oJobRepositoryLocalChronos, |
||
43 | JobRepositoryInterface $oJobRepositoryChronos, |
||
44 | DiffCompareInterface $oDiffCompare, |
||
45 | DatePeriodFactoryInterface $oDatePeriodFactory, |
||
46 | LoggerInterface $oLogger |
||
47 | ) |
||
48 | { |
||
49 | 7 | $this->oLocalRepository = $oJobRepositoryLocalChronos; |
|
50 | 7 | $this->oRemoteRepository = $oJobRepositoryChronos; |
|
51 | 7 | $this->oDiffCompare = $oDiffCompare; |
|
52 | 7 | $this->oDatePeriodFactory = $oDatePeriodFactory; |
|
53 | 7 | $this->oLogger = $oLogger; |
|
54 | 7 | } |
|
55 | |||
56 | |||
57 | 5 | protected function preCompareModifications(JobEntityInterface &$oLocalJob, JobEntityInterface &$oRemoteJob) |
|
62 | |||
63 | |||
64 | protected function getEntitySetWithDefaults() |
||
68 | |||
69 | /** |
||
70 | * @param JobEntityInterface|ChronosJobEntity $oJobEntityA |
||
71 | * @param JobEntityInterface|ChronosJobEntity $oJobEntityB |
||
72 | * @return bool |
||
73 | */ |
||
74 | 2 | public function hasSameJobType(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB) |
|
75 | { |
||
76 | return ( |
||
77 | 2 | ($oJobEntityA->isSchedulingJob() && $oJobEntityB->isSchedulingJob()) |
|
78 | 2 | || ($oJobEntityA->isDependencyJob() && $oJobEntityB->isDependencyJob()) |
|
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param string $sProperty |
||
84 | * @param JobEntityInterface $oJobEntityA |
||
85 | * @param JobEntityInterface $oJobEntityB |
||
86 | * @return bool |
||
87 | */ |
||
88 | 5 | protected function isEntityEqual($sProperty, JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB) |
|
128 | |||
129 | /** |
||
130 | * @param ChronosJobEntity $oJobEntityA |
||
131 | * @param ChronosJobEntity $oJobEntityB |
||
132 | * @return bool |
||
133 | */ |
||
134 | 1 | private function isScheduleTimeZonePropertyIdentical(ChronosJobEntity $oJobEntityA, ChronosJobEntity $oJobEntityB) |
|
151 | |||
152 | /** |
||
153 | * @param ChronosJobEntity $oJobEntityA |
||
154 | * @param ChronosJobEntity $oJobEntityB |
||
155 | * @return bool |
||
156 | */ |
||
157 | 1 | private function isSchedulePropertyIdentical(ChronosJobEntity $oJobEntityA, ChronosJobEntity $oJobEntityB) |
|
222 | |||
223 | /** |
||
224 | * @param string $sIso8601String |
||
225 | * @param string $sTimeZone |
||
226 | * @return \DateTime |
||
227 | */ |
||
228 | 1 | private function createDateTimeObj($sIso8601String, $sTimeZone = '') |
|
244 | } |
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.