Passed
Pull Request — master (#35)
by Matthew
02:08 queued 32s
created

Yieldable::yieldSingle()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
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) break;
28
        }
29
30
        if (is_callable($callback)) $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

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