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

GlobIterator::__construct()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 23
cts 23
cp 1
rs 7.4917
c 0
b 0
f 0
cc 9
nc 5
nop 2
crap 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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