Conditions | 8 |
Paths | 36 |
Total Lines | 74 |
Code Lines | 43 |
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 |
||
61 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
62 | { |
||
63 | $parent_id = $request->getAttribute('parent_id'); |
||
64 | |||
65 | if ($parent_id === null) { |
||
66 | $parent = new PlaceLocation(''); |
||
67 | } else { |
||
68 | $parent = $this->map_data_service->findById((int) $parent_id); |
||
69 | } |
||
70 | |||
71 | for ($tmp = $parent, $hierarchy = []; $tmp->id() !== null; $tmp = $tmp->parent()) { |
||
72 | $hierarchy[] = $tmp->locationName(); |
||
73 | } |
||
74 | |||
75 | // Create the file name |
||
76 | $filename = preg_replace('/[^\p{L}]+/u', '-', $hierarchy[0] ?? 'Global') . '.geojson'; |
||
77 | |||
78 | // Recursively search for child places |
||
79 | $features = []; |
||
80 | $queue = [[ |
||
81 | $parent->id(), array_reverse($hierarchy), $parent->latitude(), $parent->longitude() |
||
82 | ]]; |
||
83 | |||
84 | while ($queue !== []) { |
||
85 | [$id, $hierarchy, $latitude, $longitude] = array_shift($queue); |
||
86 | |||
87 | if ($latitude !== null && !$longitude !== null) { |
||
88 | $features[] = [ |
||
89 | 'type' => 'Feature', |
||
90 | 'geometry' => [ |
||
91 | 'type' => 'Point', |
||
92 | 'coordinates' => [ |
||
93 | $longitude, |
||
94 | $latitude, |
||
95 | ], |
||
96 | ], |
||
97 | 'properties' => [ |
||
98 | 'name' => implode(Gedcom::PLACE_SEPARATOR, array_reverse($hierarchy)), |
||
99 | ], |
||
100 | ]; |
||
101 | } |
||
102 | |||
103 | $query = DB::table('place_location'); |
||
104 | // Data for the next level. |
||
105 | |||
106 | if ($id === null) { |
||
107 | $query->whereNull('parent_id'); |
||
108 | } else { |
||
109 | $query->where('parent_id', '=', $id); |
||
110 | } |
||
111 | |||
112 | $rows = $query |
||
113 | ->orderBy('place', 'DESC') |
||
114 | ->select(['id', 'place', 'latitude', 'longitude']) |
||
115 | ->get(); |
||
116 | |||
117 | $next_level = count($hierarchy); |
||
118 | |||
119 | foreach ($rows as $row) { |
||
120 | $hierarchy[$next_level] = $row->place; |
||
121 | array_unshift($queue, [$row->id, $hierarchy, $row->latitude, $row->longitude]); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | $geojson = [ |
||
126 | 'type' => 'FeatureCollection', |
||
127 | 'features' => $features, |
||
128 | ]; |
||
129 | |||
130 | $filename = addcslashes($filename, '"'); |
||
131 | |||
132 | return response($geojson) |
||
133 | ->withHeader('Content-Type', 'application/vnd.geo+json') |
||
134 | ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
||
135 | } |
||
137 |