Completed
Pull Request — master (#16)
by
unknown
20:01
created

GlobIterator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 62
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 52 9
1
<?php
2
3
/*
4
 * This file is part of the webmozart/glob package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CPSIT\Glob\Iterator;
13
14
use ArrayIterator;
15
use EmptyIterator;
16
use IteratorIterator;
17
use RecursiveIteratorIterator;
18
use CPSIT\Glob\Glob;
19
20
/**
21
 * Returns filesystem paths matching a glob.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 *
27
 * @see    Glob
28
 */
29
class GlobIterator extends IteratorIterator
30
{
31
    /**
32
     * Creates a new iterator.
33
     *
34
     * @param string $glob  The glob pattern.
35
     * @param int    $flags A bitwise combination of the flag constants in
36
     *                      {@link Glob}.
37
     */
38 21
    public function __construct($glob, $flags = 0)
39
    {
40 21
        $basePath = Glob::getBasePath($glob, $flags);
41
42 20
        if (!Glob::isDynamic($glob) && file_exists($glob)) {
43
            // If the glob is a file path, return that path
44 3
            $innerIterator = new ArrayIterator(array($glob));
45 17
        } elseif (is_dir($basePath)) {
46
            // Use the system's much more efficient glob() function where we can
47
            if (
48
                // glob() does not support /**/
49 16
                false === strpos($glob, '/**/') &&
50
                // glob() does not support stream wrappers
51 16
                false === strpos($glob, '://') &&
52
                // glob() does not support [^...] on Windows
53 16
                ('\\' !== DIRECTORY_SEPARATOR || false === strpos($glob, '[^'))
54
            ) {
55 10
                $results = glob($glob, GLOB_BRACE);
56
57
                // $results may be empty or false if $glob is invalid
58 10
                if (empty($results)) {
59
                    // Parse glob and provoke errors if invalid
60 4
                    Glob::toRegEx($glob);
61
62
                    // Otherwise return empty result set
63 2
                    $innerIterator = new EmptyIterator();
64
                } else {
65 8
                    $innerIterator = new ArrayIterator($results);
66
                }
67
            } else {
68
                // Otherwise scan the glob's base directory for matches
69 7
                $innerIterator = new GlobFilterIterator(
70
                    $glob,
71 7
                    new RecursiveIteratorIterator(
72 7
                        new RecursiveDirectoryIterator(
73
                            $basePath,
74 7
                            RecursiveDirectoryIterator::CURRENT_AS_PATHNAME
75 7
                                | RecursiveDirectoryIterator::SKIP_DOTS
76
                        ),
77 7
                        RecursiveIteratorIterator::SELF_FIRST
78
                    ),
79 14
                    GlobFilterIterator::FILTER_VALUE,
80
                    $flags
81
                );
82
            }
83
        } else {
84
            // If the glob's base directory does not exist, return nothing
85 1
            $innerIterator = new EmptyIterator();
86
        }
87
88 16
        parent::__construct($innerIterator);
89 16
    }
90
}
91