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 |
||
| 20 | class DocumentInstantiator implements InstantiatorInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @invisible |
||
| 24 | * @var ODMInterface |
||
| 25 | */ |
||
| 26 | private $odm; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Primary instantiation class. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $class = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Normalized schema delivered by DocumentSchema. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $schema = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param ODMInterface $odm |
||
| 44 | * @param string $class |
||
| 45 | * @param array $schema |
||
| 46 | */ |
||
| 47 | public function __construct(ODMInterface $odm, string $class, array $schema) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | * |
||
| 57 | * @return CompositableInterface|DocumentEntity|Document |
||
| 58 | * |
||
| 59 | * @throws InstantionException |
||
| 60 | */ |
||
| 61 | public function instantiate($fields, bool $filter = true): CompositableInterface |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Define document class using it's fieldset and definition. |
||
| 95 | * |
||
| 96 | * @param \ArrayAccess|array $fields |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | * |
||
| 100 | * @throws DefinitionException |
||
| 101 | */ |
||
| 102 | protected function defineClass($fields) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param \Traversable|\ArrayAccess $fields |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | View Code Duplication | private function normalizeFields($fields): array |
|
| 146 | } |
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.