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 |
||
| 33 | View Code Duplication | abstract class AbstractDelegateBuilder implements Builder |
|
|
1 ignored issue
–
show
|
|||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constants |
||
| 38 | */ |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The name of the default delegate method |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const DEFAULT_DELEGATE_METHOD_NAME = 'buildModel'; |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Methods |
||
| 50 | */ |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | * |
||
| 55 | * @param mixed $incoming The input data |
||
| 56 | * @return mixed The built model |
||
| 57 | */ |
||
| 58 | 9 | public function build($incoming) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Get the delegate building callable |
||
| 67 | * |
||
| 68 | * Override this method if a custom delegate is desired |
||
| 69 | * |
||
| 70 | * @return callable The delegate builder callable |
||
| 71 | */ |
||
| 72 | 9 | protected function getDelegate(): callable |
|
| 82 | |||
| 83 | /** |
||
| 84 | * The delegate build method |
||
| 85 | * |
||
| 86 | * This doc-block and commented out abstract method is provided here to show |
||
| 87 | * what the delegate method signature WOULD be if PHP allowed the proper |
||
| 88 | * typing support to enable a generic definition in this manner |
||
| 89 | * |
||
| 90 | * See the class description for more info |
||
| 91 | * |
||
| 92 | * @param IncomingDataType $incoming The input data |
||
| 93 | * @return ModelType The built model |
||
| 94 | */ |
||
| 95 | // abstract protected function buildModel(IncomingDataType $incoming): ModelType; |
||
| 96 | } |
||
| 97 |
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.