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 |
||
| 11 | class CallbackManager |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var ObjectManager |
||
| 15 | */ |
||
| 16 | protected $objectManager; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $class; |
||
| 22 | |||
| 23 | public function __construct(ObjectManager $objectManager, string $class) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return ObjectRepository |
||
| 31 | */ |
||
| 32 | protected function getRepository() : ObjectRepository |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | View Code Duplication | public function getClass() : string |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return CallbackInterface |
||
| 51 | */ |
||
| 52 | public function createCallback() : CallbackInterface |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Request $request |
||
| 61 | * @return CallbackInterface |
||
| 62 | */ |
||
| 63 | public function createCallbackFromRequest(Request $request) : CallbackInterface |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param CallbackInterface $callback |
||
| 107 | */ |
||
| 108 | public function deleteCallback(CallbackInterface $callback) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param CallbackInterface $callback |
||
| 116 | * @param bool $flush |
||
| 117 | */ |
||
| 118 | public function updateCallback(CallbackInterface $callback, bool $flush = true) |
||
| 126 | } |
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.