HasNumericAttributes::increment()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 10
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
It seems like getAttribute() 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 ignore-call  annotation

9
        /** @scrutinizer ignore-call */ 
10
        $value = $this->getAttribute($key);
Loading history...
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
Bug introduced by
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 ignore-call  annotation

19
        return $this->/** @scrutinizer ignore-call */ setAttribute($key, $value + $amount);
Loading history...
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