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 |
||
42 | abstract class AbstractStep extends \Thread implements StepInterface |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * The maximum number of retries. |
||
47 | * |
||
48 | * @var integer |
||
49 | */ |
||
50 | const MAX_RETRIES = 0; |
||
51 | |||
52 | /** |
||
53 | * The provisioning service. |
||
54 | * |
||
55 | * @var \AppserverIo\Appserver\Core\Api\ServiceInterface; |
||
56 | */ |
||
57 | protected $service; |
||
58 | |||
59 | /** |
||
60 | * The step node with the configuration data for this step. |
||
61 | * |
||
62 | * @var \AppserverIo\Appserver\Core\Api\Node\StepNode |
||
63 | */ |
||
64 | protected $stepNode; |
||
65 | |||
66 | /** |
||
67 | * The datasource node found in the provisioning configuration. |
||
68 | * |
||
69 | * @var \AppserverIo\Psr\ApplicationServer\Configuration\DatasourceConfigurationInterface |
||
70 | */ |
||
71 | protected $datasourceNode; |
||
72 | |||
73 | /** |
||
74 | * The initial context. |
||
75 | * |
||
76 | * @var \AppserverIo\Psr\ApplicationServer\ContextInterface |
||
77 | */ |
||
78 | protected $initialContext; |
||
79 | |||
80 | /** |
||
81 | * The absolute path to the appserver PHP executable. |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $phpExecutable; |
||
86 | |||
87 | /** |
||
88 | * The absolute path to the applications folder. |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $webappPath; |
||
93 | |||
94 | /** |
||
95 | * The application instance. |
||
96 | * |
||
97 | * @var \AppserverIo\Psr\Application\ApplicationInterface |
||
98 | */ |
||
99 | protected $application; |
||
100 | |||
101 | /** |
||
102 | * Injects the provisioning service. |
||
103 | * |
||
104 | * @param \AppserverIo\Psr\ApplicationServer\ServiceInterface $service The provisioning service |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function injectService(ServiceInterface $service) |
||
112 | |||
113 | /** |
||
114 | * Injects the step node with the configuration data for this step. |
||
115 | * |
||
116 | * @param \AppserverIo\Appserver\Core\Api\Node\StepNode $stepNode The step node data |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | public function injectStepNode(StepNode $stepNode) |
||
124 | |||
125 | /** |
||
126 | * Injects the datasource node found in the provisioning configuration. |
||
127 | * |
||
128 | * @param \AppserverIo\Psr\ApplicationServer\Configuration\DatasourceConfigurationInterface $datasourceNode The datasource node data |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | public function injectDatasourceNode(DatasourceConfigurationInterface $datasourceNode) |
||
136 | |||
137 | /** |
||
138 | * Injects the absolute path to the appservers PHP executable. |
||
139 | * |
||
140 | * @param string $phpExecutable The absolute path to the appservers PHP executable |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | public function injectPhpExecutable($phpExecutable) |
||
148 | |||
149 | /** |
||
150 | * Injects the absolute path to the applications folder. |
||
151 | * |
||
152 | * @param string $webappPath The absolute path to applications folder |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | public function injectWebappPath($webappPath) |
||
160 | |||
161 | /** |
||
162 | * Injects the initial context. |
||
163 | * |
||
164 | * @param \AppserverIo\Appserver\Core\InitialContext $initialContext The initial context instance |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | public function injectInitialContext(ContextInterface $initialContext) |
||
172 | |||
173 | /** |
||
174 | * Injects the application instance. |
||
175 | * |
||
176 | * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | public function injectApplication(ApplicationInterface $application) |
||
184 | |||
185 | /** |
||
186 | * Returns the provisioning service. |
||
187 | * |
||
188 | * @return \AppserverIo\Psr\ApplicationServer\ServiceInterface The provisioning service |
||
189 | */ |
||
190 | protected function getService() |
||
194 | |||
195 | /** |
||
196 | * Returns the step node data. |
||
197 | * |
||
198 | * @return \AppserverIo\Appserver\Core\Api\Node\StepNode The step node data |
||
199 | */ |
||
200 | protected function getStepNode() |
||
204 | |||
205 | /** |
||
206 | * Returns the datasource node found in the provisioning configuration. |
||
207 | * |
||
208 | * @return \AppserverIo\Psr\ApplicationServer\Configuration\DatasourceConfigurationInterface The datasource node data |
||
209 | */ |
||
210 | protected function getDatasourceNode() |
||
214 | |||
215 | /** |
||
216 | * Returns the initial context instance. |
||
217 | * |
||
218 | * @return \AppserverIo\Psr\ApplicationServer\ContextInterface The initial context |
||
219 | */ |
||
220 | protected function getInitialContext() |
||
224 | |||
225 | /** |
||
226 | * Returns the absolute path to the appservers PHP executable. |
||
227 | * |
||
228 | * @return string The absolute path to the appservers PHP executable |
||
229 | */ |
||
230 | protected function getPhpExecutable() |
||
234 | |||
235 | /** |
||
236 | * Returns the absolute path to the applications folder. |
||
237 | * |
||
238 | * @return string The applications folder |
||
239 | */ |
||
240 | protected function getWebappPath() |
||
244 | |||
245 | /** |
||
246 | * Returns the application instance. |
||
247 | * |
||
248 | * @return \AppserverIo\Psr\Application\ApplicationInterface The application instance |
||
249 | */ |
||
250 | protected function getApplication() |
||
254 | |||
255 | /** |
||
256 | * Will return the name of the application |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | protected function getAppName() |
||
264 | |||
265 | /** |
||
266 | * Will return the name of the application environment |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function getAppEnvironment() |
||
274 | |||
275 | /** |
||
276 | * Return's the container instance the application is bound to. |
||
277 | * |
||
278 | * @return \AppserverIo\Psr\ApplicationServer\ContainerInterface The container instance |
||
279 | */ |
||
280 | protected function getContainer() |
||
284 | |||
285 | /** |
||
286 | * Return's the container configuration instance. |
||
287 | * |
||
288 | * @return \AppserverIo\Psr\ApplicationServer\Configuration\ContainerConfigurationInterface The container configuration |
||
289 | */ |
||
290 | protected function getContainerNode() |
||
294 | |||
295 | /** |
||
296 | * Return's the system properties. |
||
297 | * |
||
298 | * @return \AppserverIo\Properties\PropertiesInterface The system properties |
||
299 | */ |
||
300 | protected function getSystemProperties() |
||
304 | |||
305 | /** |
||
306 | * Return's the maximum number of retries. |
||
307 | * |
||
308 | * @return integer The maximum number |
||
309 | */ |
||
310 | protected function getMaxRetries() |
||
319 | |||
320 | /** |
||
321 | * Logs the start of the provisioning. |
||
322 | * |
||
323 | * @return void |
||
324 | */ |
||
325 | protected function logStart() |
||
336 | |||
337 | /** |
||
338 | * Logs the retry of the provisioning. |
||
339 | * |
||
340 | * @param integer $retry The retry number |
||
341 | * @param string $failureReason The reason the last try failed |
||
342 | * |
||
343 | * @return void |
||
344 | */ |
||
345 | protected function logRetry($retry, $failureReason) |
||
359 | |||
360 | /** |
||
361 | * Logs the success of the provisioning. |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | protected function logSuccess() |
||
376 | |||
377 | /** |
||
378 | * Return's the param with the passed name. |
||
379 | * |
||
380 | * @param string $name The name of the param to return |
||
381 | * |
||
382 | * @return mixed The param value |
||
383 | */ |
||
384 | protected function getParam($name) |
||
388 | |||
389 | /** |
||
390 | * Executes the steps functionality in a separate context. |
||
391 | * |
||
392 | * @return void |
||
393 | */ |
||
394 | public function run() |
||
451 | |||
452 | /** |
||
453 | * Shutdown function to log unexpected errors. |
||
454 | * |
||
455 | * @return void |
||
456 | * @see http://php.net/register_shutdown_function |
||
457 | */ |
||
458 | View Code Duplication | public function shutdown() |
|
473 | } |
||
474 |
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.