Complex classes like SchedulingViewCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchedulingViewCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class SchedulingViewCommand extends AbstractCommand |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var JobStatsServiceInterface $oJobStatsService |
||
| 26 | */ |
||
| 27 | private $oJobStatsService; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var JobDependencyServiceInterface $oJobDependencyService |
||
| 31 | */ |
||
| 32 | private $oJobDependencyService; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var JobRepositoryInterface $oJobRepositoryChronos |
||
| 36 | */ |
||
| 37 | private $oJobRepositoryChronos; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var DatePeriodFactoryInterface $oDatePeriodFactory |
||
| 41 | */ |
||
| 42 | private $oDatePeriodFactory; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Configures the current command. |
||
| 46 | */ |
||
| 47 | 3 | protected function configure() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return int |
||
| 58 | */ |
||
| 59 | 3 | protected function process() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @param int $iStartTime |
||
| 89 | * @param int $iEndTime |
||
| 90 | */ |
||
| 91 | 3 | private function printTimeLineTable($iStartTime, $iEndTime) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param Table $oTable |
||
| 140 | * @param \DatePeriod $oDisplayPeriod |
||
| 141 | * @param \DatePeriod $oJobDatePeriod |
||
| 142 | * @param string $sParentJobName |
||
| 143 | * @param float $fParentJobRunTime |
||
| 144 | * @param int $iRowCount |
||
| 145 | * @param int $iCurrentChildLevel |
||
| 146 | */ |
||
| 147 | 3 | private function printChildJobs( |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param \DatePeriod $oDatePeriod |
||
| 194 | * @param \DatePeriod $oJobDatePeriod |
||
| 195 | * @param float $fRunSeconds |
||
| 196 | * @param boolean $bPrintTime |
||
| 197 | * @param int $iJobStartTimeDelay |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | 3 | private function getTimelineStr( |
|
| 201 | \DatePeriod $oDatePeriod, |
||
| 202 | \DatePeriod $oJobDatePeriod, |
||
| 203 | $fRunSeconds = 0.0, |
||
| 204 | $bPrintTime = true, |
||
| 205 | $iJobStartTimeDelay = 0 |
||
| 206 | ) |
||
| 207 | { |
||
| 208 | 3 | $_sTimeline = ''; |
|
| 209 | 3 | $_aStartTimes = $this->getJobStartTimesInPeriod($oJobDatePeriod, $iJobStartTimeDelay); |
|
| 210 | |||
| 211 | 3 | $_bJobStarted = false; |
|
| 212 | 3 | $_sSpacer = '-'; |
|
| 213 | |||
| 214 | 3 | $_iRunMinutes = ($fRunSeconds > 0) ? round($fRunSeconds / 60) : 0; |
|
| 215 | 3 | $_iPrintedRunMinutes = 0; |
|
| 216 | |||
| 217 | 3 | $_bHasToCloseFinalTag = false; |
|
| 218 | |||
| 219 | /** @var \DateTime $_oTime */ |
||
| 220 | 3 | foreach ($oDatePeriod as $_oTime) |
|
| 221 | { |
||
| 222 | 3 | $_bPrintJobEnd = false; |
|
| 223 | 3 | $_bPrintJobStart = false; |
|
| 224 | |||
| 225 | 3 | if (isset($_aStartTimes[$_oTime->format('YmdHi')])) |
|
| 226 | 3 | { |
|
| 227 | 3 | $_bJobStarted = true; |
|
| 228 | 3 | $_bPrintJobStart = true; |
|
| 229 | 3 | $_sSpacer = '='; |
|
| 230 | 3 | } |
|
| 231 | |||
| 232 | if ($_bJobStarted) |
||
| 233 | 3 | { |
|
| 234 | 3 | ++$_iPrintedRunMinutes; |
|
| 235 | |||
| 236 | 3 | if ($_iRunMinutes <= $_iPrintedRunMinutes) |
|
| 237 | 3 | { |
|
| 238 | 3 | $_bJobStarted = false; |
|
| 239 | 3 | $_bPrintJobEnd = true; |
|
| 240 | 3 | $_sSpacer = '-'; |
|
| 241 | 3 | $_iPrintedRunMinutes = 0; |
|
| 242 | 3 | } |
|
| 243 | 3 | } |
|
| 244 | |||
| 245 | 3 | $_iMod = ((int) $_oTime->format('i')) % 15; |
|
| 246 | |||
| 247 | 3 | if ($_iMod == 0) |
|
| 248 | 3 | { |
|
| 249 | if ($bPrintTime) |
||
| 250 | 3 | { |
|
| 251 | 3 | $_sTimeline .= $this->parseTimeLineStrMark($_bPrintJobStart, $_bPrintJobEnd, $_bHasToCloseFinalTag, $_oTime->format('H>i'), $_oTime->format('H:i')); |
|
| 252 | |||
| 253 | 3 | } |
|
| 254 | else |
||
| 255 | { |
||
| 256 | 3 | $_sTimeline .= $this->parseTimeLineStrMark($_bPrintJobStart, $_bPrintJobEnd, $_bHasToCloseFinalTag, str_repeat('>', 5), str_repeat($_sSpacer, 5)); |
|
| 257 | } |
||
| 258 | 3 | } |
|
| 259 | else |
||
| 260 | { |
||
| 261 | 3 | $_sTimeline .= $this->parseTimeLineStrMark($_bPrintJobStart, $_bPrintJobEnd, $_bHasToCloseFinalTag, '>', $_sSpacer); |
|
| 262 | } |
||
| 263 | 3 | } |
|
| 264 | |||
| 265 | // add final tag to the end if you runs longer than current timeframe |
||
| 266 | if ($_bHasToCloseFinalTag) |
||
| 267 | 3 | { |
|
| 268 | $_sTimeline .= '</comment>'; |
||
| 269 | } |
||
| 270 | |||
| 271 | 3 | return $_sTimeline; |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param bool $bPrintJobStart |
||
| 276 | * @param bool $bPrintJobEnd |
||
| 277 | * @param bool $bHasToCloseFinalTag |
||
| 278 | * @param string $sStartStopMark |
||
| 279 | * @param string $sSpacer |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 3 | private function parseTimeLineStrMark($bPrintJobStart, $bPrintJobEnd, &$bHasToCloseFinalTag, $sStartStopMark, $sSpacer) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param \DatePeriod $oJobDatePeriod |
||
| 308 | * @param int $iJobStartTimeDelay |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | 3 | private function getJobStartTimesInPeriod(\DatePeriod $oJobDatePeriod, $iJobStartTimeDelay = 0) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param JobEntity $oJobEntity |
||
| 331 | * @param int $iEndTime |
||
| 332 | * @return \DatePeriod |
||
| 333 | */ |
||
| 334 | 3 | private function createDatePeriodForJob(JobEntity $oJobEntity, $iEndTime) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $sDateTimeStart |
||
| 342 | * @param int $iTimestampStart |
||
| 343 | * @param string $sDateTimeEnd |
||
| 344 | * @param int $iTimestampEnd |
||
| 345 | * @param string $sDateInterval |
||
| 346 | * @return \DatePeriod |
||
| 347 | */ |
||
| 348 | 3 | private function createDatePeriod($sDateTimeStart = '', $iTimestampStart = 0, $sDateTimeEnd = '', $iTimestampEnd = 0, $sDateInterval = 'PT1M') |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @param int $iStartTime |
||
| 369 | * @param int $iEndTime |
||
| 370 | * @return JobEntity[] |
||
| 371 | */ |
||
| 372 | 3 | private function getJobsWhichShouldStartInPeriod($iStartTime, $iEndTime) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param \DatePeriod $oJobDatePeriod |
||
| 400 | * @param int $iStartTime |
||
| 401 | * @param int $iEndTime |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | 3 | private function isPeriodInTimeFrame(\DatePeriod $oJobDatePeriod, $iStartTime, $iEndTime) |
|
| 428 | } |