| Total Complexity | 59 |
| Total Lines | 339 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LogicalProcess 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.
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 LogicalProcess, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class LogicalProcess extends Base |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * ************************************************ |
||
| 19 | * ************** Operation Function ************** |
||
| 20 | * ************************************************ |
||
| 21 | */ |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Union one or more time periods |
||
| 25 | * |
||
| 26 | * 1. Sort and merge one or more time periods with contacts |
||
| 27 | * 2. TimePeriodHelper::union($timePeriods1, $timePeriods2, $timePeriods3, ......); |
||
| 28 | * |
||
| 29 | * @param array $timePeriods |
||
| 30 | * @return array |
||
| 31 | */ |
||
| 32 | public static function union() |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Computes the difference of time periods |
||
| 62 | * |
||
| 63 | * 1. Compares $timePeriods1 against $timePeriods2 and returns the values in $timePeriods1 that are not present in $timePeriods2. |
||
| 64 | * 2. e.g. TimePeriodHelper::diff($timePeriods1, $timePeriods2); |
||
| 65 | * 3. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
||
| 66 | * |
||
| 67 | * @param array $timePeriods1 |
||
| 68 | * @param array $timePeriods2 |
||
| 69 | * @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public static function diff(array $timePeriods1, array $timePeriods2, $sortOut = 'default') |
||
| 73 | { |
||
| 74 | /*** Arguments prepare ***/ |
||
| 75 | // Subject or pattern is empty, do nothing |
||
| 76 | if (empty($timePeriods1) || empty($timePeriods2)) { |
||
| 77 | return $timePeriods1; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Data sorting out |
||
| 81 | self::dataSortOut($sortOut, $timePeriods1, $timePeriods2); |
||
| 82 | |||
| 83 | $opt = []; |
||
| 84 | foreach ($timePeriods1 as $k1 => $ori) { |
||
| 85 | foreach ($timePeriods2 as $ko => $sub) { |
||
| 86 | if ($sub[1] <= $ori[0]) { |
||
| 87 | // No overlap && Passed: --sub0--sub1--ori0--ori1-- |
||
| 88 | unset($timePeriods2[$ko]); |
||
| 89 | continue; |
||
| 90 | } elseif ($ori[1] <= $sub[0]) { |
||
| 91 | // No overlap: --ori0--ori1--sub0--sub1-- |
||
| 92 | continue; |
||
| 93 | } elseif ($sub[0] <= $ori[0] && $ori[1] <= $sub[1]) { |
||
| 94 | // Subtract all: --sub0--ori0--ori1--sub1-- |
||
| 95 | $ori = []; |
||
| 96 | break; |
||
| 97 | } elseif ($ori[0] < $sub[0] && $sub[1] < $ori[1]) { |
||
| 98 | // Delete internal: --ori0--sub0--sub1--ori1-- |
||
| 99 | $opt[] = [$ori[0], $sub[0]]; |
||
| 100 | $ori = [$sub[1], $ori[1]]; |
||
| 101 | //} elseif ($sub[0] <= $ori[0] && $sub[1] <= $ori[1]) { // Complete condition |
||
| 102 | } elseif ($sub[0] <= $ori[0]) { // Equivalent condition |
||
| 103 | // Delete overlap: --sub0--ori0--sub1--ori1-- |
||
| 104 | $ori = [$sub[1], $ori[1]]; |
||
| 105 | //} elseif ($ori[0] <= $sub[0] && $ori[1] <= $sub[1]) { // Complete condition |
||
| 106 | //} elseif ($ori[1] <= $sub[1]) { // Equivalent condition |
||
| 107 | } else { // Equivalent condition |
||
| 108 | // Delete overlap: --ori0--sub0--ori1--sub1-- |
||
| 109 | $ori = [$ori[0], $sub[0]]; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | // All No overlap |
||
| 114 | if (!empty($ori)) { |
||
| 115 | $opt[] = $ori; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | return $opt; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Computes the intersection of time periods |
||
| 124 | * |
||
| 125 | * 1. e.g. TimePeriodHelper::intersect($timePeriods1, $timePeriods2); |
||
| 126 | * 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct. |
||
| 127 | * |
||
| 128 | * @param array $timePeriods1 |
||
| 129 | * @param array $timePeriods2 |
||
| 130 | * @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | public static function intersect(array $timePeriods1, array $timePeriods2, $sortOut = 'default') |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Time period is overlap |
||
| 181 | * |
||
| 182 | * 1. Determine if there is overlap between the two time periods |
||
| 183 | * 2. Only when there is no intersection, no data is needed. |
||
| 184 | * 3. Logic is similar to intersect. |
||
| 185 | * |
||
| 186 | * @param array $timePeriods1 |
||
| 187 | * @param array $timePeriods2 |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public static function isOverlap(array $timePeriods1, array $timePeriods2) |
||
| 191 | { |
||
| 192 | // Subject or pattern is empty, do nothing |
||
| 193 | if (empty($timePeriods1) || empty($timePeriods2)) { |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | |||
| 197 | foreach ($timePeriods1 as $k1 => $ori) { |
||
| 198 | foreach ($timePeriods2 as $ko => $sub) { |
||
| 199 | if ($sub[1] <= $ori[0]) { |
||
| 200 | // No overlap && Passed: --sub0--sub1--ori0--ori1-- |
||
| 201 | unset($timePeriods2[$ko]); |
||
| 202 | continue; |
||
| 203 | } elseif ($ori[1] <= $sub[0]) { |
||
| 204 | // No overlap: --ori0--ori1--sub0--sub1-- |
||
| 205 | continue; |
||
| 206 | } else { |
||
| 207 | // --sub0--ori0--ori1--sub1-- |
||
| 208 | // --ori0--sub0--sub1--ori1-- |
||
| 209 | // --sub0--ori0--sub1--ori1-- |
||
| 210 | // --ori0--sub0--ori1--sub1-- |
||
| 211 | return true; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * The time period is in contact with the specified time (time period) |
||
| 221 | * |
||
| 222 | * @param array $timePeriods |
||
| 223 | * @param string $sDateTime |
||
| 224 | * @param string $eDateTime |
||
| 225 | * @param bool|string $sortOut |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public static function contact(array $timePeriods, $sDateTime, $eDateTime = null, $sortOut = 'default') |
||
| 229 | { |
||
| 230 | // Subject is empty, do nothing |
||
| 231 | if (empty($timePeriods)) { |
||
| 232 | return []; |
||
| 233 | } |
||
| 234 | |||
| 235 | // Data sorting out |
||
| 236 | self::dataSortOut($sortOut, $timePeriods); |
||
| 237 | // Set $eDateTime |
||
| 238 | $eDateTime = $eDateTime ?: $sDateTime; |
||
| 239 | $sTime = min($sDateTime, $eDateTime); |
||
| 240 | $eTime = max($sDateTime, $eDateTime); |
||
| 241 | |||
| 242 | // Strip: No overlap && Passed: --$sTime--$eTime--$tp0--$tp1-- |
||
| 243 | $timePeriods = array_filter($timePeriods, function ($tp) use ($sTime, $eTime) { |
||
| 244 | return $eTime <= $tp[0] && $sTime < $tp[0] ? false : true; |
||
| 245 | }); |
||
| 246 | |||
| 247 | // Strip: No overlap: --$tp0--$tp1--$sTime--$eTime-- |
||
| 248 | $timePeriods = array_filter($timePeriods, function ($tp) use ($sTime, $eTime) { |
||
|
|
|||
| 249 | return $tp[1] <= $sTime ? false : true; |
||
| 250 | }); |
||
| 251 | |||
| 252 | return array_values($timePeriods); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Time period greater than the specified time |
||
| 257 | * |
||
| 258 | * @param array $timePeriods |
||
| 259 | * @param string $refDatetime Specified time to compare against |
||
| 260 | * @param bool $fullTimePeriod Get only the full time period |
||
| 261 | * @param bool|string $sortOut |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public static function greaterThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default') |
||
| 265 | { |
||
| 266 | // Subject is empty, do nothing |
||
| 267 | if (empty($timePeriods)) { |
||
| 268 | return []; |
||
| 269 | } |
||
| 270 | |||
| 271 | // Data sorting out |
||
| 272 | self::dataSortOut($sortOut, $timePeriods); |
||
| 273 | |||
| 274 | // Get Contact time periods |
||
| 275 | $opt = []; |
||
| 276 | foreach ($timePeriods as $k => $tp) { |
||
| 277 | if ($fullTimePeriod) { |
||
| 278 | // Time period is intact |
||
| 279 | if ($tp[0] >= $refDatetime) { |
||
| 280 | $opt[] = $tp; |
||
| 281 | } |
||
| 282 | } else { |
||
| 283 | // Time period not intact |
||
| 284 | if ($tp[1] > $refDatetime) { |
||
| 285 | $opt[] = $tp; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | return $opt; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Time period less than the specified time |
||
| 295 | * |
||
| 296 | * @param array $timePeriods |
||
| 297 | * @param string $refDatetime Specified time to compare against |
||
| 298 | * @param bool $fullTimePeriod Get only the full time period |
||
| 299 | * @param bool|string $sortOut |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public static function lessThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default') |
||
| 303 | { |
||
| 304 | // Subject is empty, do nothing |
||
| 305 | if (empty($timePeriods)) { |
||
| 306 | return []; |
||
| 307 | } |
||
| 308 | |||
| 309 | // Data sorting out |
||
| 310 | self::dataSortOut($sortOut, $timePeriods); |
||
| 311 | |||
| 312 | // Get Contact time periods |
||
| 313 | $opt = []; |
||
| 314 | foreach ($timePeriods as $k => $tp) { |
||
| 315 | if ($fullTimePeriod) { |
||
| 316 | // Time period is intact |
||
| 317 | if ($tp[1] <= $refDatetime) { |
||
| 318 | $opt[] = $tp; |
||
| 319 | } |
||
| 320 | } else { |
||
| 321 | // Time period not intact |
||
| 322 | if ($tp[0] < $refDatetime) { |
||
| 323 | $opt[] = $tp; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | return $opt; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * ******************************************** |
||
| 333 | * ************** Tools Function ************** |
||
| 334 | * ******************************************** |
||
| 335 | */ |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Data sorting out |
||
| 339 | * |
||
| 340 | * @param bool|string $sortOut |
||
| 341 | * @param array $timePeriods1 |
||
| 342 | * @param array|null $timePeriods2 |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | public static function dataSortOut(&$sortOut, &$timePeriods1, &$timePeriods2 = null) |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 |
This check looks for imports that have been defined, but are not used in the scope.