Conditions | 30 |
Paths | > 20000 |
Total Lines | 282 |
Code Lines | 202 |
Lines | 25 |
Ratio | 8.87 % |
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 |
||
229 | private function createFilesAndFolders($code, $paramList) |
||
230 | { |
||
231 | $fsList = array( |
||
232 | 'dir' => array(), |
||
233 | 'file' => array(), |
||
234 | ); |
||
235 | |||
236 | // config.ymlを作成 |
||
237 | $config = array(); |
||
238 | $config['name'] = $paramList['pluginName']['value']; |
||
239 | $config['code'] = $code; |
||
240 | $config['version'] = $paramList['version']['value']; |
||
241 | if (!empty($paramList['hookPoints']['value']) || !empty($paramList['events']['value'])) { |
||
242 | $config['event'] = $code.'Event'; |
||
243 | } |
||
244 | $config['service'] = array($code.'ServiceProvider'); |
||
245 | if ($this->paramList['useOrmPath']['value']) { |
||
246 | $config['orm.path'] = array('/Resource/doctrine'); |
||
247 | } |
||
248 | |||
249 | $codePath = $this->app['config']['root_dir'].'/app/Plugin/'.$code; |
||
250 | |||
251 | $file = new Filesystem(); |
||
252 | $file->mkdir($codePath); |
||
253 | if (is_dir($codePath)) { |
||
254 | $fsList['dir'][$codePath] = true; |
||
255 | } else { |
||
256 | $fsList['dir'][$codePath] = false; |
||
257 | } |
||
258 | |||
259 | $srcPath = $codePath.'/config.yml'; |
||
260 | file_put_contents($srcPath, Yaml::dump($config)); |
||
261 | if (is_file($srcPath)) { |
||
262 | $fsList['file'][$srcPath] = true; |
||
263 | } else { |
||
264 | $fsList['file'][$srcPath] = false; |
||
265 | } |
||
266 | |||
267 | $author = $paramList['author']['value']; |
||
268 | $year = date('Y'); |
||
269 | |||
270 | // PluginManager |
||
271 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/PluginManager.php'); |
||
272 | $from = '/\[code\]/'; |
||
273 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
274 | $from = '/\[author\]/'; |
||
275 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
276 | $from = '/\[year\]/'; |
||
277 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
278 | |||
279 | $srcPath = $codePath.'/PluginManager.php'; |
||
280 | file_put_contents($srcPath, $pluginFileAfter); |
||
281 | if (is_file($srcPath)) { |
||
282 | $fsList['file'][$srcPath] = true; |
||
283 | } else { |
||
284 | $fsList['file'][$srcPath] = false; |
||
285 | } |
||
286 | |||
287 | // ServiceProvider |
||
288 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ServiceProvider.php'); |
||
289 | $from = '/\[code\]/'; |
||
290 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
291 | $from = '/\[lower_code\]/'; |
||
292 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
293 | $from = '/\[author\]/'; |
||
294 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
295 | $from = '/\[year\]/'; |
||
296 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
297 | |||
298 | $file->mkdir($codePath.'/ServiceProvider'); |
||
299 | View Code Duplication | if (is_dir($codePath.'/ServiceProvider')) { |
|
300 | $fsList['dir'][$codePath.'/ServiceProvider'] = true; |
||
301 | } else { |
||
302 | $fsList['dir'][$codePath.'/ServiceProvider'] = false; |
||
303 | } |
||
304 | |||
305 | $srcPath = $codePath.'/ServiceProvider/'.$code.'ServiceProvider.php'; |
||
306 | file_put_contents($srcPath, $pluginFileAfter); |
||
307 | if (is_file($srcPath)) { |
||
308 | $fsList['file'][$srcPath] = true; |
||
309 | } else { |
||
310 | $fsList['file'][$srcPath] = false; |
||
311 | } |
||
312 | |||
313 | // ConfigController |
||
314 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ConfigController.php'); |
||
315 | $from = '/\[code\]/'; |
||
316 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
317 | $from = '/\[author\]/'; |
||
318 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
319 | $from = '/\[year\]/'; |
||
320 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
321 | $from = '/\[code_name\]/'; |
||
322 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
323 | |||
324 | $file->mkdir($codePath.'/Controller'); |
||
325 | View Code Duplication | if (is_dir($codePath.'/Controller')) { |
|
326 | $fsList['dir'][$codePath.'/Controller'] = true; |
||
327 | } else { |
||
328 | $fsList['dir'][$codePath.'/Controller'] = false; |
||
329 | } |
||
330 | |||
331 | $srcPath = $codePath.'/Controller/ConfigController.php'; |
||
332 | file_put_contents($srcPath, $pluginFileAfter); |
||
333 | if (is_file($srcPath)) { |
||
334 | $fsList['file'][$srcPath] = true; |
||
335 | } else { |
||
336 | $fsList['file'][$srcPath] = false; |
||
337 | } |
||
338 | |||
339 | // Controller |
||
340 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/Controller.php'); |
||
341 | $from = '/\[code\]/'; |
||
342 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
343 | $from = '/\[author\]/'; |
||
344 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
345 | $from = '/\[year\]/'; |
||
346 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
347 | $from = '/\[code_name\]/'; |
||
348 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
349 | |||
350 | $srcPath = $codePath.'/Controller/'.$code.'Controller.php'; |
||
351 | file_put_contents($srcPath, $pluginFileAfter); |
||
352 | if (is_file($srcPath)) { |
||
353 | $fsList['file'][$srcPath] = true; |
||
354 | } else { |
||
355 | $fsList['file'][$srcPath] = false; |
||
356 | } |
||
357 | |||
358 | // Form |
||
359 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ConfigType.php'); |
||
360 | $from = '/\[code\]/'; |
||
361 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
362 | $from = '/\[author\]/'; |
||
363 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
364 | $from = '/\[year\]/'; |
||
365 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
366 | $from = '/\[code_name\]/'; |
||
367 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
368 | |||
369 | $file->mkdir($codePath.'/Form/Type'); |
||
370 | View Code Duplication | if (is_dir($codePath.'/Form/Type')) { |
|
371 | $fsList['dir'][$codePath.'/Form/Type'] = true; |
||
372 | } else { |
||
373 | $fsList['dir'][$codePath.'/Form/Type'] = false; |
||
374 | } |
||
375 | |||
376 | $srcPath = $codePath.'/Form/Type/'.$code.'ConfigType.php'; |
||
377 | file_put_contents($codePath.'/Form/Type/'.$code.'ConfigType.php', $pluginFileAfter); |
||
378 | if (is_file($srcPath)) { |
||
379 | $fsList['file'][$srcPath] = true; |
||
380 | } else { |
||
381 | $fsList['file'][$srcPath] = false; |
||
382 | } |
||
383 | |||
384 | // Twig |
||
385 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/config.twig'); |
||
386 | $from = '/\[code\]/'; |
||
387 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
388 | |||
389 | $file->mkdir($codePath.'/Resource/template/admin'); |
||
390 | View Code Duplication | if (is_dir($codePath.'/Resource/template/admin')) { |
|
391 | $fsList['dir'][$codePath.'/Resource/template/admin'] = true; |
||
392 | } else { |
||
393 | $fsList['dir'][$codePath.'/Resource/template/admin'] = false; |
||
394 | } |
||
395 | |||
396 | $srcPath = $codePath.'/Resource/template/admin/config.twig'; |
||
397 | file_put_contents($srcPath, $pluginFileAfter); |
||
398 | if (is_file($srcPath)) { |
||
399 | $fsList['file'][$srcPath] = true; |
||
400 | } else { |
||
401 | $fsList['file'][$srcPath] = false; |
||
402 | } |
||
403 | |||
404 | // index.twig |
||
405 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/index.twig'); |
||
406 | $from = '/\[code\]/'; |
||
407 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileBefore); |
||
408 | |||
409 | $file->mkdir($codePath.'/Resource/template/admin'); |
||
410 | View Code Duplication | if (is_dir($codePath.'/Resource/template/admin')) { |
|
411 | $fsList['dir'][$codePath.'/Resource/template/admin'] = true; |
||
412 | } else { |
||
413 | $fsList['dir'][$codePath.'/Resource/template/admin'] = false; |
||
414 | } |
||
415 | |||
416 | $srcPath = $codePath.'/Resource/template/index.twig'; |
||
417 | file_put_contents($srcPath, $pluginFileAfter); |
||
418 | if (is_file($srcPath)) { |
||
419 | $fsList['file'][$srcPath] = true; |
||
420 | } else { |
||
421 | $fsList['file'][$srcPath] = false; |
||
422 | } |
||
423 | |||
424 | $onFunctions = array(); |
||
425 | $eventKeys = array(); |
||
426 | $onEvents = array(); |
||
427 | |||
428 | // イベント |
||
429 | $events = $paramList['events']['value']; |
||
430 | if (count($events) > 0) { |
||
431 | foreach ($events as $eventKey => $eventConst) { |
||
432 | $onEvents[$eventKey] = array(array('on'.$eventConst.', NORMAL')); |
||
433 | $onFunctions[$eventKey] = 'on'.$eventConst; |
||
434 | $eventKeys[] = $eventKey; |
||
435 | } |
||
436 | } |
||
437 | |||
438 | // フックポイント |
||
439 | $hookPoints = $paramList['hookPoints']['value']; |
||
440 | if (count($hookPoints)) { |
||
441 | foreach ($hookPoints as $hookKey => $hookConst) { |
||
442 | $onName = 'on'.join(array_map('ucfirst', explode('_', strtolower($hookConst)))); |
||
443 | $onEvents[$hookKey] = array(array($onName.', NORMAL')); |
||
444 | $onFunctions[$hookKey] = $onName; |
||
445 | } |
||
446 | } |
||
447 | |||
448 | if (count($onEvents)) { |
||
449 | $srcPath = $codePath.'/event.yml'; |
||
450 | file_put_contents($srcPath, str_replace('\'', '', Yaml::dump($onEvents))); |
||
451 | if (is_file($srcPath)) { |
||
452 | $fsList['file'][$srcPath] = true; |
||
453 | } else { |
||
454 | $fsList['file'][$srcPath] = false; |
||
455 | } |
||
456 | |||
457 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/Event.php'); |
||
458 | |||
459 | // Event |
||
460 | $from = '/\[code\]/'; |
||
461 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
462 | $from = '/\[author\]/'; |
||
463 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
464 | $from = '/\[year\]/'; |
||
465 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
466 | |||
467 | $functions = ''; |
||
468 | $args = include $this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/eventArguments.php'; |
||
469 | foreach ($onFunctions as $key => $name) { |
||
470 | if (in_array($key, $eventKeys)) { |
||
471 | // 共通イベントは引数の型を利用するイベントにより変更 |
||
472 | $ext = pathinfo($key, PATHINFO_EXTENSION); |
||
473 | if (array_key_exists($ext, $args)) { |
||
474 | $functions .= " /**\n * @param {$args[$ext]} \$event\n */\n public function {$name}({$args[$ext]} \$event)\n {\n }\n\n"; |
||
475 | } else { |
||
476 | // 旧イベントの場合、引数は「eccube.event.render」のみ可能 |
||
477 | if (preg_match("/^eccube.event.render\./", $key)) { |
||
478 | $functions .= " /**\n * @param {$args['eccube.event.render']} \$event\n */\n public function {$name}({$args['eccube.event.render']} \$event)\n {\n }\n\n"; |
||
479 | } else { |
||
480 | $functions .= " /**\n *\n */\n public function {$name}()\n {\n }\n\n"; |
||
481 | } |
||
482 | } |
||
483 | } else { |
||
484 | // HookPointイベントの引数はEventArgs共通 |
||
485 | $functions .= " /**\n * @param EventArgs \$event\n */\n public function {$name}(EventArgs \$event)\n {\n }\n\n"; |
||
486 | } |
||
487 | } |
||
488 | $from = '/\[hookpoint_function\]/'; |
||
489 | $pluginFileAfter = preg_replace($from, $functions, $pluginFileAfter); |
||
490 | $srcPath = $codePath.'/'.$code.'Event.php'; |
||
491 | file_put_contents($srcPath, $pluginFileAfter); |
||
492 | if (is_file($srcPath)) { |
||
493 | $fsList['file'][$srcPath] = true; |
||
494 | } else { |
||
495 | $fsList['file'][$srcPath] = false; |
||
496 | } |
||
497 | } |
||
498 | |||
499 | // LICENSE |
||
500 | $srcPath = $codePath.'/LICENSE'; |
||
501 | $file->copy($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/LICENSE', $srcPath); |
||
502 | if (is_file($srcPath)) { |
||
503 | $fsList['file'][$srcPath] = true; |
||
504 | } else { |
||
505 | $fsList['file'][$srcPath] = false; |
||
506 | } |
||
507 | |||
508 | $this->completeMessage($fsList); |
||
509 | |||
510 | } |
||
511 | |||
560 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: