| Conditions | 11 |
| Paths | 1024 |
| Total Lines | 137 |
| Code Lines | 68 |
| 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 |
||
| 41 | public function create(){
|
||
| 42 | |||
| 43 | $this->argument['file'] = $this->argument['model']; |
||
| 44 | |||
| 45 | if(!isset($this->argument['table'])){
|
||
| 46 | $this->argument['table'] = $this->argument['file'].'s'; |
||
| 47 | } |
||
| 48 | |||
| 49 | //lower case for table |
||
| 50 | $this->argument['table'] = strtolower($this->argument['table']); |
||
| 51 | |||
| 52 | $this->directory['modelDir'] = app()->path()->model(); |
||
| 53 | $this->directory['builderDir'] = $this->directory['modelDir'].'/Builder'; |
||
| 54 | $this->directory['builderAssistantDir'] = $this->directory['modelDir'].'/Builder/Assistant'; |
||
| 55 | $this->directory['contract'] = $this->directory['modelDir'].'/Contract'; |
||
| 56 | $this->directory['helper'] = $this->directory['modelDir'].'/Helper'; |
||
| 57 | |||
| 58 | $this->argument['modelNamespace'] = Utils::getNamespace($this->directory['modelDir']); |
||
| 59 | $this->argument['builderNamespace'] = Utils::getNamespace($this->directory['builderDir']); |
||
| 60 | $this->argument['builderAssistantNamespace'] = Utils::getNamespace($this->directory['builderAssistantDir']); |
||
| 61 | $this->argument['contractNamespace'] = Utils::getNamespace($this->directory['contract']); |
||
| 62 | |||
| 63 | //set project directory |
||
| 64 | $this->file->makeDirectory($this); |
||
| 65 | |||
| 66 | //model set |
||
| 67 | $this->touch['model/model'] = $this->directory['modelDir'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'.php'; |
||
| 68 | $this->touch['model/builder'] = $this->directory['builderDir'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'Builder.php'; |
||
| 69 | $this->touch['model/builderasistant'] = $this->directory['builderAssistantDir'].''.DIRECTORY_SEPARATOR.'Builder.php'; |
||
| 70 | $this->touch['model/contract'] = $this->directory['contract'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'Contract.php'; |
||
| 71 | |||
| 72 | if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Scope.php')){
|
||
| 73 | $this->touch['model/scope'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Scope.php'; |
||
| 74 | } |
||
| 75 | |||
| 76 | if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Event.php')){
|
||
| 77 | $this->touch['model/event'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Event.php'; |
||
| 78 | } |
||
| 79 | |||
| 80 | if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'TableChanges.php')){
|
||
| 81 | $this->touch['model/tablechanges'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'TableChanges.php'; |
||
| 82 | } |
||
| 83 | |||
| 84 | if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Constructor.php')){
|
||
| 85 | $this->touch['model/constructor'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Constructor.php'; |
||
| 86 | } |
||
| 87 | |||
| 88 | //set entity map |
||
| 89 | |||
| 90 | $entityDir = $this->directory['modelDir'].''.DIRECTORY_SEPARATOR.'Entity'; |
||
| 91 | |||
| 92 | if(!file_exists($entityDir)){
|
||
| 93 | files()->makeDirectory($entityDir); |
||
| 94 | } |
||
| 95 | |||
| 96 | $entityTableName = ucfirst($this->argument['table']); |
||
| 97 | |||
| 98 | $entityClass = $entityDir.''.DIRECTORY_SEPARATOR.''.$entityTableName.''.DIRECTORY_SEPARATOR.''.$entityTableName; |
||
| 99 | |||
| 100 | |||
| 101 | $generator = new Generator($entityDir,'EntityMap'); |
||
| 102 | |||
| 103 | if(!file_exists($entityDir.''.DIRECTORY_SEPARATOR.'EntityMap.php')){
|
||
| 104 | |||
| 105 | //$this->setAnnotations(); |
||
| 106 | $generator->createClass(); |
||
| 107 | } |
||
| 108 | |||
| 109 | $entityMapNamespace = Utils::getNamespace($entityDir.''.DIRECTORY_SEPARATOR.'EntityMap.php'); |
||
| 110 | |||
| 111 | $entityMapNamespaceResolve = new $entityMapNamespace; |
||
| 112 | |||
| 113 | if(!method_exists($entityMapNamespaceResolve,strtolower($this->argument['table']))){
|
||
| 114 | |||
| 115 | $generator->createClassUse([ |
||
| 116 | Utils::getNamespace($entityClass) |
||
| 117 | ]); |
||
| 118 | |||
| 119 | $generator->createMethod([ |
||
| 120 | strtolower($this->argument['table']) |
||
| 121 | ]); |
||
| 122 | |||
| 123 | $generator->createMethodParameters([ |
||
| 124 | strtolower($this->argument['table']) => '$query' |
||
| 125 | ]); |
||
| 126 | |||
| 127 | $generator->createMethodBody([ |
||
| 128 | strtolower($this->argument['table'])=>'return new '.$entityTableName.'($query);' |
||
| 129 | ]); |
||
| 130 | |||
| 131 | $generator->createMethodDocument([ |
||
| 132 | strtolower($this->argument['table']) => [ |
||
| 133 | $entityTableName.' Entity Instance', |
||
| 134 | '', |
||
| 135 | '@param $query', |
||
| 136 | '@return '.$entityTableName |
||
| 137 | ] |
||
| 138 | ]); |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | //set builder map |
||
| 143 | $generator = new Generator($this->directory['builderDir'],'BuilderMap'); |
||
| 144 | |||
| 145 | if(!file_exists($this->directory['builderDir'].''.DIRECTORY_SEPARATOR.'BuilderMap.php')){
|
||
| 146 | |||
| 147 | $this->setAnnotations(); |
||
| 148 | $generator->createClass(); |
||
| 149 | } |
||
| 150 | |||
| 151 | if(!file_exists($this->touch['model/model'])){
|
||
| 152 | |||
| 153 | $generator->createMethod([ |
||
| 154 | strtolower($this->argument['file']) |
||
| 155 | ]); |
||
| 156 | |||
| 157 | $generator->createMethodBody([ |
||
| 158 | strtolower($this->argument['file'])=>'return new '.$this->argument['file'].'Builder();' |
||
| 159 | ]); |
||
| 160 | |||
| 161 | $generator->createMethodDocument([ |
||
| 162 | strtolower($this->argument['file']) => [ |
||
| 163 | $this->argument['file'].' Builder Instance', |
||
| 164 | '', |
||
| 165 | '@return '.$this->argument['file'].'Builder' |
||
| 166 | ] |
||
| 167 | ]); |
||
| 168 | |||
| 169 | } |
||
| 170 | |||
| 171 | //set project touch |
||
| 172 | $this->file->touch($this,[ |
||
| 173 | 'stub'=>'Model_Create' |
||
| 174 | ]); |
||
| 175 | |||
| 176 | |||
| 177 | echo $this->classical(' > Model called as "'.$this->argument['file'].'" has been successfully created in the '.app()->namespace()->model().'');
|
||
| 178 | } |
||
| 201 | } |