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 Updater |
||
16 | { |
||
17 | const __CLASS = __CLASS__; |
||
18 | |||
19 | /** |
||
20 | * @var Anonymizer |
||
21 | */ |
||
22 | private $anonymizer; |
||
23 | /** |
||
24 | * @var null|resource |
||
25 | */ |
||
26 | private $outputStream = null; |
||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $showProgress = true; |
||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | private $progressSteps = 1; |
||
35 | |||
36 | /** |
||
37 | * @var AnonymizableEntity |
||
38 | */ |
||
39 | private $entityModel; |
||
40 | |||
41 | public function __construct(Anonymizer $anonymizer) |
||
45 | /** |
||
46 | * @param null|resource $stream writable stream resource or null if no output required |
||
47 | */ |
||
48 | public function setOutputStream($stream) |
||
52 | |||
53 | /** |
||
54 | * @param boolean $showProgress |
||
55 | */ |
||
56 | public function setShowProgress($showProgress) |
||
60 | |||
61 | /** |
||
62 | * @param int $progressSteps |
||
63 | */ |
||
64 | public function setProgressSteps($progressSteps) |
||
68 | |||
69 | public function update(CollectionIterator $iterator, AnonymizableEntity $entityModel) |
||
78 | |||
79 | /** |
||
80 | * @param CollectionIterator $iterator |
||
81 | */ |
||
82 | public function updateCurrentRow(CollectionIterator $iterator) |
||
90 | |||
91 | private function outputStart() |
||
97 | |||
98 | /** |
||
99 | * @param CollectionIterator $iterator |
||
100 | */ |
||
101 | private function outputIteratorStatus(CollectionIterator $iterator) |
||
112 | |||
113 | private function outputDone() |
||
119 | } |
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.