Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class ViewMakeCommand extends GeneratorCommand |
||
13 | { |
||
14 | /** |
||
15 | * The console command name. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $name = 'cray:view'; |
||
20 | |||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = 'Create a new View'; |
||
27 | |||
28 | /** |
||
29 | * The type of class being generated. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $type = 'Model'; |
||
34 | |||
35 | private $fileName = 'index'; |
||
36 | |||
37 | /** |
||
38 | * Execute the console command. |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function handle() |
||
54 | |||
55 | /** |
||
56 | * Create a view for the model. |
||
57 | * |
||
58 | * @return void |
||
59 | */ |
||
60 | protected function createView() |
||
95 | |||
96 | /** |
||
97 | * |
||
98 | */ |
||
99 | protected function createViewDirectory() |
||
100 | { |
||
101 | $name = Str::studly(class_basename($this->argument('name'))); |
||
102 | $viewDirSlug = Str::slug(Str::plural(str_to_words($name), 2)); |
||
103 | $viewPath = Config::get('view.paths')[0]; |
||
104 | $dir = $this->option('dir'); |
||
105 | |||
106 | $path = $viewPath . '/' . $viewDirSlug; |
||
107 | |||
108 | if ($dir) { |
||
109 | $path = $viewPath . '/' . $dir . '/' . $viewDirSlug; |
||
110 | } |
||
111 | |||
112 | if (!file_exists($path)) { |
||
113 | mkdir($path, 0777, true); |
||
114 | } |
||
115 | |||
116 | return $path; |
||
117 | } |
||
118 | |||
119 | protected function buildView($type, $path) |
||
120 | { |
||
121 | $name = Str::studly(class_basename($this->argument('name'))); |
||
122 | $this->fileName = $type; |
||
123 | $stub = $this->files->get($this->getStub()); |
||
124 | $viewLabel = Str::plural(str_to_words($name), 2); |
||
125 | $viewName = Str::camel($viewLabel); |
||
126 | $stub = $this->replacePlaceholders($stub, $name, $path); |
||
127 | |||
128 | $target = $path . '/' . $type . '.blade.php'; |
||
129 | if($type=='delete'){ |
||
130 | $target = $path . '/modals/' . $type . '.blade.php'; |
||
131 | } |
||
132 | $displayPath = str_replace(resource_path(), '/resources', $target); |
||
133 | if (file_exists($target) && !$this->option('force')) { |
||
134 | $this->error("File already exists. Cannot overwrite {$displayPath}."); |
||
135 | } else { |
||
136 | file_put_contents($target, $stub); |
||
137 | $this->info("View created successfully in {$displayPath}"); |
||
138 | } |
||
139 | if ($type == 'create' || $type == 'edit') { |
||
140 | /** |
||
141 | * Create the _form partial form the stub |
||
142 | */ |
||
143 | $formPartial = $path . '/_form.blade.php'; |
||
144 | $formPartialDisplayPath = str_replace(resource_path(), '/resources', $formPartial); |
||
145 | $formStub = $this->files->get($this->getStub('_form')); |
||
146 | |||
147 | if (file_exists($formPartial) && !$this->option('force')) { |
||
148 | // $this->error("File already exists. Cannot overwrite {$formPartialDisplayPath}."); |
||
149 | } else { |
||
150 | file_put_contents($formPartial, $formStub); |
||
151 | $this->info("View created successfully in {$formPartialDisplayPath}"); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Get the stub file for the generator. |
||
158 | * |
||
159 | * @param null|string $fileName |
||
160 | * @return string |
||
161 | */ |
||
162 | protected function getStub($fileName = null) |
||
163 | { |
||
164 | if (isset($fileName)) { |
||
165 | $this->fileName = $fileName; |
||
166 | } |
||
167 | $stubsPath = "stubs/view/{$this->fileName}.stub"; |
||
168 | $stubs = $this->option('stubs'); |
||
169 | |||
170 | if ($stubs) { |
||
171 | $stubsPath = $stubs . '/' . $this->fileName . ".stub"; |
||
172 | } |
||
173 | return resource_path($stubsPath); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Replace all placeholders |
||
178 | * |
||
179 | * @param $stub |
||
180 | * @param $name |
||
181 | * @param null $path |
||
182 | * |
||
183 | * @return mixed |
||
184 | */ |
||
185 | protected function replacePlaceholders($stub, $name, $path = null) |
||
186 | { |
||
187 | $viewDir = str_replace(resource_path('views/'), '', $path); |
||
188 | $viewDir = str_replace('/', '.', $viewDir); |
||
189 | |||
190 | $modelSlug = Str::slug(Str::plural(str_to_words($name), 2)); |
||
191 | |||
192 | $viewLabel = str_to_words($name); |
||
193 | $viewLabelPlural = Str::plural(str_to_words($name)); |
||
194 | $viewName = Str::camel($name); |
||
195 | |||
196 | $replace = array_merge([], [ |
||
197 | '$label$' => $viewLabel, |
||
198 | '$labelPlural$' => $viewLabelPlural, |
||
199 | '$name$' => $viewName, |
||
200 | '$modelSlug$' => $modelSlug, |
||
201 | '$model$' => $name, |
||
202 | '$rows$' => '$' . Str::camel(Str::plural($name, 2)), |
||
203 | '$row$' => '$' . Str::camel(Str::singular($name)), |
||
204 | '$routeBase$' => $viewDir, |
||
205 | '$viewDir$' => $viewDir, |
||
206 | ]); |
||
207 | |||
208 | return str_replace( |
||
209 | array_keys($replace), array_values($replace), $stub |
||
210 | ); |
||
211 | } |
||
212 | |||
213 | protected function createDeleteView($path) |
||
214 | { |
||
215 | if (!file_exists($path . '/modals')) { |
||
216 | mkdir($path . '/modals'); |
||
217 | } |
||
218 | |||
219 | $this->buildView('delete', $path); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Get the default namespace for the class. |
||
224 | * |
||
225 | * @param string $rootNamespace |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | protected function getDefaultNamespace($rootNamespace) |
||
233 | |||
234 | protected function getArguments() |
||
240 | |||
241 | /** |
||
242 | * Get the console command options. |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | View Code Duplication | protected function getOptions() |
|
271 | } |
||
272 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.