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 |
||
| 36 | class GenericValidationObserver extends AbstractObserver implements ObserverFactoryInterface |
||
| 37 | { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The registry processor instance. |
||
| 41 | * |
||
| 42 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 43 | */ |
||
| 44 | protected $registryProcessor; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Array with virtual column name mappings (this is a temporary |
||
| 48 | * solution till techdivision/import#179 as been implemented). |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $reverseHeaderMappings = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initializes the observer with the registry processor instance. |
||
| 56 | * |
||
| 57 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 58 | */ |
||
| 59 | public function __construct(RegistryProcessorInterface $registryProcessor) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Will be invoked by the observer visitor when a factory has been defined to create the observer instance. |
||
| 66 | * |
||
| 67 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
||
| 68 | * |
||
| 69 | * @return \TechDivision\Import\Observers\ObserverInterface The observer instance |
||
| 70 | */ |
||
| 71 | public function createObserver(SubjectInterface $subject) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Will be invoked by the action on the events the listener has been registered for. |
||
| 83 | * |
||
| 84 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
||
| 85 | * |
||
| 86 | * @return array The modified row |
||
| 87 | * @see \TechDivision\Import\Observers\ObserverInterface::handle() |
||
| 88 | */ |
||
| 89 | public function handle(SubjectInterface $subject) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Process the observer's business logic. |
||
| 105 | * |
||
| 106 | * @return array The processed row |
||
| 107 | */ |
||
| 108 | View Code Duplication | protected function process() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Reverse map the passed header name, to the original column name. |
||
| 146 | * |
||
| 147 | * @param string $headerName The header name to reverse map |
||
| 148 | * |
||
| 149 | * @return string The original column name |
||
| 150 | */ |
||
| 151 | protected function reverseMapHeaderNameToColumnName(string $headerName) : string |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return's the array with callbacks for the passed type. |
||
| 158 | * |
||
| 159 | * @param string $type The type of the callbacks to return |
||
| 160 | * |
||
| 161 | * @return array The callbacks |
||
| 162 | */ |
||
| 163 | protected function getCallbacksByType($type) |
||
| 167 | } |
||
| 168 |
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.