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 | * All mapping tables to environment-specific names, as specified in config/table_map.yml |
||
| 62 | * Used for example to convert revision -> revision_replica |
||
| 63 | * https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs |
||
| 64 | * |
||
| 65 | * @param string $table Table name |
||
| 66 | * @param string $dbName Database name |
||
| 67 | * @param string|null $table_extension Optional table extension, which will only get used if we're on labs. |
||
| 68 | * |
||
| 69 | * @return string Converted table name |
||
| 70 | */ |
||
| 71 | public function getTable($table, $dbName = null, $table_extension = null) |
||
| 97 | |||
| 98 | // TODO: figure out how to use Doctrine to query host 'tools-db' |
||
| 99 | } |
||
| 100 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.