1 | <?php |
||||
2 | |||||
3 | namespace JsonFieldCast\Json; |
||||
4 | |||||
5 | trait HasNumericAttributes |
||||
6 | { |
||||
7 | 4 | public function increment(string $key, int|float $amount = 1): static |
|||
8 | { |
||||
9 | 4 | $value = $this->getAttribute($key); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
10 | |||||
11 | 4 | if ($value && !is_numeric($value)) { |
|||
12 | 2 | throw new \InvalidArgumentException("Value of key [{$key}] is not numeric"); |
|||
13 | } |
||||
14 | |||||
15 | 2 | if (!$value) { |
|||
16 | 2 | $value = 0; |
|||
17 | } |
||||
18 | |||||
19 | 2 | return $this->setAttribute($key, $value + $amount); |
|||
0 ignored issues
–
show
It seems like
setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
20 | } |
||||
21 | |||||
22 | 4 | public function decrement(string $key, int|float $amount = 1): static |
|||
23 | { |
||||
24 | 4 | $value = $this->getAttribute($key); |
|||
25 | |||||
26 | 4 | if ($value && !is_numeric($value)) { |
|||
27 | 2 | throw new \InvalidArgumentException("Value of key [{$key}] is not numeric"); |
|||
28 | } |
||||
29 | |||||
30 | 2 | if (!$value) { |
|||
31 | 2 | $value = 0; |
|||
32 | } |
||||
33 | |||||
34 | 2 | return $this->setAttribute($key, $value - $amount); |
|||
35 | } |
||||
36 | } |
||||
37 |