Failed Conditions
Push — master ( 1b60f6...3023cd )
by Bernhard
02:53
created

GlobIterator::__construct()   C

Complexity

Conditions 12
Paths 7

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 12.0988

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 56
ccs 31
cts 34
cp 0.9118
rs 6.7093
cc 12
eloc 34
nc 7
nop 2
crap 12.0988

How to fix   Long Method    Complexity   

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 Webmozart\Glob\Iterator;
13
14
use ArrayIterator;
15
use EmptyIterator;
16
use InvalidArgumentException;
17
use IteratorIterator;
18
use RecursiveIteratorIterator;
19
use Webmozart\Glob\Glob;
20
21
/**
22
 * Returns filesystem paths matching a glob.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 *
28
 * @see    Glob
29
 */
30
class GlobIterator extends IteratorIterator
31
{
32
    /**
33
     * Creates a new iterator.
34
     *
35
     * @param string $glob  The glob pattern.
36
     * @param int    $flags A bitwise combination of the flag constants in
37
     *                      {@link Glob}.
38
     */
39 19
    public function __construct($glob, $flags = 0)
40
    {
41 19
        $basePath = Glob::getBasePath($glob);
42
43 18
        if (!Glob::isDynamic($glob) && file_exists($glob)) {
44
            // If the glob is a file path, return that path
45 3
            $innerIterator = new ArrayIterator(array($glob));
46 18
        } elseif (is_dir($basePath)) {
47
            // Use the system's much more efficient glob() function where we can
48
            if (
49
                // glob() does not support /**/
50 14
                false === strpos($glob, '/**/') &&
51
                // glob() does not support stream wrappers
52 14
                false === strpos($glob, '://') &&
53
                // glob() does not support [^...] on Windows
54 10
                ('\\' !== DIRECTORY_SEPARATOR || false === strpos($glob, '[^'))
55 14
            ) {
56 10
                if (false === $results = glob($glob, GLOB_BRACE)) {
57
                    $innerIterator = new EmptyIterator();
58
                } else {
59 10
                    $innerIterator = new ArrayIterator($results);
60
                }
61 10
            } else {
62
                try {
63
                    // Otherwise scan the glob's base directory for matches
64 7
                    $innerIterator = new GlobFilterIterator(
65 7
                        $glob,
66 7
                        new RecursiveIteratorIterator(
67 7
                            new RecursiveDirectoryIterator(
68 7
                                $basePath,
69
                                RecursiveDirectoryIterator::CURRENT_AS_PATHNAME
70 7
                                    | RecursiveDirectoryIterator::SKIP_DOTS
71 7
                            ),
72
                            RecursiveIteratorIterator::SELF_FIRST
73 7
                        ),
74 7
                        GlobFilterIterator::FILTER_VALUE,
75
                        $flags
76 7
                    );
77 7
                } catch (InvalidArgumentException $e) {
78 2
                    if (0 === strpos($e->getMessage(), 'Invalid glob: missing ]')
79 2
                        || 0 === strpos($e->getMessage(), 'Invalid glob: missing }')) {
80
                        // Remain compatible with glob() which simply returns
81
                        // nothing in this case
82 2
                        $innerIterator = new EmptyIterator();
83 2
                    } else {
84
                        throw $e;
85
                    }
86
                }
87
            }
88 14
        } else {
89
            // If the glob's base directory does not exist, return nothing
90 1
            $innerIterator = new EmptyIterator();
91
        }
92
93 18
        parent::__construct($innerIterator);
94 18
    }
95
}
96