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 |
||
| 8 | class EntityFactory implements EntityFactoryInterface |
||
| 9 | { |
||
| 10 | protected $db; |
||
| 11 | protected $tables; |
||
| 12 | protected $namespaces = []; |
||
| 13 | protected $defaultEntity; |
||
| 14 | protected $queryFactory; |
||
| 15 | protected $fieldFactory; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Constructor. |
||
| 19 | * |
||
| 20 | * @param QueryFactory|null $queryFactory |
||
| 21 | * @param FieldFactory|null $fieldFactory |
||
| 22 | */ |
||
| 23 | public function __construct(QueryFactory $queryFactory = null, FieldFactory $fieldFactory = null) |
||
| 24 | { |
||
| 25 | $this->setQueryFactory($queryFactory ?: new QueryFactory()); |
||
| 26 | $this->setFieldFactory($fieldFactory ?: new FieldFactory()); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @see EntityFactoryInterface |
||
| 31 | * |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | public function setDb(SimpleCrud $db) |
||
| 35 | { |
||
| 36 | $this->db = $db; |
||
| 37 | |||
| 38 | return $this; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Add a namespace for the entities classes. |
||
| 43 | * |
||
| 44 | * @param string $namespace |
||
| 45 | * |
||
| 46 | * @return self |
||
| 47 | */ |
||
| 48 | public function addNamespace($namespace) |
||
| 49 | { |
||
| 50 | array_unshift($this->namespaces, $namespace); |
||
| 51 | |||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Set the QueryFactory instance used by the entities. |
||
| 57 | * |
||
| 58 | * @param QueryFactory $queryFactory |
||
| 59 | * |
||
| 60 | * @return self |
||
| 61 | */ |
||
| 62 | public function setQueryFactory(QueryFactory $queryFactory) |
||
| 63 | { |
||
| 64 | $this->queryFactory = $queryFactory; |
||
| 65 | |||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the QueryFactory instance used by the entities. |
||
| 71 | * |
||
| 72 | * @return QueryFactory |
||
| 73 | */ |
||
| 74 | public function getQueryFactory() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set the FieldFactory instance used by the entities. |
||
| 81 | * |
||
| 82 | * @param FieldFactory $fieldFactory |
||
| 83 | * |
||
| 84 | * @return self |
||
| 85 | */ |
||
| 86 | public function setFieldFactory(FieldFactory $fieldFactory) |
||
| 87 | { |
||
| 88 | $this->fieldFactory = $fieldFactory; |
||
| 89 | |||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the FieldFactory instance used by the entities. |
||
| 95 | * |
||
| 96 | * @return FieldFactory |
||
| 97 | */ |
||
| 98 | public function getFieldFactory() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set whether the entities are autocreated or not. |
||
| 105 | * |
||
| 106 | * @param string $defaultEntity Default class used by the entities |
||
| 107 | * |
||
| 108 | * @return self |
||
| 109 | */ |
||
| 110 | public function setAutocreate($defaultEntity = 'SimpleCrud\\Entity') |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @see EntityFactoryInterface |
||
| 119 | * |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function has($name) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @see EntityFactoryInterface |
||
| 141 | * |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function get($name) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns all tables in the database. |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | private function getTables() |
||
| 185 | } |
||
| 186 |
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.