| Conditions | 11 |
| Paths | 12 |
| Total Lines | 86 |
| Code Lines | 48 |
| 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); |
||
| 130 | protected function transferLoans(User $localUser) |
||
| 131 | { |
||
| 132 | $almaUser = $this->alma->users[$localUser->alma_primary_id]; |
||
| 133 | |||
| 134 | if (is_null($almaUser)) { |
||
| 135 | \Log::error("Kunne ikke overføre lån fordi Alma-brukeren ikke ble funnet. Meget uventet."); |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | $n = 0; |
||
| 140 | |||
| 141 | foreach ($localUser->loans as $loan) { |
||
| 142 | if ($loan->item->thing_id == 1) { |
||
| 143 | // Loan should be transferred from the temporary card to the user |
||
| 144 | |||
| 145 | $barcode = $loan->item->barcode; |
||
| 146 | $library = $loan->library; |
||
| 147 | |||
| 148 | $errBecause = "Kunne ikke overføre lån av $barcode i Alma fordi"; |
||
| 149 | |||
| 150 | if (is_null($library->temporary_barcode)) { |
||
| 151 | \Log::error("$errBecause biblioteket ikke lenger har et midlertidig lånekort."); |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | |||
| 155 | if (is_null($library->library_code)) { |
||
| 156 | \Log::error("$errBecause biblioteket ikke lenger har en bibliotekskode."); |
||
| 157 | continue; |
||
| 158 | } |
||
| 159 | |||
| 160 | $tempUser = $this->alma->users[$library->temporary_barcode]; |
||
| 161 | $almaLibrary = $this->alma->libraries[$library->library_code]; |
||
| 162 | |||
| 163 | if (is_null($tempUser)) { |
||
| 164 | \Log::error("$errBecause brukeren '{$library->temporary_barcode}' ikke ble funnet i Alma."); |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | |||
| 168 | $almaItem = $this->alma->items->fromBarcode($barcode); |
||
| 169 | $almaLoan = $almaItem->loan; |
||
| 170 | |||
| 171 | if (is_null($almaLoan)) { |
||
| 172 | \Log::warning("$errBecause dokumentet i mellomtiden har blitt returnert i Alma."); |
||
| 173 | |||
| 174 | // Checkin local loan and delete temporary item |
||
| 175 | $loan->checkIn(); |
||
| 176 | |||
| 177 | continue; |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($almaLoan->user_id != $library->temporary_barcode) { |
||
| 181 | \Log::warning("$errBecause dokumentet ikke lenger er utlånt til {$library->temporary_barcode}."); |
||
| 182 | |||
| 183 | // Checkin local loan and delete temporary item |
||
| 184 | $loan->checkIn(); |
||
| 185 | |||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | if (count($almaItem->requests)) { |
||
| 190 | \Log::warning("$errBecause dokumentet har reserveringer."); |
||
| 191 | continue; |
||
| 192 | } |
||
| 193 | |||
| 194 | // Cross fingers |
||
| 195 | try { |
||
| 196 | $almaItem->scanIn($almaLibrary, 'DEFAULT_CIRC_DESK', [ |
||
| 197 | 'place_on_hold_shelf' => 'false', |
||
| 198 | 'auto_print_slip' => 'false', |
||
| 199 | ]); |
||
| 200 | $almaItem->checkOut($almaUser, $almaLibrary); |
||
| 201 | } catch (RequestFailed $e) { |
||
| 202 | \Log::warning($errBecause . ' ' . $e->getMessage()); |
||
| 203 | continue; |
||
| 204 | } |
||
| 205 | |||
| 206 | \Log::info(sprintf( |
||
| 207 | 'Overførte lån av <a href="%s">%s</a> til Alma-brukeren.', |
||
| 208 | action('ItemsController@show', $loan->item->id), |
||
| 209 | $barcode |
||
| 210 | )); |
||
| 211 | |||
| 212 | // Checkin local loan and delete temporary item |
||
| 213 | $loan->checkIn(); |
||
| 214 | |||
| 215 | $n++; |
||
| 216 | } |
||
| 220 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: