| Conditions | 12 | 
| Paths | 432 | 
| Total Lines | 110 | 
| Code Lines | 62 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| 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 | ||
| 68 | public function handle(ServerRequestInterface $request): ResponseInterface | ||
| 69 |     { | ||
| 70 |         $parent_id = $request->getAttribute('parent_id'); | ||
| 71 | |||
| 72 |         if ($parent_id === null) { | ||
| 73 |             $parent = new PlaceLocation(''); | ||
| 74 |         } else { | ||
| 75 | $parent = $this->map_data_service->findById((int) $parent_id); | ||
| 76 | } | ||
| 77 | |||
| 78 |         for ($tmp = $parent, $hierarchy = []; $tmp->id() !== null; $tmp = $tmp->parent()) { | ||
| 79 | $hierarchy[] = $tmp->locationName(); | ||
| 80 | } | ||
| 81 | |||
| 82 | // Create the file name | ||
| 83 |         $filename = preg_replace('/[^\p{L}]+/u', '-', $hierarchy[0] ?? 'Global') . '.csv'; | ||
| 84 | |||
| 85 | // Recursively search for child places | ||
| 86 | $places = []; | ||
| 87 | $queue = [[ | ||
| 88 | $parent->id(), array_reverse($hierarchy), $parent->latitude(), $parent->longitude() | ||
| 89 | ]]; | ||
| 90 | |||
| 91 |         while ($queue !== []) { | ||
| 92 | [$id, $hierarchy, $latitude, $longitude] = array_shift($queue); | ||
| 93 | |||
| 94 |             if ($latitude !== null && $longitude !== null) { | ||
| 95 | $places[] = (object) [ | ||
| 96 | 'hierarchy' => $hierarchy, | ||
| 97 | 'latitude' => $latitude, | ||
| 98 | 'longitude' => $longitude, | ||
| 99 | ]; | ||
| 100 | } | ||
| 101 | |||
| 102 |             $query = DB::table('place_location'); | ||
| 103 | // Data for the next level. | ||
| 104 | |||
| 105 |             if ($id === null) { | ||
| 106 |                 $query->whereNull('parent_id'); | ||
| 107 |             } else { | ||
| 108 |                 $query->where('parent_id', '=', $id); | ||
| 109 | } | ||
| 110 | |||
| 111 | $rows = $query | ||
| 112 |                 ->orderBy('place', 'DESC') | ||
| 113 | ->select(['id', 'place', 'latitude', 'longitude']) | ||
| 114 | ->get(); | ||
| 115 | |||
| 116 | $next_level = count($hierarchy); | ||
| 117 | |||
| 118 |             foreach ($rows as $row) { | ||
| 119 | $hierarchy[$next_level] = $row->place; | ||
| 120 | array_unshift($queue, [$row->id, $hierarchy, $row->latitude, $row->longitude]); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | // Pad all locations to the length of the longest. | ||
| 125 | $max_level = 0; | ||
| 126 |         foreach ($places as $place) { | ||
| 127 | $max_level = max($max_level, count($place->hierarchy)); | ||
| 128 | } | ||
| 129 | |||
| 130 |         $places = array_map(function (object $place) use ($max_level): array { | ||
| 131 | return array_merge( | ||
| 132 | [ | ||
| 133 | count($place->hierarchy) - 1, | ||
| 134 | ], | ||
| 135 | array_pad($place->hierarchy, $max_level, ''), | ||
| 136 | [ | ||
| 137 | $this->map_data_service->writeLongitude((float) $place->longitude), | ||
| 138 | $this->map_data_service->writeLatitude((float) $place->latitude), | ||
| 139 | '', | ||
| 140 | '', | ||
| 141 | ] | ||
| 142 | ); | ||
| 143 | }, $places); | ||
| 144 | |||
| 145 | // Create the header line for the output file (always English) | ||
| 146 | $header = [ | ||
| 147 | 'Level', | ||
| 148 | ]; | ||
| 149 | |||
| 150 |         for ($i = 0; $i < $max_level; $i++) { | ||
| 151 | $header[] = 'Place' . $i; | ||
| 152 | } | ||
| 153 | |||
| 154 | $header[] = 'Longitude'; | ||
| 155 | $header[] = 'Latitude'; | ||
| 156 | $header[] = 'Zoom'; | ||
| 157 | $header[] = 'Icon'; | ||
| 158 | |||
| 159 |         $resource = fopen('php://memory', 'wb+'); | ||
| 160 | |||
| 161 |         if ($resource === false) { | ||
| 162 |             throw new RuntimeException('Failed to create temporary stream'); | ||
| 163 | } | ||
| 164 | |||
| 165 | fputcsv($resource, $header, MapDataService::CSV_SEPARATOR, '"', '\\'); | ||
| 166 | |||
| 167 |         foreach ($places as $place) { | ||
| 168 | fputcsv($resource, $place, MapDataService::CSV_SEPARATOR, '"', '\\'); | ||
| 169 | } | ||
| 170 | |||
| 171 | rewind($resource); | ||
| 172 | |||
| 173 | $filename = addcslashes($filename, '"'); | ||
| 174 | |||
| 175 | return response(stream_get_contents($resource)) | ||
| 176 |             ->withHeader('content-type', 'text/csv; charset=UTF-8') | ||
| 177 |             ->withHeader('content-disposition', 'attachment; filename="' . $filename . '"'); | ||
| 178 | } | ||
| 180 |