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 |
||
27 | class Legacy extends SetupFactory |
||
28 | { |
||
29 | /** |
||
30 | * Data source name. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected static $dsn; |
||
35 | |||
36 | /** |
||
37 | * Root dir for IO operations. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected static $ioRootDir; |
||
42 | |||
43 | /** |
||
44 | * Database type (sqlite, mysql, ...). |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected static $db; |
||
49 | |||
50 | /** |
||
51 | * Service container. |
||
52 | * |
||
53 | * @var \eZ\Publish\Core\Base\ServiceContainer |
||
54 | */ |
||
55 | protected static $serviceContainer; |
||
56 | |||
57 | /** |
||
58 | * If the DB schema has already been initialized. |
||
59 | * |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected static $schemaInitialized = false; |
||
63 | |||
64 | /** |
||
65 | * Initial database data. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected static $initialData; |
||
70 | |||
71 | protected $repositoryReference = 'ezpublish.api.repository'; |
||
72 | |||
73 | /** |
||
74 | * Creates a new setup factory. |
||
75 | */ |
||
76 | public function __construct() |
||
89 | |||
90 | /** |
||
91 | * Creates a temporary directory and returns it. |
||
92 | * |
||
93 | * @return string |
||
94 | * @throw \RuntimeException If the root directory can't be created |
||
95 | */ |
||
96 | private function createTemporaryDirectory() |
||
115 | /** |
||
116 | * Returns a configured repository for testing. |
||
117 | * |
||
118 | * @param bool $initializeFromScratch if the back end should be initialized |
||
119 | * from scratch or re-used |
||
120 | * |
||
121 | * @return \eZ\Publish\API\Repository\Repository |
||
122 | */ |
||
123 | public function getRepository($initializeFromScratch = true) |
||
138 | |||
139 | /** |
||
140 | * Returns a config value for $configKey. |
||
141 | * |
||
142 | * @param string $configKey |
||
143 | * |
||
144 | * @throws Exception if $configKey could not be found. |
||
145 | * |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function getConfigValue($configKey) |
||
152 | |||
153 | /** |
||
154 | * Returns a repository specific ID manager. |
||
155 | * |
||
156 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
157 | */ |
||
158 | public function getIdManager() |
||
162 | |||
163 | /** |
||
164 | * Insert the database data. |
||
165 | */ |
||
166 | public function insertData() |
||
226 | |||
227 | protected function getInitialVarDir() |
||
231 | |||
232 | protected function cleanupVarDir($sourceDir) |
||
245 | |||
246 | /** |
||
247 | * CLears internal in memory caches after inserting data circumventing the |
||
248 | * API. |
||
249 | */ |
||
250 | protected function clearInternalCaches() |
||
270 | |||
271 | /** |
||
272 | * Returns statements to be executed after data insert. |
||
273 | * |
||
274 | * @return string[] |
||
275 | */ |
||
276 | protected function getPostInsertStatements() |
||
286 | |||
287 | /** |
||
288 | * Returns the initial database data. |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | protected function getInitialData() |
||
301 | |||
302 | /** |
||
303 | * Initializes the database schema. |
||
304 | */ |
||
305 | protected function initializeSchema() |
||
315 | |||
316 | /** |
||
317 | * Applies the given SQL $statements to the database in use. |
||
318 | * |
||
319 | * @param array $statements |
||
320 | */ |
||
321 | protected function applyStatements(array $statements) |
||
327 | |||
328 | // ************* Setup copied and refactored from common.php ************ |
||
329 | |||
330 | /** |
||
331 | * Returns the database schema as an array of SQL statements. |
||
332 | * |
||
333 | * @return string[] |
||
334 | */ |
||
335 | protected function getSchemaStatements() |
||
341 | |||
342 | /** |
||
343 | * Returns the database handler from the service container. |
||
344 | * |
||
345 | * @return \eZ\Publish\Core\Persistence\Doctrine\ConnectionHandler |
||
346 | */ |
||
347 | protected function getDatabaseHandler() |
||
351 | |||
352 | /** |
||
353 | * Returns the service container used for initialization of the repository. |
||
354 | * |
||
355 | * @return \eZ\Publish\Core\Base\ServiceContainer |
||
356 | */ |
||
357 | protected function getServiceContainer() |
||
392 | |||
393 | /** |
||
394 | * This is intended to be used from external repository in order to |
||
395 | * enable container customization. |
||
396 | * |
||
397 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $containerBuilder |
||
398 | */ |
||
399 | protected function externalBuildContainer(ContainerBuilder $containerBuilder) |
||
403 | |||
404 | /** |
||
405 | * Get the Database name. |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | public function getDB() |
||
413 | } |
||
414 |
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.