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 |
||
| 34 | class ObserverVisitor |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Return's a new visitor instance. |
||
| 39 | * |
||
| 40 | * @return \TechDivision\Import\Cli\Observers\ObserverVisitor The visitor instance |
||
| 41 | */ |
||
| 42 | public static function get() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Visitor implementation that initializes the observers of the passed subject. |
||
| 49 | * |
||
| 50 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to initialize the observers for |
||
| 51 | * |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public function visit(SubjectInterface $subject) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Prepare the observers defined in the system configuration. |
||
| 64 | * |
||
| 65 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to prepare the observers for |
||
| 66 | * @param array $observers The array with the observers |
||
| 67 | * @param string $type The actual observer type |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | View Code Duplication | protected function prepareObservers(SubjectInterface $subject, array $observers, $type = null) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Initialize and return a new observer of the passed type. |
||
| 93 | * |
||
| 94 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to create the observer for |
||
| 95 | * @param string $className The type of the observer to instanciate |
||
| 96 | * |
||
| 97 | * @return \TechDivision\Import\Observers\ObserverInterface The observer instance |
||
| 98 | */ |
||
| 99 | protected function observerFactory(SubjectInterface $subject, $className) |
||
| 103 | } |
||
| 104 |
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.