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 |
||
20 | abstract class Repository |
||
21 | { |
||
22 | |||
23 | /** @var Container The application's DI container. */ |
||
24 | protected $container; |
||
25 | |||
26 | /** @var Connection The database connection to the meta database. */ |
||
27 | private $metaConnection; |
||
28 | |||
29 | /** @var Connection The database connection to the projects' databases. */ |
||
30 | private $projectsConnection; |
||
31 | |||
32 | /** @var Connection The database connection to other tools' databases. */ |
||
33 | private $toolsConnection; |
||
34 | |||
35 | /** @var CacheItemPoolInterface The cache. */ |
||
36 | protected $cache; |
||
37 | |||
38 | /** @var LoggerInterface The log. */ |
||
39 | protected $log; |
||
40 | |||
41 | /** @var Stopwatch The stopwatch for time profiling. */ |
||
42 | protected $stopwatch; |
||
43 | |||
44 | /** |
||
45 | * Create a new Repository with nothing but a null-logger. |
||
46 | */ |
||
47 | public function __construct() |
||
51 | |||
52 | /** |
||
53 | * Set the DI container. |
||
54 | * @param Container $container |
||
55 | */ |
||
56 | public function setContainer(Container $container) |
||
63 | |||
64 | /** |
||
65 | * Get the database connection for the 'meta' database. |
||
66 | * @return Connection |
||
67 | */ |
||
68 | protected function getMetaConnection() |
||
78 | |||
79 | /** |
||
80 | * Get the database connection for the 'projects' database. |
||
81 | * @return Connection |
||
82 | */ |
||
83 | protected function getProjectsConnection() |
||
93 | |||
94 | /** |
||
95 | * Get the database connection for the 'tools' database |
||
96 | * (the one that other tools store data in). |
||
97 | * @return Connection |
||
98 | */ |
||
99 | protected function getToolsConnection() |
||
109 | |||
110 | /** |
||
111 | * Get the API object for the given project. |
||
112 | * |
||
113 | * @param Project $project |
||
114 | * @return MediawikiApi |
||
115 | */ |
||
116 | public function getMediawikiApi(Project $project) |
||
126 | |||
127 | /** |
||
128 | * Is XTools connecting to MMF Labs? |
||
129 | * @return boolean |
||
130 | */ |
||
131 | public function isLabs() |
||
135 | |||
136 | /** |
||
137 | * Normalize and quote a table name for use in SQL. |
||
138 | * |
||
139 | * @param string $databaseName |
||
140 | * @param string $tableName |
||
141 | * @return string Fully-qualified and quoted table name. |
||
142 | */ |
||
143 | public function getTableName($databaseName, $tableName) |
||
168 | } |
||
169 |
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.