| Conditions | 11 |
| Paths | 100 |
| Total Lines | 57 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 50 | public function createProblemReport(): void |
||
| 51 | { |
||
| 52 | // I don't care about concurrency; there's only me using this and |
||
| 53 | // I can always run it again if there's a problem; it's an unimportant |
||
| 54 | // operation. |
||
| 55 | $this->problemRepository->clearAllProblems(); |
||
| 56 | |||
| 57 | // Build a hash of all possible valid URLs for wanders and |
||
| 58 | // images to compare what we find in descriptions, etc. with. |
||
| 59 | $validUris = []; |
||
| 60 | $wanders = $this->wanderRepository->findAll(); |
||
| 61 | foreach ($wanders as $wander) { |
||
| 62 | $uri = $this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL); |
||
| 63 | $validUris[$uri] = true; // Really I just want a hash that doesn't actually map |
||
| 64 | } |
||
| 65 | |||
| 66 | $images = $this->imageRepository->findAll(); |
||
| 67 | foreach ($images as $image) { |
||
| 68 | $uri = $this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL); |
||
| 69 | $validUris[$uri] = true; // Really I just want a hash that doesn't actually map |
||
| 70 | } |
||
| 71 | //dd($validUris); |
||
| 72 | |||
| 73 | $homepage = $this->router->generate('home', [], RouterInterface::ABSOLUTE_URL); |
||
| 74 | |||
| 75 | // Okay, now we've got a list of all valid URIs, let's have a look through all our descriptions |
||
| 76 | |||
| 77 | // Wanders first... |
||
| 78 | foreach($wanders as $wander) { |
||
| 79 | $uris = $this->markdownService->findLinks($wander->getDescription()); |
||
| 80 | foreach ($uris as $uri) { |
||
| 81 | if (substr($uri, 0, strlen($homepage)) == $homepage) { |
||
| 82 | if (!array_key_exists($uri, $validUris)) { |
||
| 83 | $problem = new Problem(); |
||
| 84 | $problem->setDescription('Wander ' . $wander->getId() . ' links to invalid URI: ' . $uri); |
||
| 85 | $problem->setUri($this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
||
| 86 | $this->entityManager->persist($problem); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | $this->entityManager->flush(); |
||
| 92 | // ...then Images: |
||
| 93 | foreach($images as $image) { |
||
| 94 | $uris = $this->markdownService->findLinks($image->getDescription()); |
||
| 95 | foreach ($uris as $uri) { |
||
| 96 | if (substr($uri, 0, strlen($homepage)) == $homepage) { |
||
| 97 | if (!array_key_exists($uri, $validUris)) { |
||
| 98 | $problem = new Problem(); |
||
| 99 | $problem->setDescription('Image ' . $image->getId() . ' links to invalid URI: ' . $uri); |
||
| 100 | $problem->setUri($this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
||
| 101 | $this->entityManager->persist($problem); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | $this->entityManager->flush(); |
||
| 107 | } |
||
| 108 | } |