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 |
||
| 35 | View Code Duplication | abstract class AbstractProcessor implements ProcessorInterface |
|
|
|
|||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The connection instance. |
||
| 40 | * . |
||
| 41 | * @var \TechDivision\Import\Connection\ConnectionInterface; |
||
| 42 | */ |
||
| 43 | protected $connection; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The respository instance with the SQL statements to use. |
||
| 47 | * |
||
| 48 | * @var \TechDivision\Import\Repositories\SqlStatementRepositoryInterface |
||
| 49 | */ |
||
| 50 | protected $sqlStatementRepository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initialize the processor with the passed connection and utility class name. |
||
| 54 | * . |
||
| 55 | * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection instance |
||
| 56 | * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The repository instance |
||
| 57 | */ |
||
| 58 | public function __construct( |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set's the connection to use. |
||
| 73 | * . |
||
| 74 | * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection instance |
||
| 75 | * |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function setConnection(ConnectionInterface $connection) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Return's the connection to use. |
||
| 85 | * |
||
| 86 | * @return \TechDivision\Import\Connection\ConnectionInterface The connection instance |
||
| 87 | */ |
||
| 88 | public function getConnection() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set's the repository instance with the SQL statements to use. |
||
| 95 | * |
||
| 96 | * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The repository instance |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function setSqlStatementRepository(SqlStatementRepositoryInterface $sqlStatementRepository) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Return's the repository instance with the SQL statements to use. |
||
| 107 | * |
||
| 108 | * @return \TechDivision\Import\Repositories\SqlStatementRepositoryInterface The repository instance |
||
| 109 | */ |
||
| 110 | public function getSqlStatementRepository() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Return's the class name of the SQL repository instance with the SQL statements to use. |
||
| 117 | * |
||
| 118 | * @return string The SQL repository instance class name |
||
| 119 | */ |
||
| 120 | public function getSqlStatementRepositoryClassName() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Load's the SQL statement with the passed ID from the SQL repository. |
||
| 127 | * |
||
| 128 | * @param string $id The ID of the SQL statement to load |
||
| 129 | * |
||
| 130 | * @return string The SQL statement with the passed ID |
||
| 131 | */ |
||
| 132 | public function loadStatement($id) |
||
| 136 | } |
||
| 137 |
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.