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 |
||
| 4 | View Code Duplication | class Factory |
|
|
|
|||
| 5 | { |
||
| 6 | const DRIVER_FTP = 'Ftp'; |
||
| 7 | const DRIVER_SFTP = 'Sftp'; |
||
| 8 | const DRIVER_FTP_IMPLICIT_SSL = 'FtpImplicitSSL'; |
||
| 9 | |||
| 10 | private static $instance; |
||
| 11 | |||
| 12 | 1 | private function __construct() |
|
| 15 | |||
| 16 | /** |
||
| 17 | * Design pattern Singleton |
||
| 18 | * @codeCoverageIgnore |
||
| 19 | */ |
||
| 20 | private function __clone() |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return self |
||
| 26 | */ |
||
| 27 | 4 | final public static function getInstance() |
|
| 28 | { |
||
| 29 | 4 | if (!self::$instance) { |
|
| 30 | 1 | $class = get_called_class(); |
|
| 31 | 1 | self::$instance = new $class; |
|
| 32 | } |
||
| 33 | 4 | return self::$instance; |
|
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $driver |
||
| 38 | * @param array $params |
||
| 39 | * @return FtpInterface |
||
| 40 | * @throws Exception |
||
| 41 | */ |
||
| 42 | 4 | final public function create($driver = self::DRIVER_FTP, $params = array()) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Get full class name to create |
||
| 57 | * @param string $driver |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | 2 | protected function getClassNameForDriver($driver) |
|
| 64 | } |
||
| 65 |
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.