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 |
||
14 | class MediaRepository extends EntityRepository |
||
15 | { |
||
16 | /** |
||
17 | * Get the query by model name and model id |
||
18 | * @param $model |
||
19 | * @param $model_id |
||
20 | * @return \Doctrine\ORM\Query |
||
21 | */ |
||
22 | View Code Duplication | public function queryForModelAndId($model, $model_id) |
|
|
|||
23 | { |
||
24 | $query = $this->_em->createQueryBuilder() |
||
25 | ->select('m') |
||
26 | ->from($this->_entityName,'m') |
||
27 | ->where('m.model = :model') |
||
28 | ->andWhere('m.modelId = :modelId') |
||
29 | ->setParameter('model',$model) |
||
30 | ->setParameter('modelId',$model_id) |
||
31 | ->getQuery() |
||
32 | ; |
||
33 | return $query; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Get query for model name |
||
38 | * @param $model |
||
39 | * @return \Doctrine\ORM\Query |
||
40 | */ |
||
41 | View Code Duplication | public function queryForModel($model) |
|
42 | { |
||
43 | $query = $this->_em->createQueryBuilder() |
||
44 | ->select('m') |
||
45 | ->from($this->_entityName,'m') |
||
46 | ->where('m.model = :model') |
||
47 | ->setParameter('model',$model) |
||
48 | ->getQuery() |
||
49 | ; |
||
50 | return $query; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * get query for an array objects |
||
55 | * @param array $model_info |
||
56 | * @return \Doctrine\ORM\Query |
||
57 | */ |
||
58 | public function queryForArray(array $model_info) |
||
59 | { |
||
60 | $query = $this->_em->createQueryBuilder() |
||
61 | ->select('m') |
||
62 | ->from($this->_entityName,'m') |
||
63 | ->where('m.model IN(:model)') |
||
64 | ->andWhere('m.modelId IN(:modelId)') |
||
65 | ->setParameter('model',array_values($model_info['models'])) |
||
66 | ->setParameter('modelId',array_values($model_info['ids'])) |
||
67 | ->getQuery() |
||
68 | ; |
||
69 | return $query; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * find media for an array objects |
||
74 | * @param array $model_info |
||
75 | * @return array |
||
76 | */ |
||
77 | public function findForArrayModels(array $model_info){ |
||
81 | |||
82 | /** |
||
83 | * find media by model name and model id |
||
84 | * @param $model |
||
85 | * @param $model_id |
||
86 | * @return array |
||
87 | */ |
||
88 | public function findForModelAndId($model, $model_id) |
||
93 | |||
94 | /** |
||
95 | * Get media by model name |
||
96 | * @param $model |
||
97 | * @return array |
||
98 | */ |
||
99 | public function findForModel($model) |
||
104 | |||
105 | } |
||
106 |
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.