| Total Complexity | 9 |
| Total Lines | 56 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | trait Countable |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param Model $model |
||
| 16 | * @param $counter |
||
| 17 | * @param int $amount |
||
| 18 | * @return Model |
||
| 19 | */ |
||
| 20 | public function incrementCounter( Model $model, $counter, $amount = 1 ) |
||
| 21 | { |
||
| 22 | $amount = $this->getAmount( $amount ); |
||
| 23 | |||
| 24 | if ( $amount ) $model->increment( $counter, $amount ); |
||
|
|
|||
| 25 | else $model->increment( $counter ); |
||
| 26 | |||
| 27 | return $model; |
||
| 28 | } |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * @param Model $model |
||
| 33 | * @param $counter |
||
| 34 | * @param int $amount |
||
| 35 | * @param bool|FALSE $allowNegative |
||
| 36 | * @return Model |
||
| 37 | */ |
||
| 38 | public function decrementCounter( Model $model, $counter, $amount = 1, $allowNegative = FALSE ) |
||
| 39 | { |
||
| 40 | $amount = $this->getAmount( $amount, 1 ); |
||
| 41 | |||
| 42 | $currentValue = $model->{$counter}; |
||
| 43 | $final = $model->{$counter} - $amount; |
||
| 44 | |||
| 45 | if ( !$allowNegative ) |
||
| 46 | { |
||
| 47 | if ( $currentValue && $final < 0 ) |
||
| 48 | { |
||
| 49 | $model->{$counter} = 0; |
||
| 50 | $model->save(); |
||
| 51 | } |
||
| 52 | |||
| 53 | } else $model->decrement( $counter, $amount ); |
||
| 54 | |||
| 55 | return $model; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @param $amount |
||
| 61 | * @param null $default |
||
| 62 | * @return int|null |
||
| 63 | */ |
||
| 64 | private function getAmount( $amount, $default = NULL ) |
||
| 67 | } |
||
| 68 | } |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: