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 CallbackVisitor |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Return's a new visitor instance. |
||
| 39 | * |
||
| 40 | * @return \TechDivision\Import\Cli\Callbacks\CallbackVisitor 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 callbacks defined in the system configuration. |
||
| 64 | * |
||
| 65 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to prepare the callbacks for |
||
| 66 | * @param array $callbacks The array with the callbacks |
||
| 67 | * @param string $type The actual callback type |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function prepareCallbacks(SubjectInterface $subject, array $callbacks, $type = null) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Initialize and return a new callback 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 callback to instanciate |
||
| 96 | * |
||
| 97 | * @return \TechDivision\Import\Callbacks\CallbackInterface The callback instance |
||
| 98 | */ |
||
| 99 | public function callbackFactory(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.