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 |
||
24 | class UserRepository extends EntityRepository |
||
25 | { |
||
26 | /** |
||
27 | * @param string $keyword |
||
28 | * |
||
29 | * @return mixed |
||
30 | */ |
||
31 | View Code Duplication | public function searchUserByKeyword($keyword) |
|
51 | |||
52 | /** |
||
53 | * Get course user relationship based in the course_rel_user table. |
||
54 | * @return array |
||
55 | */ |
||
56 | /*public function getCourses(User $user) |
||
57 | { |
||
58 | $queryBuilder = $this->createQueryBuilder('user'); |
||
59 | |||
60 | // Selecting course info. |
||
61 | $queryBuilder->select('c'); |
||
62 | |||
63 | // Loading User. |
||
64 | //$qb->from('Chamilo\UserBundle\Entity\User', 'u'); |
||
65 | |||
66 | // Selecting course |
||
67 | $queryBuilder->innerJoin('Chamilo\CoreBundle\Entity\Course', 'c'); |
||
68 | |||
69 | //@todo check app settings |
||
70 | //$qb->add('orderBy', 'u.lastname ASC'); |
||
71 | |||
72 | $wherePart = $queryBuilder->expr()->andx(); |
||
73 | |||
74 | // Get only users subscribed to this course |
||
75 | $wherePart->add($queryBuilder->expr()->eq('user.userId', $user->getUserId())); |
||
76 | |||
77 | $queryBuilder->where($wherePart); |
||
78 | $query = $queryBuilder->getQuery(); |
||
79 | |||
80 | return $query->execute(); |
||
81 | } |
||
82 | |||
83 | public function getTeachers() |
||
84 | { |
||
85 | $queryBuilder = $this->createQueryBuilder('u'); |
||
86 | |||
87 | // Selecting course info. |
||
88 | $queryBuilder |
||
89 | ->select('u') |
||
90 | ->where('u.groups.id = :groupId') |
||
91 | ->setParameter('groupId', 1); |
||
92 | |||
93 | $query = $queryBuilder->getQuery(); |
||
94 | |||
95 | return $query->execute(); |
||
96 | }*/ |
||
97 | |||
98 | /*public function getUsers($group) |
||
99 | { |
||
100 | $queryBuilder = $this->createQueryBuilder('u'); |
||
101 | |||
102 | // Selecting course info. |
||
103 | $queryBuilder |
||
104 | ->select('u') |
||
105 | ->where('u.groups = :groupId') |
||
106 | ->setParameter('groupId', $group); |
||
107 | |||
108 | $query = $queryBuilder->getQuery(); |
||
109 | |||
110 | return $query->execute(); |
||
111 | }*/ |
||
112 | |||
113 | /** |
||
114 | * Get a filtered list of user by status and (optionally) access url |
||
115 | * @param string $query The query to filter |
||
116 | * @param int $status The status |
||
117 | * @param int $accessUrlId The access URL ID |
||
118 | * @return array |
||
119 | */ |
||
120 | View Code Duplication | public function searchUsersByStatus($query, $status, $accessUrlId = null) |
|
147 | |||
148 | /** |
||
149 | * Get the coaches for a course within a session |
||
150 | * @param Session $session The session |
||
151 | * @param Course $course The course |
||
152 | * @return \Doctrine\ORM\QueryBuilder |
||
153 | */ |
||
154 | View Code Duplication | public function getCoachesForSessionCourse(Session $session, Course $course) |
|
175 | |||
176 | /** |
||
177 | * Get the sessions admins for a user |
||
178 | * @param \Chamilo\UserBundle\Entity\User $user The user |
||
179 | * @return array |
||
180 | */ |
||
181 | View Code Duplication | public function getSessionAdmins($user) |
|
207 | |||
208 | /** |
||
209 | * Get the student bosses for a user |
||
210 | * @param User $user The user |
||
211 | * @return array |
||
212 | */ |
||
213 | View Code Duplication | public function getStudentBosses($user) |
|
233 | } |
||
234 |