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 ORM extends Component implements ORMInterface, SingletonInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Memory section to store ORM schema. |
||
| 24 | */ |
||
| 25 | const MEMORY = 'orm.schema'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Already created instantiators. |
||
| 29 | * |
||
| 30 | * @invisible |
||
| 31 | * @var InstantiatorInterface[] |
||
| 32 | */ |
||
| 33 | private $instantiators = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * ORM schema. |
||
| 37 | * |
||
| 38 | * @invisible |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $schema = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var DatabaseManager |
||
| 45 | */ |
||
| 46 | protected $manager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var SchemaLocator |
||
| 50 | */ |
||
| 51 | protected $locator; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @invisible |
||
| 55 | * @var MemoryInterface |
||
| 56 | */ |
||
| 57 | protected $memory; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var FactoryInterface |
||
| 61 | */ |
||
| 62 | protected $factory; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param DatabaseManager $manager |
||
| 66 | * @param SchemaLocator $locator |
||
| 67 | * @param MemoryInterface $memory |
||
| 68 | * @param FactoryInterface $factory |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function __construct( |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Create instance of ORM SchemaBuilder. |
||
| 88 | * |
||
| 89 | * @param bool $locate Set to true to automatically locate available records and record sources |
||
| 90 | * sources in a project files (based on tokenizer scope). |
||
| 91 | * |
||
| 92 | * @return SchemaBuilder |
||
| 93 | * |
||
| 94 | * @throws SchemaException |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function schemaBuilder(bool $locate = true): SchemaBuilder |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Specify behaviour schema for ORM to be used. |
||
| 118 | * |
||
| 119 | * @param SchemaBuilder $builder |
||
| 120 | * @param bool $remember Set to true to remember packed schema in memory. |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function setSchema(SchemaBuilder $builder, bool $remember = false) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function define(string $class, int $property) |
|
| 152 | |||
| 153 | //other methods |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function instantiate( |
||
| 167 | |||
| 168 | //todo: __clone |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get object responsible for class instantiation. |
||
| 172 | * |
||
| 173 | * @param string $class |
||
| 174 | * |
||
| 175 | * @return InstantiatorInterface |
||
| 176 | */ |
||
| 177 | View Code Duplication | protected function instantiator(string $class): InstantiatorInterface |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Load packed schema from memory. |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | protected function loadSchema(): array |
||
| 206 | |||
| 207 | } |
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.