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 |
||
| 15 | class JobEnqueue extends Command |
||
| 16 | { |
||
| 17 | use ManagerRegistryAwareTrait; |
||
| 18 | use LoggerAwareTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * {@inheritdoc} |
||
| 22 | */ |
||
| 23 | protected $signature = 'job:enqueue'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var EnqueueStoredJobsService |
||
| 27 | */ |
||
| 28 | private $service; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | private $terminating = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param EnqueueStoredJobsService $service |
||
| 37 | * @param ManagerRegistry|null $managerRegistry |
||
| 38 | * @param LoggerInterface|null $logger |
||
| 39 | */ |
||
| 40 | 4 | View Code Duplication | public function __construct( |
| 50 | |||
| 51 | 4 | public function handle() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 4 | private function topicName(): string |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param bool $condition |
||
| 99 | * @param string $logMessage |
||
| 100 | */ |
||
| 101 | 3 | private function flushAndLog(bool $condition, string $logMessage): void |
|
| 109 | |||
| 110 | private function terminate() |
||
| 115 | |||
| 116 | } |
||
| 117 |
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.