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