| Conditions | 7 |
| Paths | 24 |
| Total Lines | 52 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 namespace App\Repositories; |
||
| 44 | public function countByInterval(CarbonInterval $interval) |
||
| 45 | { |
||
| 46 | $beginDate = Carbon::now()->sub($interval)->addHour(); |
||
| 47 | |||
| 48 | if( $interval->d>0 ) |
||
| 49 | { |
||
| 50 | $beginDate = $beginDate->startOfDay(); |
||
| 51 | } |
||
| 52 | |||
| 53 | $intervals = []; |
||
| 54 | $dateCounter = clone $beginDate; |
||
| 55 | while($dateCounter <= Carbon::now()) |
||
| 56 | { |
||
| 57 | if( $interval->h>0 ) |
||
| 58 | { |
||
| 59 | $intervals[$dateCounter->hour] = 0; |
||
| 60 | $dateCounter->addHour(1); |
||
| 61 | } |
||
| 62 | else |
||
| 63 | { |
||
| 64 | $intervals[$dateCounter->day] = 0; |
||
| 65 | $dateCounter->addDay(1); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | try |
||
| 70 | { |
||
| 71 | $sales = $this->model->where('is_active', '=', true) |
||
| 72 | ->where('time', '>=', $beginDate) |
||
| 73 | ->get(); |
||
| 74 | } |
||
| 75 | catch(Exception $e) |
||
| 76 | { |
||
| 77 | throw new RepositoryException('Could not get sale data', RepositoryException::DATABASE_ERROR); |
||
| 78 | } |
||
| 79 | |||
| 80 | foreach($sales as $sale) |
||
| 81 | { |
||
| 82 | $carbonTimestamp = Carbon::createFromFormat('Y-m-d H:m:s', $sale->time); |
||
| 83 | |||
| 84 | if( $interval->h>0 ) |
||
| 85 | { |
||
| 86 | $intervals[$carbonTimestamp->hour]++; |
||
| 87 | } |
||
| 88 | else |
||
| 89 | { |
||
| 90 | $intervals[$carbonTimestamp->day]++; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | return $intervals; |
||
| 95 | } |
||
| 96 | |||
| 141 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.