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 |
||
| 32 | View Code Duplication | abstract class AbstractProcessor implements ProcessorInterface |
|
|
|
|||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The utility class name with the SQL statements to use. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $utilityClassName; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The PDO connection instance. |
||
| 44 | * |
||
| 45 | * @var \PDO |
||
| 46 | */ |
||
| 47 | protected $connection; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Initialize the processor with the passed connection and utility class name. |
||
| 51 | * . |
||
| 52 | * @param \PDO $connection The PDO connection instance |
||
| 53 | * @param string $utilityClassName The utility class name |
||
| 54 | */ |
||
| 55 | public function __construct(\PDO $connection, $utilityClassName) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set's the passed utility class with the SQL statements to use. |
||
| 68 | * |
||
| 69 | * @param string $utilityClassName The utility class name |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | public function setUtilityClassName($utilityClassName) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Return's the utility class with the SQL statements to use. |
||
| 80 | * |
||
| 81 | * @return string The utility class name |
||
| 82 | */ |
||
| 83 | public function getUtilityClassName() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Sets's the initialized PDO connection. |
||
| 90 | * |
||
| 91 | * @param \PDO $connection The initialized PDO connection |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function setConnection(\PDO $connection) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Return's the initialized PDO connection. |
||
| 102 | * |
||
| 103 | * @return \PDO The initialized PDO connection |
||
| 104 | */ |
||
| 105 | public function getConnection() |
||
| 109 | } |
||
| 110 |
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.