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 |
||
| 11 | abstract class AbstractManager implements ManagerInterface |
||
| 12 | { |
||
| 13 | /** @var EntityManager */ |
||
| 14 | protected $entityManager; |
||
| 15 | |||
| 16 | /** @var EntityRepository */ |
||
| 17 | protected $repository; |
||
| 18 | |||
| 19 | private $modeTransactionnal = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param EntityManager $entityManager |
||
| 23 | * @param $entity |
||
| 24 | */ |
||
| 25 | public function __construct(EntityManager $entityManager, $entity) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param AbstractEntity $object |
||
| 33 | * |
||
| 34 | * @return AbstractEntity|boolean |
||
| 35 | * |
||
| 36 | * @throws ObjectClassNotAllowedException |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function insert(AbstractEntity $object) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Permet de gérer un flush en mode transactions manuelles. |
||
| 55 | */ |
||
| 56 | private function flush() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param AbstractEntity $object |
||
| 66 | * |
||
| 67 | * @return AbstractEntity|boolean |
||
| 68 | * |
||
| 69 | * @throws ObjectClassNotAllowedException |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function update(AbstractEntity $object) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param Entity $object |
||
| 87 | * @throws ObjectClassNotAllowedException |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function delete(AbstractEntity $object) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Finds an entity by its primary key / identifier. |
||
| 103 | * |
||
| 104 | * @param mixed $id The identifier. |
||
| 105 | * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
||
| 106 | * or NULL if no specific lock mode should be used |
||
| 107 | * during the search. |
||
| 108 | * @param int|null $lockVersion The lock version. |
||
| 109 | * |
||
| 110 | * @return Entity|object|null The entity instance or NULL if the entity can not be found. |
||
| 111 | */ |
||
| 112 | public function find($id, $lockMode = null, $lockVersion = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Finds a single entity by a set of criteria. |
||
| 119 | * |
||
| 120 | * @param array $criteria |
||
| 121 | * @param array|null $orderBy |
||
| 122 | * |
||
| 123 | * @return Entity|object|null The entity instance or NULL if the entity can not be found. |
||
| 124 | */ |
||
| 125 | public function findOneBy(array $criteria, array $orderBy = null) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Finds entities by a set of criteria. |
||
| 132 | * |
||
| 133 | * @param array $criteria |
||
| 134 | * @param array|null $orderBy |
||
| 135 | * @param int|null $limit |
||
| 136 | * @param int|null $offset |
||
| 137 | * |
||
| 138 | * @return Entity[] The objects. |
||
| 139 | */ |
||
| 140 | public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Finds all entities in the repository. |
||
| 147 | * |
||
| 148 | * @return array The entities. |
||
| 149 | */ |
||
| 150 | public function findAll() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return EntityRepository |
||
| 157 | */ |
||
| 158 | public function getRepository() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Vide l'UnitOfWork de l'entity manager. |
||
| 165 | */ |
||
| 166 | public function clear() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Permet de passer en gestion des transations manuelles. (Conseillé par SensioLabs). |
||
| 173 | */ |
||
| 174 | public function beginTransaction() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Dans le cas d'une gestion des transactions manuelles en cas d'échec on rollback le tout. |
||
| 182 | */ |
||
| 183 | public function rollback() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Permet d'extraire uniquement les champs désirés. |
||
| 194 | * |
||
| 195 | * @param array $array |
||
| 196 | * @param array $fields |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function exportFields(array $array, array $fields = []) |
||
| 212 | |||
| 213 | } |
||
| 214 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: