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 |
||
| 35 | View Code Duplication | class CallbackVisitor |
|
|
|
|||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The DI container builder instance. |
||
| 40 | * |
||
| 41 | * @var \Symfony\Component\DependencyInjection\TaggedContainerInterface |
||
| 42 | */ |
||
| 43 | protected $container; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The constructor to initialize the instance. |
||
| 47 | * |
||
| 48 | * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface The container instance |
||
| 49 | */ |
||
| 50 | public function __construct(TaggedContainerInterface $container) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Visitor implementation that initializes the observers of the passed subject. |
||
| 57 | * |
||
| 58 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to initialize the observers for |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | public function visit(SubjectInterface $subject) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Prepare the callbacks defined in the system configuration. |
||
| 72 | * |
||
| 73 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to prepare the callbacks for |
||
| 74 | * @param array $callbacks The array with the callbacks |
||
| 75 | * @param string $type The actual callback type |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function prepareCallbacks(SubjectInterface $subject, array $callbacks, $type = null) |
||
| 99 | } |
||
| 100 |
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.