| Total Complexity | 42 |
| Total Lines | 147 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TransformerGenerator 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 TransformerGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class TransformerGenerator extends Generator |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Get stub name. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $stub = 'transformer/transformer'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get root namespace. |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | public function getRootNamespace() |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Get generator path config node. |
||
| 40 | * |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public function getPathConfigNode() |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Get destination path for generated file. |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function getPath() |
||
| 54 | { |
||
| 55 | return $this->getBasePath() . |
||
| 56 | '/' . |
||
| 57 | parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . |
||
| 58 | '/' . |
||
| 59 | $this->getName() . |
||
| 60 | 'Transformer.php'; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get base path of destination file. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function getBasePath() |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get default Columns description. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getColumns() |
||
| 79 | { |
||
| 80 | $fields = $this->fields; |
||
| 81 | $result = ''; |
||
| 82 | if (!empty($fields)) { |
||
| 83 | foreach ($fields as $index => $field) { |
||
| 84 | $type = $this->getTypeFromField($field); |
||
| 85 | $result .= "\t\t\t'{$field['name']}' => ($type) ".'$model->'."{$field['name']},\r\n"; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | $result .= ''; |
||
| 89 | |||
| 90 | return $result; |
||
| 91 | } |
||
| 92 | |||
| 93 | private function getTypeFromField($filed) |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get array replacements. |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function getReplacements() |
||
| 165 | ] |
||
| 166 | ); |
||
| 169 |