| Conditions | 18 |
| Paths | 109 |
| Total Lines | 100 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 102 | public function edit(Request $request) |
||
| 103 | { |
||
| 104 | $movie = new Movie; |
||
| 105 | $title = 'Movie Edit'; |
||
| 106 | |||
| 107 | // Check if ID is provided |
||
| 108 | if (! $request->has('id')) { |
||
| 109 | return redirect()->to('admin/movie-list') |
||
| 110 | ->with('error', 'No movie ID provided.'); |
||
| 111 | } |
||
| 112 | |||
| 113 | $id = $request->input('id'); |
||
| 114 | $mov = $movie->getMovieInfo($id); |
||
| 115 | |||
| 116 | if ($mov === null) { |
||
| 117 | return redirect()->to('admin/movie-list') |
||
| 118 | ->with('error', 'Movie with IMDB ID '.$id.' does not exist in database.'); |
||
| 119 | } |
||
| 120 | |||
| 121 | // Handle update from TMDB |
||
| 122 | if ($request->has('update') && (int) $request->input('update') === 1) { |
||
| 123 | try { |
||
| 124 | if (! \defined('STDOUT')) { |
||
| 125 | \define('STDOUT', fopen('php://stdout', 'wb')); |
||
| 126 | } |
||
| 127 | |||
| 128 | $movieInfo = $movie->updateMovieInfo($id); |
||
| 129 | |||
| 130 | if ($movieInfo) { |
||
| 131 | return redirect()->back() |
||
| 132 | ->with('success', 'Movie information updated successfully from TMDB!'); |
||
| 133 | } else { |
||
| 134 | return redirect()->back() |
||
| 135 | ->with('error', 'Failed to update movie information from TMDB. Please try again.'); |
||
| 136 | } |
||
| 137 | } catch (\Exception $e) { |
||
| 138 | return redirect()->back() |
||
| 139 | ->with('error', 'Error updating movie: '.$e->getMessage()); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // Handle form submission |
||
| 144 | $action = $request->input('action') ?? 'view'; |
||
| 145 | |||
| 146 | if ($action === 'submit') { |
||
| 147 | try { |
||
| 148 | $coverLoc = public_path('covers/movies/'.$id.'-cover.jpg'); |
||
| 149 | $backdropLoc = public_path('covers/movies/'.$id.'-backdrop.jpg'); |
||
| 150 | |||
| 151 | // Ensure directory exists |
||
| 152 | if (! file_exists(public_path('covers/movies'))) { |
||
| 153 | mkdir(public_path('covers/movies'), 0755, true); |
||
| 154 | } |
||
| 155 | |||
| 156 | // Handle cover upload |
||
| 157 | if ($request->hasFile('cover') && $request->file('cover')->isValid()) { |
||
| 158 | $coverFile = $request->file('cover'); |
||
| 159 | $coverFile->move(public_path('covers/movies'), $id.'-cover.jpg'); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Handle backdrop upload |
||
| 163 | if ($request->hasFile('backdrop') && $request->file('backdrop')->isValid()) { |
||
| 164 | $backdropFile = $request->file('backdrop'); |
||
| 165 | $backdropFile->move(public_path('covers/movies'), $id.'-backdrop.jpg'); |
||
| 166 | } |
||
| 167 | |||
| 168 | $request->merge(['cover' => file_exists($coverLoc) ? 1 : 0]); |
||
| 169 | $request->merge(['backdrop' => file_exists($backdropLoc) ? 1 : 0]); |
||
| 170 | |||
| 171 | $movie->update([ |
||
| 172 | 'actors' => $request->input('actors'), |
||
| 173 | 'backdrop' => $request->input('backdrop'), |
||
| 174 | 'cover' => $request->input('cover'), |
||
| 175 | 'director' => $request->input('director'), |
||
| 176 | 'genre' => $request->input('genre'), |
||
| 177 | 'imdbid' => $id, |
||
| 178 | 'language' => $request->input('language'), |
||
| 179 | 'plot' => $request->input('plot'), |
||
| 180 | 'rating' => $request->input('rating'), |
||
| 181 | 'tagline' => $request->input('tagline'), |
||
| 182 | 'title' => $request->input('title'), |
||
| 183 | 'year' => $request->input('year'), |
||
| 184 | ]); |
||
| 185 | |||
| 186 | // Link releases to this movie |
||
| 187 | $movieInfo = MovieInfo::query()->where('imdbid', $id)->first(['id']); |
||
| 188 | if ($movieInfo !== null) { |
||
| 189 | Release::query()->where('imdbid', $id)->update(['movieinfo_id' => $movieInfo->id]); |
||
| 190 | } |
||
| 191 | |||
| 192 | return redirect()->to('admin/movie-list') |
||
| 193 | ->with('success', 'Movie updated successfully!'); |
||
| 194 | } catch (\Exception $e) { |
||
| 195 | return redirect()->back() |
||
| 196 | ->with('error', 'Error saving movie: '.$e->getMessage()) |
||
| 197 | ->withInput(); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | return view('admin.movies.edit', compact('title', 'mov'))->with('movie', $mov); |
||
| 202 | } |
||
| 204 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.