| Conditions | 5 |
| Paths | 5 |
| Total Lines | 57 |
| Code Lines | 33 |
| 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 |
||
| 53 | protected function processLoan(Loan $loan) |
||
| 54 | { |
||
| 55 | $barcode = $loan->item->barcode; |
||
| 56 | $library = $loan->library; |
||
| 57 | |||
| 58 | echo " - $barcode: "; |
||
| 59 | |||
| 60 | $errBecause = sprintf( |
||
| 61 | 'Kunne ikke sjekke lån av <a href="%s">%s</a> mot Alma fordi', |
||
| 62 | action('ItemsController@show', $loan->item_id), |
||
| 63 | $barcode |
||
| 64 | ); |
||
| 65 | |||
| 66 | $sucBecause = sprintf( |
||
| 67 | 'Fjerner lokalt lån av <a href="%s">%s</a> fordi', |
||
| 68 | action('ItemsController@show', $loan->item_id), |
||
| 69 | $barcode |
||
| 70 | ); |
||
| 71 | |||
| 72 | if (is_null($library->temporary_barcode)) { |
||
| 73 | $this->line('konfigfeil'); |
||
| 74 | \Log::error("$errBecause biblioteket ikke lenger har et midlertidig lånekort."); |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $tempUser = $this->alma->users[$library->temporary_barcode]; |
||
| 79 | |||
| 80 | if (is_null($tempUser)) { |
||
| 81 | $this->line('konfigfeil'); |
||
| 82 | \Log::error("$errBecause brukeren '{$library->temporary_barcode}' ikke ble funnet i Alma."); |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | $almaItem = $this->alma->items->fromBarcode($barcode); |
||
| 87 | $almaLoan = $almaItem->loan; |
||
| 88 | |||
| 89 | if (is_null($almaLoan)) { |
||
| 90 | $this->line('returnert i Alma'); |
||
| 91 | |||
| 92 | // Delete local loan and item |
||
| 93 | $loan->checkIn(); |
||
| 94 | |||
| 95 | \Log::info("$sucBecause dokumentet har blitt returnert i Alma."); |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($almaLoan->user_id != $library->temporary_barcode) { |
||
| 100 | $this->line('utlånt til annen person i Alma'); |
||
| 101 | |||
| 102 | // Delete local loan and item |
||
| 103 | $loan->checkIn(); |
||
| 104 | |||
| 105 | \Log::info("$sucBecause dokumentet har blitt utlånt til en annen person i Alma."); |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->line('fremdeles utlånt i Alma'); |
||
| 110 | } |
||
| 112 |
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: