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 |
||
16 | class EnqueueSingleStoredJobService |
||
17 | { |
||
18 | /** |
||
19 | * @var PsrContext |
||
20 | */ |
||
21 | private $context; |
||
22 | |||
23 | /** |
||
24 | * @var JobStore |
||
25 | */ |
||
26 | protected $jobStore; |
||
27 | |||
28 | /** |
||
29 | * @var StoredJobSerializer |
||
30 | */ |
||
31 | private $storedJobSerializer; |
||
32 | |||
33 | /** |
||
34 | * @var JobFlightManager |
||
35 | */ |
||
36 | private $jobFlightManager; |
||
37 | |||
38 | /** |
||
39 | * @param PsrContext $context |
||
40 | * @param JobStore $jobStore |
||
41 | * @param StoredJobSerializer $storedJobSerializer |
||
42 | * @param JobFlightManager|null $jobFlightManager |
||
43 | */ |
||
44 | View Code Duplication | public function __construct( |
|
55 | |||
56 | /** |
||
57 | * @param string $topicName |
||
58 | * @param int|null $storedJobId |
||
59 | * @throws FailedToEnqueueStoredJobException |
||
60 | * @throws StoredJobNotFoundException |
||
61 | */ |
||
62 | public function execute(string $topicName, int $storedJobId = null) |
||
83 | |||
84 | /** |
||
85 | * @return PsrProducer |
||
86 | */ |
||
87 | protected function createProducer(): PsrProducer |
||
93 | |||
94 | /** |
||
95 | * @param string $topicName |
||
96 | * @return PsrTopic |
||
97 | */ |
||
98 | protected function createTopic(string $topicName): PsrTopic |
||
104 | |||
105 | /** |
||
106 | * @param StoredJob $storedJob |
||
107 | * @return PsrMessage |
||
108 | */ |
||
109 | protected function createMessage(StoredJob $storedJob): PsrMessage |
||
115 | } |
||
116 |
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.