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 |
||
| 31 | trait RepositoryTrait |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @return EntityManager |
||
| 35 | */ |
||
| 36 | abstract protected function getEntityManager(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns the class name for this entity. |
||
| 40 | * |
||
| 41 | * This method is public to more naturally match ObjectRepository. |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | abstract public function getClassName(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Creates a new QueryBuilder instance that is prepopulated for this entity name. |
||
| 49 | * |
||
| 50 | * @param string $alias |
||
| 51 | * @param string $indexBy The index for the from. |
||
|
|
|||
| 52 | * |
||
| 53 | * @return QueryBuilder |
||
| 54 | */ |
||
| 55 | public function createQueryBuilder($alias, $indexBy = null) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Creates a new result set mapping builder for this entity. |
||
| 64 | * |
||
| 65 | * The column naming strategy is "INCREMENT". |
||
| 66 | * |
||
| 67 | * @param string $alias |
||
| 68 | * |
||
| 69 | * @return ResultSetMappingBuilder |
||
| 70 | */ |
||
| 71 | public function createResultSetMappingBuilder($alias) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Creates a new Query instance based on a predefined metadata named query. |
||
| 81 | * |
||
| 82 | * @param string $queryName |
||
| 83 | * |
||
| 84 | * @return Query |
||
| 85 | */ |
||
| 86 | public function createNamedQuery($queryName) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Creates a native SQL query. |
||
| 93 | * |
||
| 94 | * @param string $queryName |
||
| 95 | * |
||
| 96 | * @return NativeQuery |
||
| 97 | */ |
||
| 98 | public function createNativeNamedQuery($queryName) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Clears the repository, causing all managed entities to become detached. |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function clear() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Finds an entity by its primary key / identifier. |
||
| 119 | * |
||
| 120 | * @param mixed $id The identifier. |
||
| 121 | * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
||
| 122 | * or NULL if no specific lock mode should be used |
||
| 123 | * during the search. |
||
| 124 | * @param int|null $lockVersion The lock version. |
||
| 125 | * |
||
| 126 | * @return object|null The entity instance or NULL if the entity can not be found. |
||
| 127 | */ |
||
| 128 | public function find($id, $lockMode = null, $lockVersion = null) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Finds all entities in the repository. |
||
| 135 | * |
||
| 136 | * @return array The entities. |
||
| 137 | */ |
||
| 138 | public function findAll() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Finds entities by a set of criteria. |
||
| 145 | * |
||
| 146 | * @param array $criteria |
||
| 147 | * @param array|null $orderBy |
||
| 148 | * @param int|null $limit |
||
| 149 | * @param int|null $offset |
||
| 150 | * |
||
| 151 | * @return array The objects. |
||
| 152 | */ |
||
| 153 | public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Finds a single entity by a set of criteria. |
||
| 162 | * |
||
| 163 | * @param array $criteria |
||
| 164 | * @param array|null $orderBy |
||
| 165 | * |
||
| 166 | * @return object|null The entity instance or NULL if the entity can not be found. |
||
| 167 | */ |
||
| 168 | public function findOneBy(array $criteria, array $orderBy = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Adds support for magic finders. |
||
| 177 | * |
||
| 178 | * @param string $method |
||
| 179 | * @param array $arguments |
||
| 180 | * |
||
| 181 | * @return array|object The found entity/entities. |
||
| 182 | * |
||
| 183 | * @throws ORMException |
||
| 184 | * @throws \BadMethodCallException If the method called is an invalid find* method |
||
| 185 | * or no find* method at all and therefore an invalid |
||
| 186 | * method call. |
||
| 187 | */ |
||
| 188 | public function __call($method, $arguments) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Select all elements from a selectable that match the expression and |
||
| 238 | * return a new collection containing these elements. |
||
| 239 | * |
||
| 240 | * @param \Doctrine\Common\Collections\Criteria $criteria |
||
| 241 | * |
||
| 242 | * @return \Doctrine\Common\Collections\Collection |
||
| 243 | */ |
||
| 244 | public function matching(Criteria $criteria) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return \Doctrine\ORM\Mapping\ClassMetadata |
||
| 253 | */ |
||
| 254 | private function getClassMetadata() |
||
| 258 | } |
||
| 259 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.