Completed
Push — develop ( f11ef2...d41b65 )
by David
06:01 queued 11s
created

Glob   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 9.2
c 0
b 0
f 0
wmc 40
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F toRegex() 0 68 40

How to fix   Complexity   

Complex Class

Complex classes like Glob often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Glob, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[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 Symfony\Component\Finder;
13
14
/**
15
 * Glob matches globbing patterns against text.
16
 *
17
 *     if match_glob("foo.*", "foo.bar") echo "matched\n";
18
 *
19
 *     // prints foo.bar and foo.baz
20
 *     $regex = glob_to_regex("foo.*");
21
 *     for (['foo.bar', 'foo.baz', 'foo', 'bar'] as $t)
22
 *     {
23
 *         if (/$regex/) echo "matched: $car\n";
24
 *     }
25
 *
26
 * Glob implements glob(3) style matching that can be used to match
27
 * against text, rather than fetching names from a filesystem.
28
 *
29
 * Based on the Perl Text::Glob module.
30
 *
31
 * @author Fabien Potencier <[email protected]> PHP port
32
 * @author     Richard Clamp <[email protected]> Perl version
33
 * @copyright  2004-2005 Fabien Potencier <[email protected]>
34
 * @copyright  2002 Richard Clamp <[email protected]>
35
 */
36
class Glob
37
{
38
    /**
39
     * Returns a regexp which is the equivalent of the glob pattern.
40
     *
41
     * @return string
42
     */
43
    public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#')
44
    {
45
        $firstByte = true;
46
        $escaping = false;
47
        $inCurlies = 0;
48
        $regex = '';
49
        $sizeGlob = \strlen($glob);
50
        for ($i = 0; $i < $sizeGlob; ++$i) {
51
            $car = $glob[$i];
52
            if ($firstByte && $strictLeadingDot && '.' !== $car) {
53
                $regex .= '(?=[^\.])';
54
            }
55
56
            $firstByte = '/' === $car;
57
58
            if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
59
                $car = '[^/]++/';
60
                if (!isset($glob[$i + 3])) {
61
                    $car .= '?';
62
                }
63
64
                if ($strictLeadingDot) {
65
                    $car = '(?=[^\.])'.$car;
66
                }
67
68
                $car = '/(?:'.$car.')*';
69
                $i += 2 + isset($glob[$i + 3]);
70
71
                if ('/' === $delimiter) {
72
                    $car = str_replace('/', '\\/', $car);
73
                }
74
            }
75
76
            if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
77
                $regex .= "\\$car";
78
            } elseif ('*' === $car) {
79
                $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
80
            } elseif ('?' === $car) {
81
                $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
82
            } elseif ('{' === $car) {
83
                $regex .= $escaping ? '\\{' : '(';
84
                if (!$escaping) {
85
                    ++$inCurlies;
86
                }
87
            } elseif ('}' === $car && $inCurlies) {
88
                $regex .= $escaping ? '}' : ')';
89
                if (!$escaping) {
90
                    --$inCurlies;
91
                }
92
            } elseif (',' === $car && $inCurlies) {
93
                $regex .= $escaping ? ',' : '|';
94
            } elseif ('\\' === $car) {
95
                if ($escaping) {
96
                    $regex .= '\\\\';
97
                    $escaping = false;
98
                } else {
99
                    $escaping = true;
100
                }
101
102
                continue;
103
            } else {
104
                $regex .= $car;
105
            }
106
            $escaping = false;
107
        }
108
109
        return $delimiter.'^'.$regex.'$'.$delimiter;
110
    }
111
}
112