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 |
||
13 | class HasOne extends Attributes |
||
14 | { |
||
15 | /** |
||
16 | * Constructor. |
||
17 | * |
||
18 | * @param Embed[] $original Any original properties to apply. |
||
19 | */ |
||
20 | public function __construct(array $original = []) |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | public function areDirty() |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function calculateChangeSet() |
||
57 | { |
||
58 | $set = []; |
||
59 | View Code Duplication | foreach ($this->original as $key => $current) { |
|
60 | if (false === $current->isDirty()) { |
||
61 | continue; |
||
62 | } |
||
63 | $original = isset($this->original[$key]) ? $this->original[$key] : null; |
||
64 | $set[$key]['old'] = $original; |
||
65 | $set[$key]['new'] = $current; |
||
66 | } |
||
67 | |||
68 | View Code Duplication | foreach ($this->current as $key => $current) { |
|
69 | if (false === $current->isDirty()) { |
||
70 | continue; |
||
71 | } |
||
72 | $original = isset($this->original[$key]) ? $this->original[$key] : null; |
||
73 | $set[$key]['old'] = $original; |
||
74 | $set[$key]['new'] = $current; |
||
75 | } |
||
76 | |||
77 | View Code Duplication | foreach ($this->remove as $key) { |
|
78 | $set[$key]['old'] = $this->original[$key]; |
||
79 | $set[$key]['new'] = null; |
||
80 | } |
||
81 | ksort($set); |
||
82 | return $set; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function rollback() |
||
100 | } |
||
101 |
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.