| Conditions | 7 |
| Paths | 17 |
| Total Lines | 91 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 145 | public function generateEntity($table) |
||
| 146 | { |
||
| 147 | if(in_array($table,$this->showTables()) || in_array($table = strtolower($table),$this->showTables())){ |
||
| 148 | |||
| 149 | $entityDirectory = path()->model().''.DIRECTORY_SEPARATOR.'Entity'; |
||
| 150 | |||
| 151 | if(!file_exists($entityDirectory)){ |
||
| 152 | files()->makeDirectory($entityDirectory); |
||
| 153 | } |
||
| 154 | |||
| 155 | $columns = $this->showColumnsFrom($table); |
||
| 156 | |||
| 157 | $list = []; |
||
| 158 | |||
| 159 | foreach ($columns as $column) { |
||
| 160 | $list[] = $column['Field']; |
||
| 161 | } |
||
| 162 | |||
| 163 | if(!file_exists($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table))){ |
||
| 164 | $generator = new Generator($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table),$table.''); |
||
| 165 | |||
| 166 | $generator->createClass(); |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | $abstractClassPath = $entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table).''.DIRECTORY_SEPARATOR.'Entity'; |
||
| 171 | $abstractNamespace = Utils::getNamespace($abstractClassPath.''.DIRECTORY_SEPARATOR.''.ucfirst($table).'Abstract'); |
||
| 172 | |||
| 173 | $generator->createClassExtend($abstractNamespace,ucfirst($table).'Abstract'); |
||
| 174 | |||
| 175 | $generator = new Generator($abstractClassPath,$table.'Abstract'); |
||
| 176 | |||
| 177 | $generator->createClass(); |
||
| 178 | |||
| 179 | $method =array_merge([ |
||
| 180 | '__construct' |
||
| 181 | ],array_merge($list,['__get'])); |
||
| 182 | |||
| 183 | $generator->createMethod($method); |
||
| 184 | |||
| 185 | $generator->createMethodParameters([ |
||
| 186 | '__construct' => '$query', |
||
| 187 | '__get' => '$name' |
||
| 188 | ]); |
||
| 189 | |||
| 190 | $methodBodyList = []; |
||
| 191 | $createMethodAccessibleProperty = []; |
||
| 192 | $createMethodDocument = []; |
||
| 193 | $createClassDocument = []; |
||
| 194 | |||
| 195 | foreach ($list as $item) { |
||
| 196 | $methodBodyList[$item] = 'return self::$query->'.$item.';'; |
||
| 197 | $createClassDocument[] = '@property $this '.$item; |
||
| 198 | $createMethodAccessibleProperty[$item] = 'protected static'; |
||
| 199 | $createMethodDocument[$item] = [ |
||
| 200 | '@return mixed' |
||
| 201 | ]; |
||
| 202 | } |
||
| 203 | |||
| 204 | $generator->createClassDocument($createClassDocument); |
||
| 205 | |||
| 206 | $generator->createMethodDocument(array_merge($createMethodDocument,[ |
||
| 207 | '__construct' => [ |
||
| 208 | ''.$table.' constructor.', |
||
| 209 | '@param null|object $query' |
||
| 210 | ], |
||
| 211 | '__get' =>[ |
||
| 212 | 'access entity object with magic method', |
||
| 213 | '', |
||
| 214 | '@param $name', |
||
| 215 | '@return mixed' |
||
| 216 | ] |
||
| 217 | ])); |
||
| 218 | |||
| 219 | $createMethodBody = array_merge([ |
||
| 220 | '__construct' => 'self::$query = $query;', |
||
| 221 | '__get' => 'return static::{$name}();' |
||
| 222 | ],$methodBodyList); |
||
| 223 | |||
| 224 | $generator->createMethodBody($createMethodBody); |
||
| 225 | |||
| 226 | $generator->createMethodAccessibleProperty($createMethodAccessibleProperty); |
||
| 227 | |||
| 228 | |||
| 229 | $generator->createClassProperty([ |
||
| 230 | 'protected static $query;' |
||
| 231 | ]); |
||
| 232 | |||
| 233 | $generator->createClassPropertyDocument([ |
||
| 234 | 'protected static $query' => [ |
||
| 235 | '@var object|null' |
||
| 236 | ] |
||
| 244 |