Passed
Push — master ( b52996...b4af64 )
by Matthew
02:19
created

Yieldable::yieldKeyVal()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 4
nc 6
nop 2
1
<?php
2
3
namespace Dynamic\Salsify\Traits;
4
5
/**
6
 * Trait Yieldable
7
 * @package Dynamic\Salsify\Traits
8
 */
9
trait Yieldable
10
{
11
12
    /**
13
     * @var string
14
     */
15
    public static $STOP_GENERATOR = 'stop';
16
17
    /**
18
     * @param array|iterable $list
19
     * @param callable|null $callback
20
     * @return \Generator
21
     */
22
    public function yieldSingle($list, $callback = null)
23
    {
24
        foreach ($list as $item) {
25
            $injected = (yield $item);
26
27
            if ($injected === static::$STOP_GENERATOR) {
28
                break;
29
            }
30
        }
31
32
        if (is_callable($callback)) {
33
            $this->callback();
0 ignored issues
show
Bug introduced by
It seems like callback() 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

33
            $this->/** @scrutinizer ignore-call */ 
34
                   callback();
Loading history...
34
        }
35
    }
36
37
    /**
38
     * @param array|iterable $list
39
     * @param callable|null $callback
40
     * @return \Generator
41
     */
42
    public function yieldKeyVal($list, $callback = null)
43
    {
44
        foreach ($list as $key => $val) {
45
            $injected = (yield $key => $val);
46
47
            if ($injected === static::$STOP_GENERATOR) {
48
                break;
49
            }
50
        }
51
52
        if (is_callable($callback)) {
53
            $this->callback();
54
        }
55
    }
56
}
57