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 LabsHelper |
||
| 16 | { |
||
| 17 | /** @var string The current database name. */ |
||
| 18 | protected $dbName; |
||
| 19 | |||
| 20 | /** @var Connection The database connection. */ |
||
| 21 | protected $client; |
||
| 22 | |||
| 23 | /** @var ContainerInterface The DI container. */ |
||
| 24 | protected $container; |
||
| 25 | |||
| 26 | /** @var string The project URL. */ |
||
| 27 | protected $url; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * LabsHelper constructor. |
||
| 31 | * @param ContainerInterface $container |
||
| 32 | */ |
||
| 33 | public function __construct(ContainerInterface $container) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Check to see if a given tool is enabled. |
||
| 40 | * @param string $tool The tool short name. |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public function checkEnabled($tool) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Is xTools connecting to WMF Labs? |
||
| 52 | * |
||
| 53 | * @return boolean |
||
| 54 | */ |
||
| 55 | public function isLabs() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the record for the given project in the meta.wiki table |
||
| 62 | * @param string $project Valid project in the formats: |
||
| 63 | * https://en.wikipedia.org, en.wikipedia, enwiki |
||
| 64 | * @return array|false Database record or false if no record was found. |
||
| 65 | * Relevant values returned include the 'dbname' (enwiki), |
||
| 66 | * 'lang', 'name' (Wikipedia) and 'url' (https://en.wikipeda.org) |
||
| 67 | */ |
||
| 68 | private function getProjectMetadata($project) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get a list of all projects. |
||
| 122 | */ |
||
| 123 | public function allProjects() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * All mapping tables to environment-specific names, as specified in config/table_map.yml |
||
| 134 | * Used for example to convert revision -> revision_replica |
||
| 135 | * https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs |
||
| 136 | * |
||
| 137 | * @param string $table Table name |
||
| 138 | * @param string $dbName Database name |
||
| 139 | * @param string|null $table_extension Optional table extension, which will only get used if we're on labs. |
||
| 140 | * |
||
| 141 | * @return string Converted table name |
||
| 142 | */ |
||
| 143 | public function getTable($table, $dbName = null, $table_extension = null) |
||
| 169 | |||
| 170 | // TODO: figure out how to use Doctrine to query host 'tools-db' |
||
| 171 | } |
||
| 172 |