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 |
||
15 | class ImportRunner |
||
16 | { |
||
17 | /** |
||
18 | * @var WorkflowFactoryInterface |
||
19 | */ |
||
20 | private $workflowFactory; |
||
21 | |||
22 | /** |
||
23 | * @var EventDispatcherInterface |
||
24 | */ |
||
25 | private $eventDispatcher; |
||
26 | |||
27 | /** |
||
28 | * @var LoggerInterface |
||
29 | */ |
||
30 | private $logger; |
||
31 | |||
32 | 14 | public function __construct(WorkflowFactoryInterface $workflowFactory = null, EventDispatcherInterface $eventDispatcher = null, LoggerInterface $logger = null) |
|
33 | { |
||
34 | 14 | if (!$workflowFactory) { |
|
35 | 8 | $workflowFactory = new DefaultWorkflowFactory(); |
|
36 | } |
||
37 | |||
38 | 14 | $this->workflowFactory = $workflowFactory; |
|
39 | 14 | $this->eventDispatcher = $eventDispatcher; |
|
40 | 14 | $this->logger = $logger ? $logger : new NullLogger(); |
|
41 | 14 | } |
|
42 | |||
43 | /** |
||
44 | * @return ImportRunner |
||
45 | */ |
||
46 | 2 | public static function build(WorkflowFactoryInterface $workflowFactory = null) |
|
50 | |||
51 | 14 | private function process(Workflow $workflow, Import $import) |
|
52 | { |
||
53 | 14 | $e = null; |
|
54 | 14 | $importRun = null; |
|
55 | |||
56 | 14 | if ($this->eventDispatcher && $importRun = $import->getRun()) { |
|
57 | $e = new ImportProcessEvent($import); |
||
58 | } |
||
59 | |||
60 | 14 | View Code Duplication | if ($e && $importRun->getConfiguration()) { |
|
|||
61 | $this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_PREPARE.'.'.$importRun->getConfiguration()->getImporterId(), $e); |
||
62 | } |
||
63 | |||
64 | 14 | $workflow->process(); |
|
65 | |||
66 | 14 | View Code Duplication | if ($e && $importRun->getConfiguration()) { |
67 | $this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_FINISH.'.'.$importRun->getConfiguration()->getImporterId(), $e); |
||
68 | } |
||
69 | 14 | } |
|
70 | |||
71 | /** |
||
72 | * @return array |
||
73 | */ |
||
74 | 2 | public function preview(Import $import, $offset = 0) |
|
75 | { |
||
76 | 2 | $importRun = $import->getRun(); |
|
77 | 2 | $previewResult = array('from' => array(), 'to' => array()); |
|
78 | |||
79 | 2 | $workflow = $this->workflowFactory->buildPreviewWorkflow($import, $previewResult, $offset); |
|
80 | 2 | $this->process($workflow, $import); |
|
81 | |||
82 | 2 | if (0 == count($previewResult['from'])) { |
|
83 | throw new ImportRunException("Unable to preview row with offset '$offset'. EOF?", $importRun); |
||
84 | } |
||
85 | |||
86 | //cleanup from writer |
||
87 | 2 | if (count($previewResult['to']) > 0) { |
|
88 | 2 | $previewResult['to'] = $previewResult['to'][0]; |
|
89 | } else { |
||
90 | $previewResult['to'] = array_fill_keys($import->mappings()->getTargetFields(), null); |
||
91 | } |
||
92 | |||
93 | 2 | return $previewResult; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return Import |
||
98 | */ |
||
99 | 3 | public function dryRun(Import $import) |
|
100 | { |
||
101 | 3 | $importRun = $import->getRun(); |
|
102 | 3 | $workflow = $this->workflowFactory->buildDryrunWorkflow($import, $importRun); |
|
103 | 3 | $this->process($workflow, $import); |
|
104 | |||
105 | 3 | return $importRun; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return Import |
||
110 | */ |
||
111 | 11 | public function run(Import $import) |
|
123 | } |
||
124 |
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.