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 |
||
| 30 | class ParteRepository extends EntityRepository |
||
| 31 | { |
||
| 32 | public function findAllOrdered() |
||
| 33 | { |
||
| 34 | return $this->getEntityManager() |
||
| 35 | ->getRepository('AppBundle:Parte') |
||
| 36 | ->createQueryBuilder('p') |
||
| 37 | ->orderBy('p.fechaSuceso', 'DESC') |
||
| 38 | ->getQuery() |
||
| 39 | ->getResult(); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function findNotificados() |
||
| 43 | { |
||
| 44 | return $this->getEntityManager() |
||
| 45 | ->getRepository('AppBundle:Parte') |
||
| 46 | ->createQueryBuilder('p') |
||
| 47 | ->innerJoin('p.alumno', 'a') |
||
| 48 | ->where('p.fechaAviso IS NOT NULL'); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function findNoNotificados() |
||
| 52 | { |
||
| 53 | return $this->getEntityManager() |
||
| 54 | ->getRepository('AppBundle:Parte') |
||
| 55 | ->createQueryBuilder('p') |
||
| 56 | ->innerJoin('p.alumno', 'a') |
||
| 57 | ->where('p.fechaAviso IS NULL'); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function findAllNoNotificadosPorAlumnoYUsuario($alumno, $usuario) |
||
| 61 | { |
||
| 62 | return $this->findNoNotificadosPorUsuarioOTutoria($usuario) |
||
| 63 | ->andWhere('p.alumno = :alumno') |
||
| 64 | ->setParameter('usuario', $usuario) |
||
| 65 | ->setParameter('alumno', $alumno) |
||
| 66 | ->orderBy('p.fechaSuceso', 'DESC') |
||
| 67 | ->getQuery() |
||
| 68 | ->getResult(); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function findAllNoNotificadosPorAlumno($alumno) |
||
| 72 | { |
||
| 73 | return $this->findNoNotificados() |
||
| 74 | ->andWhere('p.alumno = :alumno') |
||
| 75 | ->setParameter('alumno', $alumno) |
||
| 76 | ->orderBy('p.fechaSuceso', 'DESC') |
||
| 77 | ->getQuery() |
||
| 78 | ->getResult(); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function findPorUsuarioOTutoria($usuario) |
||
| 82 | { |
||
| 83 | $orX = $this->getEntityManager()->createQueryBuilder() |
||
| 84 | ->expr()->orX() |
||
| 85 | ->add('p.usuario = :usuario') |
||
| 86 | ->add('a.grupo = :grupo'); |
||
| 87 | |||
| 88 | return $this->getEntityManager() |
||
| 89 | ->getRepository('AppBundle:Parte') |
||
| 90 | ->createQueryBuilder('p') |
||
| 91 | ->select('p') |
||
| 92 | ->innerJoin('p.alumno', 'a') |
||
| 93 | ->innerJoin('AppBundle:Grupo', 'g', 'WITH', 'a.grupo = g') |
||
| 94 | ->andWhere($orX) |
||
| 95 | ->setParameter('usuario', $usuario) |
||
| 96 | ->setParameter('grupo', $usuario->getTutoria()); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function findPorUsuario($usuario) |
||
| 100 | { |
||
| 101 | return $this->getEntityManager() |
||
| 102 | ->getRepository('AppBundle:Parte') |
||
| 103 | ->createQueryBuilder('p') |
||
| 104 | ->where('p.usuario = :usuario') |
||
| 105 | ->setParameter('usuario', $usuario); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function findNoNotificadosPorUsuario($usuario) |
||
| 109 | { |
||
| 110 | return $this->findPorUsuario($usuario) |
||
| 111 | ->andWhere('p.fechaAviso IS NULL'); |
||
| 112 | } |
||
| 113 | |||
| 114 | View Code Duplication | public function findNoNotificadosPorUsuarioOTutoria($usuario) |
|
| 115 | { |
||
| 116 | $orX = $this->getEntityManager()->createQueryBuilder() |
||
| 117 | ->expr()->orX() |
||
| 118 | ->add('p.usuario = :usuario') |
||
| 119 | ->add('a.grupo = :grupo'); |
||
| 120 | |||
| 121 | return $this->findNoNotificados() |
||
| 122 | ->innerJoin('AppBundle:Grupo', 'g', 'WITH', 'a.grupo = g') |
||
| 123 | ->andWhere($orX) |
||
| 124 | ->setParameter('usuario', $usuario) |
||
| 125 | ->setParameter('grupo', $usuario->getTutoria()); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function findAllNoNotificadosPorUsuario($usuario) |
||
| 129 | { |
||
| 130 | return $this->findNoNotificadosPorUsuario($usuario) |
||
| 131 | ->orderBy('p.fechaSuceso', 'DESC') |
||
| 132 | ->getQuery() |
||
| 133 | ->getResult(); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function findAllPorUsuarioOTutoria($usuario) |
||
| 137 | { |
||
| 138 | return $this->findPorUsuarioOTutoria($usuario) |
||
| 139 | ->orderBy('p.fechaSuceso', 'DESC') |
||
| 140 | ->getQuery() |
||
| 141 | ->getResult(); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function countAll() |
||
| 153 | |||
| 154 | public function countNoNotificados() |
||
| 155 | { |
||
| 156 | return $this->findNoNotificados() |
||
| 157 | ->select('COUNT(p.id)') |
||
| 158 | ->getQuery() |
||
| 159 | ->getSingleScalarResult(); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function countNoNotificadosPorUsuario($usuario) |
||
| 163 | { |
||
| 164 | return $this->findNoNotificadosPorUsuario($usuario) |
||
| 165 | ->select('COUNT(p.id)') |
||
| 166 | ->getQuery() |
||
| 167 | ->getSingleScalarResult(); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function countNoNotificadosPorUsuarioOTutoria($usuario) |
||
| 171 | { |
||
| 172 | return $this->findNoNotificadosPorUsuarioOTutoria($usuario) |
||
| 173 | ->select('COUNT(p.id)') |
||
| 174 | ->getQuery() |
||
| 175 | ->getSingleScalarResult(); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function countPorUsuarioOTutoria($usuario) |
||
| 179 | { |
||
| 180 | return $this->findPorUsuarioOTutoria($usuario) |
||
| 181 | ->select('COUNT(p.id)') |
||
| 182 | ->getQuery() |
||
| 183 | ->getSingleScalarResult(); |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | public function countPorUsuario($usuario) |
||
| 188 | { |
||
| 189 | return $this->findPorUsuario($usuario) |
||
| 190 | ->select('COUNT(p.id)') |
||
| 191 | ->getQuery() |
||
| 192 | ->getSingleScalarResult(); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function countSancionables() |
||
| 196 | { |
||
| 197 | return $this->findNotificados() |
||
| 198 | ->select('COUNT(p.id)') |
||
| 199 | ->andWhere('p.prescrito = false') |
||
| 200 | ->andWhere('p.sancion IS NULL') |
||
| 201 | ->getQuery() |
||
| 202 | ->getSingleScalarResult(); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function countSancionablesPrioritarios() |
||
| 206 | { |
||
| 207 | return $this->findNotificados() |
||
| 208 | ->select('COUNT(p.id)') |
||
| 209 | ->andWhere('p.prescrito = false') |
||
| 210 | ->andWhere('p.sancion IS NULL') |
||
| 211 | ->andWhere('p.prioritario = true') |
||
| 212 | ->getQuery() |
||
| 213 | ->getSingleScalarResult(); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function findAllSancionablesPorAlumno($alumno) |
||
| 231 | |||
| 232 | public function findPrescritos($plazo, $filtrarRecordados = false) |
||
| 233 | { |
||
| 234 | $fecha = new \DateTime(); |
||
| 235 | $fecha->sub(new \DateInterval('P' . $plazo . 'D')); |
||
| 236 | |||
| 237 | $data = $this->getEntityManager() |
||
| 238 | ->getRepository('AppBundle:Parte') |
||
| 239 | ->createQueryBuilder('p') |
||
| 240 | ->where('p.sancion IS NULL') |
||
| 241 | ->andWhere('p.prescrito = false') |
||
| 242 | ->andWhere('p.fechaSuceso < :fechaLimite'); |
||
| 243 | |||
| 244 | if ($filtrarRecordados) { |
||
| 245 | $data = $data->andWhere('p.fechaRecordatorio IS NULL'); |
||
| 246 | } |
||
| 247 | |||
| 248 | return $data |
||
| 249 | ->setParameter('fechaLimite', $fecha) |
||
| 253 | } |
||
| 254 |