FilterTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 44
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A named() 0 7 1
A directoriesOnly() 0 6 1
A filesOnly() 0 6 1
filter() 0 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
 * Capture similarities for objects that could be filtered for name.
14
 */
15
trait FilterTrait
16
{
17
    /**
18
     * Get a recursor/iterator filters on the name of the objects it works on.
19
     *
20
     * The regexp will be embedded as such "%^$regexp$%" before it is passed
21
     * to preg_match.
22
     */
23 6
    public function named(string $regexp)
24
    {
25 6
        $regexp = "%^$regexp$%";
26 6
        return $this->filter(function (FSObject $obj) use ($regexp) {
27 6
            return preg_match($regexp, $obj->name());
28 6
        });
29
    }
30
31
    /**
32
     * Get an iterator for every directory in the current iterator.
33
     */
34 2
    public function directoriesOnly() : Iterator
35
    {
36 2
        return $this->filter(function (FSObject $obj) {
37 2
            return !$obj->isFile();
38 2
        });
39
    }
40
41
    /**
42
     * Get an iterator for every file in the current iterator.
43
     */
44 2
    public function filesOnly() : Iterator
45
    {
46 2
        return $this->filter(function (FSObject $obj) {
47 2
            return $obj->isFile();
48 2
        });
49
    }
50
51
    /**
52
     * This should actually return a filtered version of the object.
53
     *
54
     * @param   \Closure    $predicate
55
     * @return  mixed
56
     */
57
    abstract public function filter(\Closure $predicate);
58
}
59