| Conditions | 7 |
| Paths | 12 |
| Total Lines | 55 |
| Code Lines | 37 |
| 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 |
||
| 41 | public function handle() |
||
| 42 | { |
||
| 43 | $this->info(sprintf( |
||
| 44 | '-[ %s : Send reminders start ]------------------------------------', |
||
| 45 | strftime('%Y-%m-%d %H:%M:%S') |
||
| 46 | )); |
||
| 47 | |||
| 48 | $n = 0; |
||
| 49 | foreach (Loan::with( |
||
| 50 | 'item', |
||
| 51 | 'item.thing', |
||
| 52 | 'library', |
||
| 53 | 'user', |
||
| 54 | 'notifications' |
||
| 55 | )->get() as $loan) { |
||
| 56 | $settings = $loan->getLibrarySettings(); |
||
| 57 | |||
| 58 | if (!$settings->reminders) { |
||
| 59 | $this->comment("[{$loan->id}] Not sending reminders for {$loan->item->thing->name()}" . |
||
| 60 | " from {$loan->library->name}."); |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($loan->due_at->getTimestamp() > Carbon::now()->getTimestamp()) { |
||
| 65 | $this->comment("[{$loan->id}] Loan is not due yet."); |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (count($loan->notifications) > 0) { |
||
| 70 | $this->comment("[{$loan->id}] Reminder already sent"); |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | if (empty($loan->user->email)) { |
||
| 75 | $this->error("[{$loan->id}] Cannot send reminder. No email set for user {$loan->user->id}"); |
||
| 76 | \Log::error(sprintf( |
||
| 77 | 'Kan ikke sende påminnelse til <a href="%s">%s</a> – mangler epostadresse.', |
||
| 78 | action('UsersController@getShow', $loan->user->id), |
||
| 79 | "bruker #{$loan->user->id}" |
||
| 80 | )); |
||
| 81 | continue; |
||
| 82 | } |
||
| 83 | |||
| 84 | // Send reminder |
||
| 85 | $this->info("[{$loan->id}] Sending reminder to {$loan->user->email}"); |
||
| 86 | $loan->user->notify(new FirstReminder($loan)); |
||
| 87 | $n++; |
||
| 88 | } |
||
| 89 | if ($n > 0) { |
||
| 90 | \Log::info("$n påminnelse(r) ble sendt ut."); |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->info(sprintf( |
||
| 94 | '-[ %s : Send reminders complete ]------------------------------------', |
||
| 95 | strftime('%Y-%m-%d %H:%M:%S') |
||
| 96 | )); |
||
| 99 |
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: