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 |
||
| 9 | abstract class LocationBase extends CComponent implements ILocationGenerator |
||
| 10 | {
|
||
| 11 | /** |
||
| 12 | * @var CDataProviderIterator |
||
| 13 | */ |
||
| 14 | protected $_modelSource; |
||
| 15 | /** |
||
| 16 | * @var CController |
||
| 17 | */ |
||
| 18 | protected $_controller; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param CController $controller |
||
| 22 | */ |
||
| 23 | public function __construct(CController $controller) |
||
| 24 | {
|
||
| 25 | $this->_controller = $controller; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | public abstract function getRoute(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public abstract function current(); |
||
| 37 | |||
| 38 | public function next() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return int |
||
| 45 | */ |
||
| 46 | public function key() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | public function valid() |
||
| 58 | |||
| 59 | public function rewind() |
||
| 63 | |||
| 64 | public function unique(array $keys, array $dataSource) |
||
| 92 | } |
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.