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 |
||
7 | final class ArrayManipulator implements Manipulator |
||
8 | { |
||
9 | /** @var Pipeline */ |
||
10 | private $pipeline; |
||
11 | |||
12 | 15 | public function __construct() |
|
16 | |||
17 | 12 | View Code Duplication | public function append($key, $args) |
28 | |||
29 | 13 | private function setNestedValue(&$serializedBase, array $keysDepth, $value) |
|
30 | { |
||
31 | 12 | $currentKey = \current($keysDepth); |
|
32 | 12 | $nextKey = \next($keysDepth); |
|
33 | 12 | if (false === $nextKey) { |
|
34 | 12 | $serializedBase[$currentKey] = $value; |
|
35 | |||
36 | 13 | return $serializedBase; |
|
37 | } |
||
38 | |||
39 | 3 | return $this->setNestedValue($serializedBase[$currentKey], $keysDepth, $value); |
|
40 | } |
||
41 | |||
42 | 3 | View Code Duplication | public function filter($key) |
53 | |||
54 | 3 | private function unsetNestedValue(&$serializedBase, array $keysDepth) |
|
55 | { |
||
56 | 3 | $currentKey = \current($keysDepth); |
|
57 | 3 | $nextKey = \next($keysDepth); |
|
58 | 3 | if (!$nextKey) { |
|
59 | 3 | unset($serializedBase[$currentKey]); |
|
60 | |||
61 | 3 | return $serializedBase; |
|
62 | } |
||
63 | |||
64 | 3 | return $this->unsetNestedValue($serializedBase[$currentKey], $keysDepth); |
|
65 | } |
||
66 | |||
67 | 15 | public function process($serializedBase) |
|
74 | } |
||
75 |
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.