SubIterator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 83
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 9 1
A map() 0 9 1
A fold() 0 9 1
A mapTop() 0 4 1
A mapTopOnNonFiles() 0 9 2
A wrap() 0 4 1
A wrappedMapTop() 0 4 1
A wrappedMapTopOnNonFiles() 0 4 1
1
<?php
2
/******************************************************************************
3
 * An iterator interface over the Leagues flysystem.
4
 * Copyright (c) 2021, 2015 Richard Klees <[email protected]>
5
 *
6
 * This software is licensed under GPLv3. You should have received
7
 * a copy of the along with the code.
8
 */
9
10
namespace Lechimp\Flightcontrol;
11
12
/**
13
 * An iterator on each of the contents of another iterator.
14
 */
15
class SubIterator extends Iterator
16
{
17
    /**
18
     * @var Iterator
19
     */
20
    protected $top;
21
22 6
    public function __construct(Iterator $top)
23
    {
24 6
        $this->top = $top;
25 6
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30 2
    public function filter(\Closure $predicate) : Iterator
31
    {
32
        // As we are working somewhere down the iteration, we have to apply
33
        // the filter on all non files in the iterator above and return
34
        // something similar to this.
35 2
        return $this->wrappedMapTopOnNonFiles(function ($obj) use ($predicate) {
36 2
            return $obj->filter($predicate);
37 2
        });
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 4
    public function map(\Closure $trans) : Iterator
44
    {
45
        // As we are working somewhere down the iteration, we have to map
46
        // the transformation on all non files in the iterator above and return
47
        // something similar to this.
48 4
        return $this->wrappedMapTopOnNonFiles(function ($obj) use ($trans) {
49 4
            return $obj->map($trans);
50 4
        });
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 6
    public function fold($start_value, $iteration)
57
    {
58
        // As this unwraps one layer of the iteration, we need to apply the fold
59
        // function to the things in the iterator above and return something
60
        // similar to the iterator above.
61 6
        return $this->mapTopOnNonFiles(function ($v) use ($start_value, $iteration) {
62 6
            return $v->fold($start_value, $iteration);
63 6
        });
64
    }
65
66
    // Helpers
67
68 6
    protected function mapTop(\Closure $trans)
69
    {
70 6
        return $this->top->map($trans);
71
    }
72
73 6
    protected function mapTopOnNonFiles(\Closure $trans)
74
    {
75 6
        return $this->mapTop(function ($obj) use ($trans) {
76 6
            if ($obj->isFile()) {
77 4
                return $obj;
78
            }
79 6
            return $trans($obj);
80 6
        });
81
    }
82
83 4
    protected function wrap(Iterator $iter)
84
    {
85 4
        return new SubIterator($iter);
86
    }
87
88
    protected function wrappedMapTop(\Closure $trans)
89
    {
90
        return $this->wrap($this->mapTop($trans));
91
    }
92
93 4
    protected function wrappedMapTopOnNonFiles(\Closure $trans)
94
    {
95 4
        return $this->wrap($this->mapTopOnNonFiles($trans));
96
    }
97
}
98