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:
Complex classes like AbstractPluginGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractPluginGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class AbstractPluginGenerator |
||
|
|||
36 | { |
||
37 | |||
38 | const DEFAULT_NESTING_LEVEL = 100; |
||
39 | const NEW_HOOK_VERSION = '3.0.9'; |
||
40 | const STOP_PROCESS = 'quit'; |
||
41 | const INPUT_OPEN = '['; |
||
42 | const INPUT_CLOSE = ']'; |
||
43 | const PLUGIN_PREFIX = 'plg_'; |
||
44 | |||
45 | /** |
||
46 | * app |
||
47 | * |
||
48 | * @var \Eccube\Application |
||
49 | */ |
||
50 | protected $app; |
||
51 | |||
52 | /** |
||
53 | * QuestionHelper |
||
54 | * |
||
55 | * @var \Symfony\Component\Console\Helper\QuestionHelper |
||
56 | */ |
||
57 | protected $dialog; |
||
58 | |||
59 | /** |
||
60 | * InputInterface |
||
61 | * |
||
62 | * @var \Symfony\Component\Console\Input\InputInterface |
||
63 | */ |
||
64 | protected $input; |
||
65 | |||
66 | /** |
||
67 | * InputInterface |
||
68 | * |
||
69 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
70 | */ |
||
71 | protected $output; |
||
72 | |||
73 | /** |
||
74 | * $paramList |
||
75 | * @var array $paramList |
||
76 | */ |
||
77 | protected $paramList; |
||
78 | |||
79 | /** |
||
80 | * |
||
81 | * @var int |
||
82 | */ |
||
83 | private $nestingLevel; |
||
84 | |||
85 | /** |
||
86 | * ヘッダー |
||
87 | */ |
||
88 | abstract protected function getHeader(); |
||
89 | |||
90 | /** |
||
91 | * start() |
||
92 | */ |
||
93 | abstract protected function start(); |
||
94 | |||
95 | /** |
||
96 | * フィルドーセット |
||
97 | */ |
||
98 | abstract protected function initFieldSet(); |
||
99 | |||
100 | public function __construct(Application $app) |
||
105 | |||
106 | /** |
||
107 | * |
||
108 | * @param \Symfony\Component\Console\Helper\QuestionHelper $dialog |
||
109 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
110 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
111 | */ |
||
112 | public function init($dialog, $input, $output) |
||
119 | |||
120 | public function run() |
||
163 | |||
164 | protected function exitGenerator($msg = 'Quitting Bye bye.') |
||
168 | |||
169 | protected function makeLineRequest($params) |
||
267 | |||
268 | protected function getNestingLevel() |
||
272 | |||
273 | protected function setNestingLevel($nestingLevel) |
||
277 | |||
278 | |||
279 | /** |
||
280 | * app/Plugin直下にあるディレクトリ名(プラグインコード)を取得 |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | protected function getPluginCodes() |
||
299 | |||
300 | /** |
||
301 | * Entity、Repositoryファイルの作成 |
||
302 | * |
||
303 | * @param array $metadatas |
||
304 | * @param array $fsList |
||
305 | */ |
||
306 | protected function generateEntities(array $metadatas, array &$fsList) |
||
307 | { |
||
308 | /** @var ClassMetadataInfo $class */ |
||
309 | foreach ($metadatas as $class) { |
||
310 | |||
311 | // Entity作成 |
||
312 | $EntityGenerator = new EntityGenerator(); |
||
313 | $EntityGenerator->setBackupExisting(false); |
||
314 | $EntityGenerator->setClassToExtend('Eccube\\Entity\\AbstractEntity'); |
||
315 | $EntityGenerator->setGenerateAnnotations(false); |
||
316 | $EntityGenerator->setRegenerateEntityIfExists(true); |
||
317 | $EntityGenerator->setGenerateStubMethods(true); |
||
318 | $EntityGenerator->setUpdateEntityIfExists(false); |
||
319 | |||
320 | $appPath = $this->app['config']['root_dir'].'/app/'; |
||
321 | $EntityGenerator->generate(array($class), $appPath); |
||
322 | |||
323 | $filename = $appPath.str_replace('\\', DIRECTORY_SEPARATOR, $class->name).'.php'; |
||
324 | if (is_file($filename)) { |
||
325 | $fsList['file'][$filename] = true; |
||
326 | } else { |
||
327 | $fsList['file'][$filename] = false; |
||
328 | } |
||
329 | |||
330 | // Repository作成 |
||
331 | $RepositoryGenerator = new EntityRepositoryGenerator(); |
||
332 | $RepositoryGenerator->writeEntityRepositoryClass($class->customRepositoryClassName, $appPath); |
||
333 | |||
334 | $filename = $appPath.str_replace('\\', DIRECTORY_SEPARATOR, $class->customRepositoryClassName).'.php'; |
||
335 | if (is_file($filename)) { |
||
336 | $fsList['file'][$filename] = true; |
||
337 | } else { |
||
338 | $fsList['file'][$filename] = false; |
||
339 | } |
||
340 | } |
||
341 | |||
342 | } |
||
343 | |||
344 | |||
345 | /** |
||
346 | * migraionファイルの作成 |
||
347 | * |
||
348 | * @param array $metadatas |
||
349 | * @param array $fsList |
||
350 | * @param $pluginCode |
||
351 | * @param $codePath |
||
352 | */ |
||
353 | protected function generateMigration(array $metadatas, array &$fsList = array(), $pluginCode, $codePath) |
||
354 | { |
||
355 | View Code Duplication | if (count($metadatas)) { |
|
356 | $migrationContent = $this->makeMigration($pluginCode, $metadatas); |
||
357 | $date = date('YmdHis'); |
||
358 | $migrationContent = str_replace('[datetime]', $date, $migrationContent); |
||
359 | $migPath = $codePath.'/Resource/doctrine/migration/Version'.$date.'.php'; |
||
360 | |||
361 | file_put_contents($migPath, $migrationContent); |
||
362 | if (is_file($migPath)) { |
||
363 | $fsList['file'][$migPath] = true; |
||
364 | } else { |
||
365 | $fsList['file'][$migPath] = false; |
||
366 | } |
||
367 | } |
||
368 | } |
||
369 | |||
370 | |||
371 | /** |
||
372 | * migrationファイルの作成 |
||
373 | * |
||
374 | * @param $pluginCode |
||
375 | * @param array $metadatas |
||
376 | * @return mixed|string |
||
377 | */ |
||
378 | protected function makeMigration($pluginCode, array $metadatas) |
||
417 | |||
418 | |||
419 | protected function makeCreateParts($metadatas) |
||
420 | { |
||
421 | $ret = array(); |
||
422 | foreach ($metadatas as $metadata) { |
||
423 | |||
424 | $nameFormated = Inflector::camelize($metadata->table['name']); |
||
425 | $tmp = array(); |
||
426 | $tmp[] = ''; |
||
427 | $tmp[] = ' /**'; |
||
428 | $tmp[] = ' * @param Schema $schema'; |
||
429 | $tmp[] = ' */'; |
||
430 | $tmp[] = ' public function createTable'.ucfirst($nameFormated).'(Schema $schema)'; |
||
431 | $tmp[] = ' {'; |
||
432 | $tmp[] = ' $table = $schema->createTable(\''.$metadata->table['name'].'\');'; |
||
433 | $columns = $metadata->fieldMappings; |
||
434 | View Code Duplication | foreach ($columns as $column) { |
|
435 | |||
436 | $typeName = $column['type']; |
||
437 | $tmp[] = ' $table->addColumn(\''.$column['columnName'].'\', \''.$typeName.'\', array('; |
||
438 | $param = array(); |
||
439 | if (isset($column['nullable']) && $column['nullable']) { |
||
440 | $param['notnull'] = 'true'; |
||
441 | } else { |
||
442 | $param['notnull'] = 'false'; |
||
443 | } |
||
444 | |||
445 | foreach ($param as $parKey => $parVal) { |
||
446 | $tmp[] = ' \''.$parKey.'\' => '.$parVal.','; |
||
447 | } |
||
448 | $tmp[] = ' ));'; |
||
449 | } |
||
450 | |||
451 | |||
452 | $tmp[] = ' }'; |
||
453 | $tmp[] = ''; |
||
454 | $ret[ucfirst($nameFormated)] = $tmp; |
||
455 | } |
||
456 | |||
457 | return $ret; |
||
458 | } |
||
459 | |||
460 | protected function makeDropParts($metadatas) |
||
469 | |||
470 | |||
471 | /** |
||
472 | * メッセージ表示 |
||
473 | * |
||
474 | * @param array $fsList |
||
475 | */ |
||
476 | protected function completeMessage(array $fsList) |
||
514 | } |
||
515 |