We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 14 |
Paths | 1320 |
Total Lines | 120 |
Code Lines | 72 |
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 declare(strict_types=1); |
||
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 | } |
||
69 | |||
70 | $allowed_types = [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG]; |
||
71 | if (!in_array($size[2], $allowed_types)) { |
||
72 | create_error('Only gif, jpg or png-image allowed!'); |
||
73 | } |
||
74 | |||
75 | // check if width > 500 |
||
76 | if ($size[0] > 500) { |
||
77 | create_error('Image is wider than 500 pixels!'); |
||
78 | } |
||
79 | |||
80 | // check if height > 500 |
||
81 | if ($size[1] > 500) { |
||
82 | create_error('Image is higher than 500 pixels!'); |
||
83 | } |
||
84 | |||
85 | if (!move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD . $account->getAccountID())) { |
||
86 | create_error('Failed to upload image!'); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // check if we had a album entry so far |
||
91 | $db = Database::getInstance(); |
||
92 | $dbResult = $db->read('SELECT 1 FROM album WHERE account_id = ' . $db->escapeNumber($account->getAccountID())); |
||
93 | if ($dbResult->hasRecord()) { |
||
94 | if (!$noPicture) { |
||
95 | $comment = '<span class="green">*** Picture changed</span>'; |
||
96 | } |
||
97 | |||
98 | // change album entry |
||
99 | $db->write('UPDATE album |
||
100 | SET location = ' . $db->escapeString($location) . ', |
||
101 | email = ' . $db->escapeString($email) . ', |
||
102 | website= ' . $db->escapeString($website) . ', |
||
103 | day = ' . $db->escapeNumber($day) . ', |
||
104 | month = ' . $db->escapeNumber($month) . ', |
||
105 | year = ' . $db->escapeNumber($year) . ', |
||
106 | other = ' . $db->escapeString($other) . ', |
||
107 | last_changed = ' . $db->escapeNumber(Epoch::time()) . ', |
||
108 | approved = \'TBC\', |
||
109 | disabled = \'FALSE\' |
||
110 | WHERE account_id = ' . $db->escapeNumber($account->getAccountID())); |
||
111 | } else { |
||
112 | // if he didn't upload a picture before |
||
113 | // we kick him out here |
||
114 | if ($noPicture) { |
||
115 | create_error('What is it worth if you don\'t upload an image?'); |
||
116 | } |
||
117 | |||
118 | $comment = '<span class="green">*** Picture added</span>'; |
||
119 | |||
120 | // add album entry |
||
121 | $db->insert('album', [ |
||
122 | 'account_id' => $db->escapeNumber($account->getAccountID()), |
||
123 | 'location' => $db->escapeString($location), |
||
124 | 'email' => $db->escapeString($email), |
||
125 | 'website' => $db->escapeString($website), |
||
126 | 'day' => $db->escapeNumber($day), |
||
127 | 'month' => $db->escapeNumber($month), |
||
128 | 'year' => $db->escapeNumber($year), |
||
129 | 'other' => $db->escapeString($other), |
||
130 | 'created' => $db->escapeNumber(Epoch::time()), |
||
131 | 'last_changed' => $db->escapeNumber(Epoch::time()), |
||
132 | 'approved' => $db->escapeString('TBC'), |
||
133 | ]); |
||
134 | } |
||
135 | |||
136 | if (!empty($comment)) { |
||
137 | // check if we have comments for this album already |
||
138 | $db->lockTable('album_has_comments'); |
||
139 | |||
140 | $dbResult = $db->read('SELECT IFNULL(MAX(comment_id)+1, 0) AS next_comment_id FROM album_has_comments WHERE album_id = ' . $db->escapeNumber($account->getAccountID())); |
||
141 | $comment_id = $dbResult->record()->getInt('next_comment_id'); |
||
142 | |||
143 | $db->insert('album_has_comments', [ |
||
144 | 'album_id' => $db->escapeNumber($account->getAccountID()), |
||
145 | 'comment_id' => $db->escapeNumber($comment_id), |
||
146 | 'time' => $db->escapeNumber(Epoch::time()), |
||
147 | 'post_id' => 0, |
||
148 | 'msg' => $db->escapeString($comment), |
||
149 | ]); |
||
150 | $db->unlock(); |
||
151 | } |
||
152 | |||
153 | $successMsg = 'SUCCESS: Your information has been updated!'; |
||
154 | $container = new AlbumEdit($successMsg); |
||
155 | $container->go(); |
||
156 | } |
||
159 |