We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 8 |
| Paths | 34 |
| Total Lines | 57 |
| Code Lines | 30 |
| 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 declare(strict_types=1); |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Determine whether a URL is reachable based on HTTP status code class. |
||
| 14 | */ |
||
| 15 | function isUrlReachable(string $url): bool { |
||
| 16 | $ch = curl_init($url); |
||
| 17 | if ($ch === false) { |
||
| 18 | throw new Exception('Failed to initialize curl'); |
||
| 19 | } |
||
| 20 | curl_setopt_array($ch, [ |
||
| 21 | CURLOPT_HEADER => true, |
||
| 22 | CURLOPT_NOBODY => true, // headers only |
||
| 23 | CURLOPT_RETURNTRANSFER => true, // don't print output |
||
| 24 | CURLOPT_TIMEOUT => 5, // in seconds |
||
| 25 | ]); |
||
| 26 | curl_exec($ch); |
||
| 27 | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 28 | curl_close($ch); |
||
| 29 | |||
| 30 | $statusClass = floor($statusCode / 100); |
||
| 31 | return $statusClass == 2 || $statusClass == 3; |
||
| 32 | } |
||
| 33 | |||
| 34 | class AlbumEditProcessor extends AccountPageProcessor { |
||
| 35 | |||
| 36 | public function build(SmrAccount $account): never { |
||
| 37 | $location = Request::get('location'); |
||
| 38 | $email = Request::get('email'); |
||
| 39 | |||
| 40 | // get website (and validate it) |
||
| 41 | $website = Request::get('website'); |
||
| 42 | if ($website != '') { |
||
| 43 | // add http:// if missing |
||
| 44 | if (!preg_match('=://=', $website)) { |
||
| 45 | $website = 'http://' . $website; |
||
| 46 | } |
||
| 47 | |||
| 48 | // validate |
||
| 49 | if (!isUrlReachable($website)) { |
||
| 50 | create_error('The website you entered is invalid!'); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | $other = Request::get('other'); |
||
| 55 | |||
| 56 | $day = Request::getInt('day'); |
||
| 57 | $month = Request::getInt('month'); |
||
| 58 | $year = Request::getInt('year'); |
||
| 59 | |||
| 60 | // check if we have an image |
||
| 61 | $noPicture = true; |
||
| 62 | if ($_FILES['photo']['error'] == UPLOAD_ERR_OK) { |
||
| 63 | $noPicture = false; |
||
| 64 | // get dimensions |
||
| 65 | $size = getimagesize($_FILES['photo']['tmp_name']); |
||
| 66 | if ($size === false) { |
||
| 67 | create_error('Uploaded file must be an image!'); |
||
| 68 | } |
||
| 159 |