Issues (43)

src/Traits/OperationsOnItemsTrait.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
use Exception;
6
7
/**
8
 * Class OperationsOnItemsTrait
9
 * @package Nip\Collections\Traits
10
 */
11
trait OperationsOnItemsTrait
12
{
13
14
    /**
15
     * Execute a callback over each item.
16
     *
17
     * @param  callable  $callback
18
     * @return $this
19
     */
20 1
    public function each(callable $callback)
21
    {
22 1
        foreach ($this as $key => $item) {
23 1
            if ($callback($item, $key) === false) {
24
                break;
25 1
            }
26
        }
27
28 1
        return $this;
29 1
    }
30
31 1
    /**
32
     * @param $key
33
     * @param $value
34
     *
35 1
     * @throws Exception
36 1
     */
37
    public function valueAdd($key, $value)
38
    {
39
        if (!$this->has($key)) {
0 ignored issues
show
It seems like has() 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

39
        if (!$this->/** @scrutinizer ignore-call */ has($key)) {
Loading history...
40
            $this->set($key, $value);
0 ignored issues
show
It seems like set() 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

40
            $this->/** @scrutinizer ignore-call */ 
41
                   set($key, $value);
Loading history...
41
42
            return;
43
        }
44 1
45
        $currentValue = $this->get($key, 0);
0 ignored issues
show
It seems like get() 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

45
        /** @scrutinizer ignore-call */ 
46
        $currentValue = $this->get($key, 0);
Loading history...
46 1
        $currentValue = $currentValue === null ? 0 : $currentValue;
47
48 1
        if (!is_numeric($currentValue)) {
49
            throw new Exception("Current value for key {$key} is not numeric. [{$value}] provided. ");
50
        }
51
52 1
        $this->set($key, ($currentValue + $value));
53 1
    }
54
55
    /**
56
     * @param $key
57
     * @param $value
58
     *
59
     * @throws Exception
60
     */
61
    public function valueSubtract($key, $value)
62
    {
63
        $currentValue = $this->get($key, 0);
64
65
        if (!is_numeric($currentValue)) {
66
            throw new Exception("Current value for key {$key} is not numeric. [{$value}] provided. ");
67
        }
68
69
        $this->set($key, ($currentValue - $value));
70
    }
71
}
72