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 |
||
39 | abstract class AbstractPlugin implements PluginInterface |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * The appliation instance. |
||
44 | * |
||
45 | * @var \TechDivision\Import\ApplicationInterface |
||
46 | */ |
||
47 | protected $application; |
||
48 | |||
49 | /** |
||
50 | * The plugin configuration instance. |
||
51 | * |
||
52 | * @var \TechDivision\Import\Configuration\PluginConfigurationInterface |
||
53 | */ |
||
54 | protected $pluginConfiguration; |
||
55 | |||
56 | /** |
||
57 | * The import adapter instance. |
||
58 | * |
||
59 | * @var \TechDivision\Import\Adapter\ImportAdapterInterface |
||
60 | */ |
||
61 | protected $importAdapter; |
||
62 | |||
63 | /** |
||
64 | * Initializes the plugin with the application instance. |
||
65 | * |
||
66 | * @param \TechDivision\Import\ApplicationInterface $application The application instance |
||
67 | */ |
||
68 | 3 | public function __construct(ApplicationInterface $application) |
|
69 | { |
||
70 | 3 | $this->application = $application; |
|
71 | 3 | } |
|
72 | |||
73 | /** |
||
74 | * Set's the plugin configuration instance. |
||
75 | * |
||
76 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | 3 | public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration) |
|
81 | { |
||
82 | 3 | $this->pluginConfiguration = $pluginConfiguration; |
|
83 | 3 | } |
|
84 | |||
85 | /** |
||
86 | * Return's the plugin configuration instance. |
||
87 | * |
||
88 | * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance |
||
89 | */ |
||
90 | 3 | public function getPluginConfiguration() |
|
91 | { |
||
92 | 3 | return $this->pluginConfiguration; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return's the unique serial for this import process. |
||
97 | * |
||
98 | * @return string The unique serial |
||
99 | */ |
||
100 | 2 | public function getSerial() |
|
101 | { |
||
102 | 2 | return $this->getApplication()->getSerial(); |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Set's the import adapter instance. |
||
107 | * |
||
108 | * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | public function setImportAdapter(ImportAdapterInterface $importAdapter) |
||
113 | { |
||
114 | $this->importAdapter = $importAdapter; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Return's the import adapter instance. |
||
119 | * |
||
120 | * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance |
||
121 | */ |
||
122 | public function getImportAdapter() |
||
123 | { |
||
124 | return $this->importAdapter; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Return's the plugin's execution context configuration. |
||
129 | * |
||
130 | * @return \TechDivision\Import\ExecutionContextInterface The execution context configuration to use |
||
131 | */ |
||
132 | public function getExecutionContext() |
||
133 | { |
||
134 | return $this->getPluginConfiguration()->getExecutionContext(); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Return's the target directory for the artefact export. |
||
139 | * |
||
140 | * @return string The target directory for the artefact export |
||
141 | */ |
||
142 | View Code Duplication | public function getTargetDir() |
|
|
|||
143 | { |
||
144 | |||
145 | // load the status from the registry processor |
||
146 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
||
147 | |||
148 | // query whether or not a target directory (mandatory) has been configured |
||
149 | if (isset($status[RegistryKeys::TARGET_DIRECTORY])) { |
||
150 | return $status[RegistryKeys::TARGET_DIRECTORY]; |
||
151 | } |
||
152 | |||
153 | // throw an exception if the root category is NOT available |
||
154 | throw new \Exception(sprintf('Can\'t find a target directory in status data for import %s', $this->getSerial())); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Return's the application instance. |
||
159 | * |
||
160 | * @return \TechDivision\Import\ApplicationInterface The application instance |
||
161 | */ |
||
162 | 3 | public function getApplication() |
|
163 | { |
||
164 | 3 | return $this->application; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * Return's the RegistryProcessor instance to handle the running threads. |
||
169 | * |
||
170 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
171 | */ |
||
172 | 3 | protected function getRegistryProcessor() |
|
173 | { |
||
174 | 3 | return $this->getApplication()->getRegistryProcessor(); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Return's the import processor instance. |
||
179 | * |
||
180 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
181 | */ |
||
182 | protected function getImportProcessor() |
||
183 | { |
||
184 | return $this->getApplication()->getImportProcessor(); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Return's the logger with the passed name, by default the system logger. |
||
189 | * |
||
190 | * @param string $name The name of the requested system logger |
||
191 | * |
||
192 | * @return \Psr\Log\LoggerInterface The logger instance |
||
193 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
194 | */ |
||
195 | 1 | protected function getSystemLogger($name = LoggerKeys::SYSTEM) |
|
196 | { |
||
197 | 1 | return $this->getApplication()->getSystemLogger($name); |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Query whether or not the system logger with the passed name is available. |
||
202 | * |
||
203 | * @param string $name The name of the requested system logger |
||
204 | * |
||
205 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
206 | */ |
||
207 | protected function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
208 | { |
||
209 | return $this->getApplication()->hasSystemLogger($name); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Return's the array with the system logger instances. |
||
214 | * |
||
215 | * @return array The logger instance |
||
216 | */ |
||
217 | protected function getSystemLoggers() |
||
218 | { |
||
219 | return $this->getApplication()->getSystemLoggers(); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Remove's the passed line from the file with the passed name. |
||
224 | * |
||
225 | * @param string $line The line to be removed |
||
226 | * @param string $filename The name of the file the line has to be removed |
||
227 | * |
||
228 | * @return void |
||
229 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
230 | */ |
||
231 | protected function removeLineFromFile($line, $filename) |
||
235 | |||
236 | /** |
||
237 | * Return's the system configuration. |
||
238 | * |
||
239 | * @return \TechDivision\Import\Configuration\ConfigurationInterface The system configuration |
||
240 | */ |
||
241 | protected function getConfiguration() |
||
245 | |||
246 | /** |
||
247 | * Return's the PID filename to use. |
||
248 | * |
||
249 | * @return string The PID filename |
||
250 | */ |
||
251 | protected function getPidFilename() |
||
255 | |||
256 | /** |
||
257 | * Return's the source directory that has to be watched for new files. |
||
258 | * |
||
259 | * @return string The source directory |
||
260 | */ |
||
261 | protected function getSourceDir() |
||
265 | |||
266 | /** |
||
267 | * Removes the passed directory recursively. |
||
268 | * |
||
269 | * @param string $src Name of the directory to remove |
||
270 | * |
||
271 | * @return void |
||
272 | * @throws \Exception Is thrown, if the directory can not be removed |
||
273 | */ |
||
274 | protected function removeDir($src) |
||
300 | |||
301 | /** |
||
302 | * Return's the configured swift mailer instance. |
||
303 | * |
||
304 | * @return \Swift_Mailer|null The mailer instance |
||
305 | */ |
||
306 | protected function getSwiftMailer() |
||
307 | { |
||
308 | |||
309 | // the swift mailer configuration |
||
310 | if ($swiftMailerConfiguration = $this->getPluginConfiguration()->getSwiftMailer()) { |
||
311 | // create the swift mailer (factory) instance |
||
312 | $possibleSwiftMailer = $this->getApplication()->getContainer()->get($swiftMailerConfiguration->getId()); |
||
313 | |||
314 | // query whether or not we've a factory or the instance |
||
326 | } |
||
327 |
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.