Iterator::fold()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 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 a directory.
14
*/
15
abstract class Iterator
16
{
17 1
    use FilterTrait;
18
19
    /**
20
     * Iterate on the contents of the this iterator.
21
     */
22 4
    public function iterateOn() : Iterator
23
    {
24 4
        return new SubIterator($this);
25
    }
26
27
    /**
28
     * Get an iterator for every content in the current iterator
29
     * for which the provided predicate returns true.
30
     *
31
     * @param  \Closure             $predicate  (a -> Bool)
32
     */
33
    abstract public function filter(\Closure $predicate) : Iterator;
34
35
    /**
36
     * Map a function over the objects inside the iterator.
37
     *
38
     * @param   \Closure    $trans      a -> b
39
     */
40
    abstract public function map(\Closure $trans) : Iterator;
41
42
    /**
43
     * Define the function to be iterated with and close this level
44
     * of iteration.
45
     *
46
     * @param   \Closure    $iteration  a -> File|Directory -> a
47
     * @return  Iterator|mixed
48
     */
49
    abstract public function fold($start_value, $iteration);
50
51
    /**
52
     * Like fold, but with no start value or return.
53
     *
54
     * @param   \Closure    $iteration  File|Directory -> ()
55
     * @return  Iterator|mixed
56
     */
57 18
    public function with($iteration)
58
    {
59
        return $this
60 18
        ->fold(
61 18
            array(),
62 18
            function ($a, $f) use ($iteration) {
63 18
                $iteration($f);
64
                // As the subjacent FDirectory is lazy, we need to evaluate
65
                // the contents, as there might be additional computations
66
                // hidden.
67 18
                if (!$f->isFile()) {
68 16
                    $f->contents();
69
                }
70 18
                $a[] = $f; // Do not disturb the structure of
71 18
                return $a; // the directory tree.
72 18
            }
73
        );
74
    }
75
    
76
    /**
77
     * Close a level of iteration without an iteration function.
78
     *
79
     * @return  Iterator|mixed
80
     */
81 2
    public function run()
82
    {
83 2
        $this->with(function ($obj) {
0 ignored issues
show
Unused Code introduced by
The parameter $obj is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84 2
        });
85 2
    }
86
}
87