Passed
Push — master ( fedd27...f10ad5 )
by Michiel
11:13
created

SelectorUtils::matchPatternStart()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 50
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8.0052

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 8
nop 3
dl 0
loc 50
ccs 22
cts 23
cp 0.9565
crap 8.0052
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * <p>This is a utility class used by selectors and DirectoryScanner. The
22
 * functionality more properly belongs just to selectors, but unfortunately
23
 * DirectoryScanner exposed these as protected methods. Thus we have to
24
 * support any subclasses of DirectoryScanner that may access these methods.
25
 * </p>
26
 * <p>This is a Singleton.</p>
27
 *
28
 * @author  Hans Lellelid, [email protected] (Phing)
29
 * @author  Arnout J. Kuiper, [email protected] (Ant)
30
 * @author  Magesh Umasankar
31
 * @author  Bruce Atherton, [email protected] (Ant)
32
 * @package phing.types.selectors
33
 */
34
class SelectorUtils
35
{
36
    private static $instance;
37
38
    /**
39
     * Retrieves the instance of the Singleton.
40
     */
41 7
    public static function getInstance()
42
    {
43 7
        if (!isset(self::$instance)) {
44 1
            self::$instance = new SelectorUtils();
45
        }
46
47 7
        return self::$instance;
48
    }
49
50
    /**
51
     * Tests whether or not a given path matches the start of a given
52
     * pattern up to the first "**".
53
     * <p>
54
     * This is not a general purpose test and should only be used if you
55
     * can live with false positives. For example, <code>pattern=**\a</code>
56
     * and <code>str=b</code> will yield <code>true</code>.
57
     *
58
     * @param string $pattern
59
     * @param string $str
60
     * @param bool $isCaseSensitive
61
     *
62
     * @internal param The $pattern pattern to match against. Must not be
63
     *                <code>null</code>.
64
     * @internal param The $str path to match, as a String. Must not be
65
     *                <code>null</code>.
66
     * @internal param Whether $isCaseSensitive or not matching should be performed
67
     *                        case sensitively.
68
     *
69
     * @return bool whether or not a given path matches the start of a given
70
     *                 pattern up to the first "**".
71
     */
72 33
    public static function matchPatternStart($pattern, $str, $isCaseSensitive = true)
73
    {
74
75
        // When str starts with a DIRECTORY_SEPARATOR, pattern has to start with a
76
        // DIRECTORY_SEPARATOR.
77
        // When pattern starts with a DIRECTORY_SEPARATOR, str has to start with a
78
        // DIRECTORY_SEPARATOR.
79
        if (
80 33
            StringHelper::startsWith(DIRECTORY_SEPARATOR, $str) !== StringHelper::startsWith(
81 33
                DIRECTORY_SEPARATOR,
82
                $pattern
83
            )
84
        ) {
85
            return false;
86
        }
87
88 33
        $patDirs = explode(DIRECTORY_SEPARATOR, $pattern);
89 33
        $strDirs = explode(DIRECTORY_SEPARATOR, $str);
90
91 33
        $patIdxStart = 0;
92 33
        $patIdxEnd = count($patDirs) - 1;
93 33
        $strIdxStart = 0;
94 33
        $strIdxEnd = count($strDirs) - 1;
95
96
        // up to first '**'
97 33
        while ($patIdxStart <= $patIdxEnd && $strIdxStart <= $strIdxEnd) {
98 33
            $patDir = $patDirs[$patIdxStart];
99 33
            if ($patDir == "**") {
100 17
                break;
101
            }
102 20
            if (!self::match($patDir, $strDirs[$strIdxStart], $isCaseSensitive)) {
103 9
                return false;
104
            }
105 14
            $patIdxStart++;
106 14
            $strIdxStart++;
107
        }
108
109 27
        if ($strIdxStart > $strIdxEnd) {
110
            // String is exhausted
111 12
            return true;
112
        }
113
114 18
        if ($patIdxStart > $patIdxEnd) {
115
            // String not exhausted, but pattern is. Failure.
116 1
            return false;
117
        }
118
119
// pattern now holds ** while string is not exhausted
120
        // this will generate false positives but we can live with that.
121 17
        return true;
122
    }
123
124
    /**
125
     * Tests whether or not a given path matches a given pattern.
126
     *
127
     * @param string $pattern The pattern to match against. Must not be <code>null</code>.
128
     * @param string $str The path to match, as a String. Must not be <code>null</code>.
129
     * @param bool $isCaseSensitive Whether or not matching should be performed case sensitively.
130
     *
131
     * @return bool <code>true</code> if the pattern matches against the string,
132
     */
133 118
    public static function matchPath($pattern, $str, $isCaseSensitive = true)
134
    {
135
        // explicitly exclude directory itself
136 118
        if ($str == '' && $pattern == '**/*') {
137 1
            return false;
138
        }
139
140 117
        $rePattern = preg_quote($pattern, '/');
141 117
        $dirSep = preg_quote(DIRECTORY_SEPARATOR, '/');
142 117
        $trailingDirSep = '((' . $dirSep . ')?|(' . $dirSep . ').+)';
143
        $patternReplacements = [
144 117
            $dirSep . '\*\*' . $dirSep => $dirSep . '.*' . $trailingDirSep,
145 117
            $dirSep . '\*\*' => $trailingDirSep,
146 117
            '\*\*' . $dirSep => '(.*' . $dirSep . ')?',
147 117
            '\*\*' => '.*',
148 117
            '\*' => '[^' . $dirSep . ']*',
149 117
            '\?' => '[^' . $dirSep . ']'
150
        ];
151 117
        $rePattern = str_replace(array_keys($patternReplacements), array_values($patternReplacements), $rePattern);
152 117
        $rePattern = '/^' . $rePattern . '$/' . ($isCaseSensitive ? '' : 'i');
153
154 117
        return (bool) preg_match($rePattern, $str);
155
    }
156
157
    /**
158
     * Tests whether or not a string matches against a pattern.
159
     * The pattern may contain two special characters:<br>
160
     * '*' means zero or more characters<br>
161
     * '?' means one and only one character
162
     *
163
     * @param string $pattern The pattern to match against.
164
     *                                Must not be
165
     *                                <code>null</code>.
166
     * @param string $str The string which must be matched against the pattern.
167
     *                                Must not be <code>null</code>.
168
     * @param bool $isCaseSensitive Whether or not matching should be performed
169
     *                                case sensitively.case sensitively.
170
     *
171
     * @return bool <code>true</code> if the string matches against the pattern,
172
     *                           or <code>false</code> otherwise.
173
     */
174 20
    public static function match($pattern, $str, $isCaseSensitive = true)
175
    {
176 20
        $rePattern = preg_quote($pattern, '/');
177 20
        $rePattern = str_replace(["\*", "\?"], ['.*', '.'], $rePattern);
178 20
        $rePattern = '/^' . $rePattern . '$/' . ($isCaseSensitive ? '' : 'i');
179
180 20
        return (bool) preg_match($rePattern, $str);
181
    }
182
183
    /**
184
     * Returns dependency information on these two files. If src has been
185
     * modified later than target, it returns true. If target doesn't exist,
186
     * it likewise returns true. Otherwise, target is newer than src and
187
     * is not out of date, thus the method returns false. It also returns
188
     * false if the src file doesn't even exist, since how could the
189
     * target then be out of date.
190
     *
191
     * @param  PhingFile $src the original file
192
     * @param  PhingFile $target the file being compared against
193
     * @param  int $granularity the amount in seconds of slack we will give in
194
     *                                determining out of dateness
195
     * @return bool whether   the target is out of date
196
     */
197 3
    public static function isOutOfDate(PhingFile $src, PhingFile $target, $granularity)
198
    {
199 3
        if (!$src->exists()) {
200 1
            return false;
201
        }
202 2
        if (!$target->exists()) {
203 1
            return true;
204
        }
205 1
        if (($src->lastModified() - $granularity) > $target->lastModified()) {
206
            return true;
207
        }
208
209 1
        return false;
210
    }
211
212
    /**
213
     * @param string $string
214
     * @return string
215
     */
216 1
    public static function removeWhitespace($string)
217
    {
218 1
        return preg_replace(
219 1
            "/(\t|\n|\v|\f|\r| |\xC2\x85|\xc2\xa0|\xe1\xa0\x8e|\xe2\x80[\x80-\x8D]|\xe2\x80\xa8|\xe2\x80\xa9|\xe2\x80\xaF|\xe2\x81\x9f|\xe2\x81\xa0|\xe3\x80\x80|\xef\xbb\xbf)+/",
220 1
            '',
221 1
            $string
222
        );
223
    }
224
}
225