Complex classes like YaEtl 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 YaEtl, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class YaEtl extends NodalFlow |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * The total amount of record to fetch, in case |
||
| 30 | * there is a limit set |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $extractLimit; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $aggregateNodes = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $aggregateNodeIdx = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $stats = [ |
||
| 50 | 'start' => null, |
||
| 51 | 'end' => null, |
||
| 52 | 'duration' => null, |
||
| 53 | 'mib' => null, |
||
| 54 | 'report' => '', |
||
| 55 | 'num_extract' => 0, |
||
| 56 | 'num_extractor' => 0, |
||
| 57 | 'num_join' => 0, |
||
| 58 | 'num_joiner' => 0, |
||
| 59 | 'num_merge' => 0, |
||
| 60 | 'num_records' => 0, |
||
| 61 | 'num_transform' => 0, |
||
| 62 | 'num_transformer' => 0, |
||
| 63 | 'num_branch' => 0, |
||
| 64 | 'num_load' => 0, |
||
| 65 | 'num_loader' => 0, |
||
| 66 | 'num_flush' => 0, |
||
| 67 | 'invocations' => [], |
||
| 68 | 'nodes' => [], |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $reverseAggregateTable = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param int $recordLimit |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | public function setExtractLimit($recordLimit) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param ExtractorInterface $extractor |
||
| 90 | * @param null|ExtractorInterface $aggregateWith Use the extractore instance you want to aggregate with |
||
| 91 | * |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function from(ExtractorInterface $extractor, ExtractorInterface $aggregateWith = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param NodeInterface $node |
||
| 110 | * |
||
| 111 | * @throws Exception |
||
| 112 | */ |
||
| 113 | public function add(NodeInterface $node) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param JoinableInterface $extractor |
||
| 120 | * @param JoinableInterface $joinFrom |
||
| 121 | * @param OnClauseInterface $onClause |
||
| 122 | * |
||
| 123 | * @return $this |
||
| 124 | */ |
||
| 125 | public function join(JoinableInterface $extractor, JoinableInterface $joinFrom, OnClauseInterface $onClause) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param TransformerInterface $transformer |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function transform(TransformerInterface $transformer) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param LoaderInterface $loader |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function to(LoaderInterface $loader) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @staticvar type $flowHashes |
||
| 168 | * |
||
| 169 | * @param YaEtl $flow |
||
| 170 | * @param type $isAReturningVal |
||
|
|
|||
| 171 | * |
||
| 172 | * @throws \Exception |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function branch(YaEtl $flow, $isAReturningVal = false) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Triggered right after the flow stops |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function flowEnd() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * kiss method to expose basic stats |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function getStats() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param ExtractorInterface $extractor |
||
| 253 | * @param ExtractorInterface $aggregateWith |
||
| 254 | * |
||
| 255 | * @throws \Exception |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | protected function aggregateTo(ExtractorInterface $extractor, ExtractorInterface $aggregateWith) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param array $stats |
||
| 290 | * |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | protected function collectNodeStats(array &$stats) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Replaces a node with another one |
||
| 328 | * |
||
| 329 | * @param type $nodeIdx |
||
| 330 | * @param NodeInterface $node |
||
| 331 | * |
||
| 332 | * @throws \InvalidArgumentException |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | protected function replace($nodeIdx, NodeInterface $node) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param array $stats |
||
| 364 | * |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | protected function processStats($stats) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * It could lead to really tricky situation if we where to |
||
| 387 | * allow multiple instances of the same node. It's obviously |
||
| 388 | * wrong with an Extractor, but even a Transformer could |
||
| 389 | * create dark corner cases. |
||
| 390 | * |
||
| 391 | * @param NodeInterface $node |
||
| 392 | * |
||
| 393 | * @throws \Exception |
||
| 394 | * |
||
| 395 | * @return $this |
||
| 396 | */ |
||
| 397 | protected function enforceNodeInstanceUnicity(NodeInterface $node) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $hash |
||
| 408 | * @param array $nodeMap |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | protected function findNodeHashInMap($hash, $nodeMap) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * calls each WorkFlow's loaders flush method |
||
| 432 | * |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | protected function flush() |
||
| 446 | } |
||
| 447 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.