| Conditions | 12 |
| Paths | 18 |
| Total Lines | 117 |
| Code Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 113 | public function sentence($ago = false, $referenceDate = null) |
||
| 114 | { |
||
| 115 | $timestamp = $this->selectTimestamp(); |
||
| 116 | $now = $referenceDate === null ? time() : strtotime($referenceDate); |
||
| 117 | $elapsed = $now - $timestamp; |
||
| 118 | |||
| 119 | $future = $elapsed < 0; |
||
| 120 | $elapsed = abs($elapsed); |
||
| 121 | $englishDate = ''; |
||
| 122 | |||
| 123 | $timeFrames = array( |
||
| 124 | array( |
||
| 125 | 'min' => 0, |
||
| 126 | 'max' => 10, |
||
| 127 | 'scale' => 'now', |
||
| 128 | 'plurals' => false, |
||
| 129 | 'divisor' => 1, |
||
| 130 | 'has_future' => false, |
||
| 131 | 'show_elapsed' => false |
||
| 132 | ), |
||
| 133 | array( |
||
| 134 | 'min' => 10, |
||
| 135 | 'max' => 60, |
||
| 136 | 'scale' => 'second', |
||
| 137 | 'plurals' => true, |
||
| 138 | 'divisor' => 1, |
||
| 139 | 'has_future' => false, |
||
| 140 | 'show_elapsed' => true |
||
| 141 | ), |
||
| 142 | array( |
||
| 143 | 'min' => 60, |
||
| 144 | 'max' => 3600, |
||
| 145 | 'scale' => 'minute', |
||
| 146 | 'plurals' => true, |
||
| 147 | 'divisor' => 60, |
||
| 148 | 'has_future' => false, |
||
| 149 | 'show_elapsed' => true |
||
| 150 | ), |
||
| 151 | array( |
||
| 152 | 'min' => 3600, |
||
| 153 | 'max' => 86400, |
||
| 154 | 'scale' => 'hour', |
||
| 155 | 'plurals' => true, |
||
| 156 | 'divisor' => 3600, |
||
| 157 | 'has_future' => false, |
||
| 158 | 'show_elapsed' => true |
||
| 159 | ), |
||
| 160 | array( |
||
| 161 | 'min' => 86400, |
||
| 162 | 'max' => 172800, |
||
| 163 | 'scale' => array('yesterday', 'tomorrow'), |
||
| 164 | 'has_future' => true, |
||
| 165 | 'plurals' => false, |
||
| 166 | 'divisor' => 86400, |
||
| 167 | 'show_elapsed' => false |
||
| 168 | ), |
||
| 169 | array( |
||
| 170 | 'min' => 172800, |
||
| 171 | 'max' => 604800, |
||
| 172 | 'scale' => 'day', |
||
| 173 | 'plurals' => true, |
||
| 174 | 'divisor' => 86400, |
||
| 175 | 'has_future' => false, |
||
| 176 | 'show_elapsed' => true |
||
| 177 | ), |
||
| 178 | array( |
||
| 179 | 'min' => 604800, |
||
| 180 | 'max' => 2419200, |
||
| 181 | 'scale' => 'week', |
||
| 182 | 'plurals' => true, |
||
| 183 | 'divisor' => 604800, |
||
| 184 | 'has_future' => false, |
||
| 185 | 'show_elapsed' => true |
||
| 186 | ), |
||
| 187 | array( |
||
| 188 | 'min' => 2419200, |
||
| 189 | 'max' => 31536000, |
||
| 190 | 'scale' => 'month', |
||
| 191 | 'plurals' => true, |
||
| 192 | 'divisor' => 2419200, |
||
| 193 | 'has_future' => false, |
||
| 194 | 'show_elapsed' => true |
||
| 195 | ), |
||
| 196 | array( |
||
| 197 | 'min' => 31536000, |
||
| 198 | 'max' => 0, |
||
| 199 | 'scale' => 'year', |
||
| 200 | 'plurals' => true, |
||
| 201 | 'divisor' => 31536000, |
||
| 202 | 'has_future' => false, |
||
| 203 | 'show_elapsed' => true |
||
| 204 | ), |
||
| 205 | ); |
||
| 206 | |||
| 207 | foreach($timeFrames as $timeFrame) |
||
| 208 | { |
||
| 209 | if(($elapsed >= $timeFrame['min'] && $elapsed < $timeFrame['max']) || ($elapsed >= $timeFrame['min'] && $timeFrame['max'] === 0)) |
||
| 210 | { |
||
| 211 | $value = floor($elapsed / $timeFrame['divisor']); |
||
| 212 | $englishDate = $this->getEnglishDate($timeFrame, $value, $future); |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | if($englishDate != 'now' && $englishDate != 'yesterday' && $englishDate != 'today' && $ago) |
||
| 218 | { |
||
| 219 | if($future) |
||
| 220 | { |
||
| 221 | $englishDate = 'in '. $englishDate; |
||
| 222 | } |
||
| 223 | else |
||
| 224 | { |
||
| 225 | $englishDate .= ' ago'; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | return $englishDate; |
||
| 230 | } |
||
| 243 |