| Conditions | 17 |
| Paths | 676 |
| Total Lines | 85 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 |
||
| 57 | public function createProblemReport(): void |
||
| 58 | { |
||
| 59 | // I don't care about concurrency; there's only me using this and |
||
| 60 | // I can always run it again if there's a problem; it's an unimportant |
||
| 61 | // operation. |
||
| 62 | $this->problemRepository->clearAllProblems(); |
||
| 63 | |||
| 64 | // Build a hash of all possible valid URLs for wanders and |
||
| 65 | // images to compare what we find in descriptions, etc. with. |
||
| 66 | $validUris = []; |
||
| 67 | $wanders = $this->wanderRepository->findAll(); |
||
| 68 | foreach ($wanders as $wander) { |
||
| 69 | $uri = $this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL); |
||
| 70 | $validUris[$uri] = true; // Really I just want a hash that doesn't actually map |
||
| 71 | } |
||
| 72 | |||
| 73 | $images = $this->imageRepository->findAll(); |
||
| 74 | foreach ($images as $image) { |
||
| 75 | $uri = $this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL); |
||
| 76 | $validUris[$uri] = true; // Really I just want a hash that doesn't actually map |
||
| 77 | } |
||
| 78 | $homepage = $this->router->generate('home', [], RouterInterface::ABSOLUTE_URL); |
||
| 79 | |||
| 80 | // Okay, now we've got a list of all valid URIs, let's have a look through all our descriptions |
||
| 81 | |||
| 82 | // Wanders first... |
||
| 83 | foreach($wanders as $wander) { |
||
| 84 | $description = $wander->getDescription(); |
||
| 85 | |||
| 86 | // Broken links |
||
| 87 | $links = $this->markdownService->findLinks($description); |
||
| 88 | foreach ($links as $link) { |
||
| 89 | if (substr($link['uri'], 0, strlen($homepage)) == $homepage) { |
||
| 90 | if (!array_key_exists($link['uri'], $validUris)) { |
||
| 91 | $problem = new Problem(); |
||
| 92 | $problem->setDescription('Wander ' . $wander->getId() . ' links to invalid URI: ' . $link['uri'] . ' (text is: "' . $link['text'] . '")'); |
||
| 93 | $problem->setUri($this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
||
| 94 | $this->entityManager->persist($problem); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // Spell check recent wanders |
||
| 100 | $date = $wander->getStartTime(); |
||
| 101 | if ($date !== null && Carbon::now()->subWeeks(2)->lessThan($date)) { |
||
| 102 | $misspelledWords = $this->spellingService->checkString($this->markdownService->markdownToText($description)); |
||
| 103 | if (count($misspelledWords) > 0) { |
||
| 104 | $problem = new Problem(); |
||
| 105 | $problem->setDescription('Wander ' . $wander->getId() . ' has potential spelling mistakes: ' . implode(", ", $misspelledWords)); |
||
| 106 | $problem->setUri($this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
||
| 107 | $this->entityManager->persist($problem); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | $this->entityManager->flush(); |
||
| 112 | // ...then Images: |
||
| 113 | foreach($images as $image) { |
||
| 114 | $description = $image->getDescription(); |
||
| 115 | |||
| 116 | // Broken links |
||
| 117 | $links = $this->markdownService->findLinks($image->getDescription()); |
||
| 118 | foreach ($links as $link) { |
||
| 119 | if (substr($link['uri'], 0, strlen($homepage)) == $homepage) { |
||
| 120 | if (!array_key_exists($link['uri'], $validUris)) { |
||
| 121 | $problem = new Problem(); |
||
| 122 | $problem->setDescription('Image ' . $image->getId() . ' links to invalid URI: ' . $link['uri'] . ' (text is: "' . $link['text'] . '")'); |
||
| 123 | $problem->setUri($this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
||
| 124 | $this->entityManager->persist($problem); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // Spell check |
||
| 130 | $date = $image->getCapturedAt(); |
||
| 131 | if ($date !== null && Carbon::now()->subWeeks(2)->lessThan($date)) { |
||
| 132 | $misspelledWords = $this->spellingService->checkString($this->markdownService->markdownToText($description)); |
||
| 133 | if (count($misspelledWords) > 0) { |
||
| 134 | $problem = new Problem(); |
||
| 135 | $problem->setDescription('Image ' . $image->getId() . ' has potential spelling mistakes: ' . implode(", ", $misspelledWords)); |
||
| 136 | $problem->setUri($this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
||
| 137 | $this->entityManager->persist($problem); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $this->entityManager->flush(); |
||
| 142 | } |
||
| 143 | } |