| Total Complexity | 43 |
| Total Lines | 261 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Generator extends GeneratorAbstract |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * creates class for generator |
||
| 11 | * |
||
| 12 | * @param array $replacement |
||
| 13 | * @return Generator |
||
| 14 | * |
||
| 15 | * @throws FileNotFoundException |
||
| 16 | */ |
||
| 17 | public function createClass($replacement=array()) |
||
| 18 | { |
||
| 19 | if(file_exists($this->path)){ |
||
| 20 | $content = $this->fileSystem->get($this->getStubFile()); |
||
| 21 | $this->loaded['createClass'] = true; |
||
| 22 | |||
| 23 | if($this->fileSystem->put($this->file,$content)!==FALSE){ |
||
| 24 | $this->replaceFileContent($replacement,$this->file); |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | return $this; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * creates class document for generator |
||
| 33 | * |
||
| 34 | * @param array $documents |
||
| 35 | * |
||
| 36 | * @throws FileNotFoundException |
||
| 37 | */ |
||
| 38 | public function createClassDocument($documents=array()) |
||
| 39 | { |
||
| 40 | if(isset($this->loaded['createClass']) && count($documents)){ |
||
| 41 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassDocument')); |
||
| 42 | eval("?>$content"); |
||
|
|
|||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * create class extend object for generator |
||
| 48 | * |
||
| 49 | * @param $namespace |
||
| 50 | * @param $alias |
||
| 51 | * |
||
| 52 | * @throws FileNotFoundException |
||
| 53 | */ |
||
| 54 | public function createClassExtend($namespace, $alias) |
||
| 55 | { |
||
| 56 | if(!is_null($namespace) && !is_null($alias) && !preg_match('@extends@',$this->getClassString())){ |
||
| 57 | if(preg_match('@class\s(.*?).*@',$this->getClassString(),$class)){ |
||
| 58 | $statements = explode(' ',$class[0]); |
||
| 59 | $className = $statements[1]; |
||
| 60 | |||
| 61 | if(count($statements)>2){ |
||
| 62 | $implements = implode(' ',array_slice($statements,2)); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | if(isset($className)){ |
||
| 67 | $this->createClassUse([ |
||
| 68 | $namespace |
||
| 69 | ]); |
||
| 70 | |||
| 71 | if(!isset($implements)){ |
||
| 72 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassExtend')); |
||
| 73 | eval("?>$content"); |
||
| 74 | } |
||
| 75 | else{ |
||
| 76 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassExtendWithImplements')); |
||
| 77 | eval("?>$content"); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * create class interface object for generator |
||
| 85 | * |
||
| 86 | * @param array $implements |
||
| 87 | * |
||
| 88 | * @throws FileNotFoundException |
||
| 89 | */ |
||
| 90 | public function createClassImplements($implements=array()) |
||
| 91 | { |
||
| 92 | if(!is_null($this->getClassString())){ |
||
| 93 | |||
| 94 | $implementList = []; |
||
| 95 | $implementUseList = []; |
||
| 96 | |||
| 97 | foreach($implements as $namespace=>$alias) { |
||
| 98 | $implementUseList[] = $namespace; |
||
| 99 | $implementList[] = $alias; |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->createClassUse($implementUseList); |
||
| 103 | |||
| 104 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassImplements')); |
||
| 105 | eval("?>$content"); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * creates class property for generator |
||
| 111 | * |
||
| 112 | * @param array $properties |
||
| 113 | * @param bool $loadedMethod |
||
| 114 | * |
||
| 115 | * @throws FileNotFoundException |
||
| 116 | */ |
||
| 117 | public function createClassProperty($properties=array(),$loadedMethod=false) |
||
| 118 | { |
||
| 119 | if(is_null($this->classProperties)){ |
||
| 120 | $this->classProperties = $properties; |
||
| 121 | } |
||
| 122 | |||
| 123 | if(isset($this->loaded['createMethod'])){ |
||
| 124 | $this->classProperties = $properties; |
||
| 125 | $loadedMethod = true; |
||
| 126 | } |
||
| 127 | |||
| 128 | if($loadedMethod){ |
||
| 129 | foreach ($this->classProperties as $property) { |
||
| 130 | if(!preg_match('@'.$this->regexEscape($property).'@',$this->fileSystem->get($this->file))){ |
||
| 131 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassProperty')); |
||
| 132 | eval("?>$content"); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | if(isset($this->loaded['createClassPropertyDocument'])){ |
||
| 137 | $this->createClassPropertyDocument($this->loaded['createClassPropertyDocument']); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * creates class property document for generator |
||
| 144 | * |
||
| 145 | * @param array $properties |
||
| 146 | * |
||
| 147 | * @throws FileNotFoundException |
||
| 148 | */ |
||
| 149 | public function createClassPropertyDocument($properties=array()) |
||
| 150 | { |
||
| 151 | $this->loaded['createClassPropertyDocument'] = $properties; |
||
| 152 | |||
| 153 | foreach ($properties as $property=>$documents){ |
||
| 154 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassPropertyDocument')); |
||
| 155 | eval("?>$content"); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * creates class trait for generator |
||
| 161 | * |
||
| 162 | * @param $trait |
||
| 163 | * |
||
| 164 | * @throws FileNotFoundException |
||
| 165 | */ |
||
| 166 | public function createClassTrait($trait) |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * creates class use statements for generator |
||
| 176 | * |
||
| 177 | * @param array $uses |
||
| 178 | * |
||
| 179 | * @throws FileNotFoundException |
||
| 180 | */ |
||
| 181 | public function createClassUse($uses=array()) |
||
| 182 | { |
||
| 183 | if(!is_null($this->getClassString()) && count($uses)){ |
||
| 184 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassUse')); |
||
| 185 | eval("?>$content"); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * creates method for generator |
||
| 191 | * |
||
| 192 | * @param array $methods |
||
| 193 | * |
||
| 194 | * @throws FileNotFoundException |
||
| 195 | */ |
||
| 196 | public function createMethod($methods=array()) |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * accessible properties method for generator |
||
| 209 | * |
||
| 210 | * @param array $methods |
||
| 211 | * @return void|mixed |
||
| 212 | * |
||
| 213 | * @throws FileNotFoundException |
||
| 214 | */ |
||
| 215 | public function createMethodAccessibleProperty($methods=array()) |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * creates method for generator |
||
| 226 | * |
||
| 227 | * @param array $methods |
||
| 228 | * |
||
| 229 | * @throws FileNotFoundException |
||
| 230 | */ |
||
| 231 | public function createMethodBody($methods=array()) |
||
| 232 | { |
||
| 233 | foreach ($methods as $method=>$body){ |
||
| 234 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodBody')); |
||
| 235 | eval("?>$content"); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * creates method for generator |
||
| 241 | * |
||
| 242 | * @param array $methods |
||
| 243 | * |
||
| 244 | * @throws FileNotFoundException |
||
| 245 | */ |
||
| 246 | public function createMethodDocument($methods=array()) |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * accessible properties method for generator |
||
| 256 | * |
||
| 257 | * @param array $methods |
||
| 258 | * @return void|mixed |
||
| 259 | * |
||
| 260 | * @throws FileNotFoundException |
||
| 261 | */ |
||
| 262 | public function createMethodParameters($methods=array()) |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | } |
||
| 272 |