HasNumericAttributes   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 30
ccs 14
cts 14
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A increment() 0 13 4
A decrement() 0 13 4
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