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 |
||
| 12 | class Accessor implements AccessorInterface, ActiveNodeFactoryInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Locator |
||
| 16 | */ |
||
| 17 | private $locator; |
||
| 18 | /** |
||
| 19 | * @var Registry |
||
| 20 | */ |
||
| 21 | private $registry; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param Registry $registry |
||
| 25 | */ |
||
| 26 | 18 | public function __construct(Registry $registry) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @inheritDoc |
||
| 34 | */ |
||
| 35 | 12 | View Code Duplication | public function getNode(&$root, $path) |
| 42 | |||
| 43 | /** |
||
| 44 | * @inheritDoc |
||
| 45 | */ |
||
| 46 | 5 | View Code Duplication | public function findNode(&$root, $path) |
| 53 | |||
| 54 | 17 | private function traverse(&$root, $path, Context $context) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritDoc |
||
| 62 | */ |
||
| 63 | 7 | public function read(&$root, $path) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritDoc |
||
| 72 | */ |
||
| 73 | 1 | public function enumerate(&$root, $path) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritDoc |
||
| 88 | */ |
||
| 89 | 5 | public function write(&$root, $path, $value) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritDoc |
||
| 106 | */ |
||
| 107 | 5 | public function exists($root, $path) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param $value |
||
| 115 | * @param array $path |
||
| 116 | * |
||
| 117 | * @return TypeAccessorInterface |
||
| 118 | * |
||
| 119 | * @throws IllegalTargetException |
||
| 120 | */ |
||
| 121 | 5 | private function getAccessor($value, array $path) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @inheritDoc |
||
| 132 | */ |
||
| 133 | 1 | public function wrap(&$structure) |
|
| 137 | } |
||
| 138 |
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.