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 |
||
| 19 | class RecordSchema implements SchemaInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var ReflectionEntity |
||
| 23 | */ |
||
| 24 | private $reflection; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @invisible |
||
| 28 | * @var MutatorsConfig |
||
| 29 | */ |
||
| 30 | private $mutatorsConfig; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var ColumnRenderer |
||
| 34 | */ |
||
| 35 | private $renderer; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param ReflectionEntity $reflection |
||
| 39 | * @param MutatorsConfig $mutators |
||
| 40 | * @param ColumnRenderer|null $rendered |
||
| 41 | */ |
||
| 42 | public function __construct( |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function getClass(): string |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return ReflectionEntity |
||
| 62 | */ |
||
| 63 | public function getReflection(): ReflectionEntity |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function getInstantiator(): string |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function getDatabase() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function getTable(): string |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns set of declared indexes. |
||
| 107 | * |
||
| 108 | * Example: |
||
| 109 | * const INDEXES = [ |
||
| 110 | * [self::UNIQUE, 'email'], |
||
| 111 | * [self::INDEX, 'status', 'balance'], |
||
| 112 | * [self::INDEX, 'public_id'] |
||
| 113 | * ]; |
||
| 114 | * |
||
| 115 | * @do generator |
||
| 116 | * |
||
| 117 | * @return \Generator|IndexDefinition[] |
||
| 118 | * |
||
| 119 | * @throws DefinitionException |
||
| 120 | */ |
||
| 121 | public function getIndexes(): \Generator |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function defineTable(AbstractTable $table): AbstractTable |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function getRelations(): array |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function packSchema(SchemaBuilder $builder, AbstractTable $table = null): array |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | View Code Duplication | protected function getFields(): array |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Check if field schema/type defines relation. |
||
| 176 | * |
||
| 177 | * @param mixed $type |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | protected function isRelation($type): bool |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param array $definition |
||
| 192 | * |
||
| 193 | * @return IndexDefinition |
||
| 194 | * |
||
| 195 | * @throws DefinitionException |
||
| 196 | */ |
||
| 197 | protected function castIndex(array $definition) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Default defined values. |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | protected function createDefaults(): array |
||
| 236 | } |
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.