| Conditions | 16 |
| Paths | 360 |
| Total Lines | 68 |
| Code Lines | 35 |
| 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 |
||
| 92 | public function toString() { |
||
| 93 | $text = ''; |
||
| 94 | |||
| 95 | // Min/max stay length constraint variables. |
||
| 96 | $minimum_stay = empty($this->min_days) ? '' : format_plural($this->min_days, '@count day', '@count days', array('@count' => $this->min_days)); |
||
| 97 | $maximum_stay = empty($this->max_days) ? '' : format_plural($this->max_days, '@count day', '@count days', array('@count' => $this->max_days)); |
||
| 98 | |||
| 99 | // Day of the week constraint variable. |
||
| 100 | $day_of_the_week = rooms_availability_constraints_get_weekday($this->checkin_day); |
||
| 101 | |||
| 102 | // Date range constraint variables. |
||
| 103 | $start_date = $this->start_date->format('Y-m-d'); |
||
| 104 | $end_date = $this->end_date->format('Y-m-d'); |
||
| 105 | |||
| 106 | // Next create replacement placeholders to be used in t() below. |
||
| 107 | $args = array( |
||
| 108 | '@minimum_stay' => $minimum_stay, |
||
| 109 | '@maximum_stay' => $maximum_stay, |
||
| 110 | '@start_date' => $start_date, |
||
| 111 | '@end_date' => $end_date, |
||
| 112 | '@day_of_the_week' => $day_of_the_week, |
||
| 113 | ); |
||
| 114 | |||
| 115 | // Finally, build out the constraint text string adding components |
||
| 116 | // as necessary. |
||
| 117 | |||
| 118 | // Specify a date range constraint. |
||
| 119 | if ($start_date && $end_date) { |
||
| 120 | $text = t('From @start_date to @end_date', $args); |
||
| 121 | } |
||
| 122 | |||
| 123 | // Specify the day of the week constraint. |
||
| 124 | if ($day_of_the_week) { |
||
| 125 | if ($start_date && $end_date) { |
||
| 126 | $text = t('From @start_date to @end_date, if booking starts on @day_of_the_week', $args); |
||
| 127 | } |
||
| 128 | else { |
||
| 129 | $text = t('If booking starts on @day_of_the_week', $args); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | // Specify the min/max stay length constraint. |
||
| 134 | if ($minimum_stay || $maximum_stay) { |
||
| 135 | if (empty($text)) { |
||
| 136 | $text = t('The stay') . ' '; |
||
| 137 | } |
||
| 138 | else { |
||
| 139 | $text .= ' ' . t('the stay') . ' '; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | if ($minimum_stay && $maximum_stay) { |
||
| 143 | // Special case when min stay and max stay are the same. |
||
| 144 | if ($minimum_stay == $maximum_stay) { |
||
| 145 | $text .= t('must be for @minimum_stay', $args); |
||
| 146 | } |
||
| 147 | else { |
||
| 148 | $text .= t('must be at least @minimum_stay and at most @maximum_stay', $args); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | elseif ($minimum_stay) { |
||
| 152 | $text .= t('must be for at least @minimum_stay', $args); |
||
| 153 | } |
||
| 154 | elseif ($maximum_stay) { |
||
| 155 | $text .= t('cannot be more than @maximum_stay', $args); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $text; |
||
| 159 | } |
||
| 160 | |||
| 162 |
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: