| Conditions | 1 |
| Paths | 1 |
| Total Lines | 70 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 23 | public function index( |
||
| 24 | WanderRepository $wanderRepository, |
||
| 25 | ImageRepository $imageRepository, |
||
| 26 | ProblemRepository $problemRepository |
||
| 27 | ): Response |
||
| 28 | { |
||
| 29 | // Problems that might take time/resources to find, that are |
||
| 30 | // popped into a table on specific demand rather than every |
||
| 31 | // time we load this page: |
||
| 32 | $builtProblems = $problemRepository->findAll(); |
||
| 33 | |||
| 34 | // TODO: Might be sensible to do everything here into the problems |
||
| 35 | // table, depending on how much we add to this. |
||
| 36 | // Other issues, found on the fly: |
||
| 37 | $qb = $wanderRepository->createQueryBuilder('w'); |
||
| 38 | |||
| 39 | // TODO: This doesn't work, as e.g. keywords being an empty array |
||
| 40 | // |
||
| 41 | $problems = $qb |
||
| 42 | ->join('w.images', 'i') |
||
| 43 | ->leftJoin('w.featuredImage', 'fi') |
||
| 44 | ->select('w AS wander') |
||
| 45 | ->addSelect('COUNT(i) AS image_count') |
||
| 46 | ->addSelect('SUM(CASE WHEN i.title IS NULL THEN 1 ELSE 0 END) AS no_title') |
||
| 47 | ->addSelect('SUM(CASE WHEN i.latlng IS NULL THEN 1 ELSE 0 END) AS no_latlng') |
||
| 48 | ->addSelect('SUM(CASE WHEN i.location IS NULL THEN 1 ELSE 0 END) AS no_location') |
||
| 49 | ->addSelect('SUM(CASE WHEN i.rating IS NULL OR i.rating = 0 THEN 1 ELSE 0 END) AS no_rating') |
||
| 50 | // TODO: This is a hideous bodge and will break when we finally give in and move |
||
| 51 | // keywords and auto-tags to being related entities rather than a dirty PHP |
||
| 52 | // array, but it's good enough for a problem admin page for now. |
||
| 53 | //->addSelect("SUM(CASE WHEN i.keywords IS NULL OR i.keywords = 'a:0:{}' THEN 1 ELSE 0 END) AS no_keywords") |
||
| 54 | ->addSelect("SUM(CASE WHEN i.tags is empty THEN 1 ELSE 0 END) AS no_tags") |
||
| 55 | ->addSelect("SUM(CASE WHEN i.autoTags IS NULL OR i.autoTags = 'a:0:{}' THEN 1 ELSE 0 END) AS no_auto_tags") |
||
| 56 | ->addSelect("CASE WHEN fi.id IS NULL THEN 1 ELSE 0 END AS no_featured_image") |
||
| 57 | ->addSelect( |
||
| 58 | "(SUM(CASE WHEN i.title IS NULL THEN 1 ELSE 0 END)) + " . |
||
| 59 | "(SUM(CASE WHEN i.latlng IS NULL THEN 1 ELSE 0 END)) + " . |
||
| 60 | "(SUM(CASE WHEN i.location IS NULL THEN 1 ELSE 0 END)) + " . |
||
| 61 | "(SUM(CASE WHEN i.rating IS NULL OR i.rating = 0 THEN 1 ELSE 0 END)) + " . |
||
| 62 | "(SUM(CASE WHEN i.tags is empty THEN 1 ELSE 0 END)) AS total_problems_excl_auto" |
||
| 63 | ) |
||
| 64 | ->addSelect( |
||
| 65 | "(SUM(CASE WHEN i.title IS NULL THEN 1 ELSE 0 END)) + " . |
||
| 66 | "(10 * SUM(CASE WHEN i.latlng IS NULL THEN 1 ELSE 0 END)) + " . |
||
| 67 | "(2 * SUM(CASE WHEN i.location IS NULL THEN 1 ELSE 0 END)) + " . |
||
| 68 | "(5 * SUM(CASE WHEN i.rating IS NULL OR i.rating = 0 THEN 1 ELSE 0 END)) + " . |
||
| 69 | "(1 * CASE WHEN fi.id IS NULL THEN 1 ELSE 0 END) + " . |
||
| 70 | "(0.01 * SUM(CASE WHEN i.tags is empty THEN 1 ELSE 0 END)) + " . |
||
| 71 | "(0.001 * SUM(CASE WHEN i.autoTags IS NULL OR i.autoTags = 'a:0:{}' THEN 1 ELSE 0 END)) AS weighted_problem_score" |
||
| 72 | ) |
||
| 73 | ->addGroupBy('w') |
||
| 74 | ->addGroupBy('fi') |
||
| 75 | ->having('no_title > 0') |
||
| 76 | ->orHaving('no_latlng > 0') |
||
| 77 | ->orHaving('no_location > 0') |
||
| 78 | ->orHaving('no_rating > 0') |
||
| 79 | ->orHaving('no_tags > 0') |
||
| 80 | ->orHaving('no_auto_tags > 0') |
||
| 81 | ->orHaving('no_featured_image > 0') |
||
| 82 | ->orderBy('weighted_problem_score', 'desc') |
||
| 83 | ->addOrderBy('w.startTime', 'desc') |
||
| 84 | ->getQuery() |
||
| 85 | ->getResult(); |
||
| 86 | |||
| 87 | $orphans = $imageRepository->findWithNoWander(); |
||
| 88 | |||
| 89 | return $this->render('admin/problems/index.html.twig', [ |
||
| 90 | 'problems' => $problems, |
||
| 91 | 'orphans' => $orphans, |
||
| 92 | 'built_problems' => $builtProblems |
||
| 93 | ]); |
||
| 181 |