Passed
Push — master ( eabf33...342b63 )
by Michiel
05:53
created

SelectorUtils   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 96.72%

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 188
ccs 59
cts 61
cp 0.9672
rs 10
c 0
b 0
f 0
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A matchPath() 0 22 4
A match() 0 7 2
A isOutOfDate() 0 13 4
A getInstance() 0 7 2
A removeWhitespace() 0 6 1
B matchPatternStart() 0 50 8
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
use Phing\Io\File;
21
use Phing\Util\StringHelper;
22
23
/**
24
 * <p>This is a utility class used by selectors and DirectoryScanner. The
25
 * functionality more properly belongs just to selectors, but unfortunately
26
 * DirectoryScanner exposed these as protected methods. Thus we have to
27
 * support any subclasses of DirectoryScanner that may access these methods.
28
 * </p>
29
 * <p>This is a Singleton.</p>
30
 *
31
 * @author  Hans Lellelid, [email protected] (Phing)
32
 * @author  Arnout J. Kuiper, [email protected] (Ant)
33
 * @author  Magesh Umasankar
34
 * @author  Bruce Atherton, [email protected] (Ant)
35
 * @package phing.types.selectors
36
 */
37
class SelectorUtils
38
{
39
    private static $instance;
40
41
    /**
42
     * Retrieves the instance of the Singleton.
43
     */
44 7
    public static function getInstance()
45
    {
46 7
        if (!isset(self::$instance)) {
47 1
            self::$instance = new SelectorUtils();
48
        }
49
50 7
        return self::$instance;
51
    }
52
53
    /**
54
     * Tests whether or not a given path matches the start of a given
55
     * pattern up to the first "**".
56
     * <p>
57
     * This is not a general purpose test and should only be used if you
58
     * can live with false positives. For example, <code>pattern=**\a</code>
59
     * and <code>str=b</code> will yield <code>true</code>.
60
     *
61
     * @param string $pattern
62
     * @param string $str
63
     * @param bool $isCaseSensitive
64
     *
65
     * @internal param The $pattern pattern to match against. Must not be
66
     *                <code>null</code>.
67
     * @internal param The $str path to match, as a String. Must not be
68
     *                <code>null</code>.
69
     * @internal param Whether $isCaseSensitive or not matching should be performed
70
     *                        case sensitively.
71
     *
72
     * @return bool whether or not a given path matches the start of a given
73
     *                 pattern up to the first "**".
74
     */
75 38
    public static function matchPatternStart($pattern, $str, $isCaseSensitive = true)
76
    {
77
78
        // When str starts with a DIRECTORY_SEPARATOR, pattern has to start with a
79
        // DIRECTORY_SEPARATOR.
80
        // When pattern starts with a DIRECTORY_SEPARATOR, str has to start with a
81
        // DIRECTORY_SEPARATOR.
82
        if (
83 38
            StringHelper::startsWith(DIRECTORY_SEPARATOR, $str) !== StringHelper::startsWith(
84 38
                DIRECTORY_SEPARATOR,
85 38
                $pattern
86
            )
87
        ) {
88
            return false;
89
        }
90
91 38
        $patDirs = explode(DIRECTORY_SEPARATOR, $pattern);
92 38
        $strDirs = explode(DIRECTORY_SEPARATOR, $str);
93
94 38
        $patIdxStart = 0;
95 38
        $patIdxEnd = count($patDirs) - 1;
96 38
        $strIdxStart = 0;
97 38
        $strIdxEnd = count($strDirs) - 1;
98
99
        // up to first '**'
100 38
        while ($patIdxStart <= $patIdxEnd && $strIdxStart <= $strIdxEnd) {
101 38
            $patDir = $patDirs[$patIdxStart];
102 38
            if ($patDir == "**") {
103 17
                break;
104
            }
105 25
            if (!self::match($patDir, $strDirs[$strIdxStart], $isCaseSensitive)) {
106 19
                return false;
107
            }
108 10
            $patIdxStart++;
109 10
            $strIdxStart++;
110
        }
111
112 23
        if ($strIdxStart > $strIdxEnd) {
113
            // String is exhausted
114 8
            return true;
115
        }
116
117 18
        if ($patIdxStart > $patIdxEnd) {
118
            // String not exhausted, but pattern is. Failure.
119 1
            return false;
120
        }
121
122
// pattern now holds ** while string is not exhausted
123
        // this will generate false positives but we can live with that.
124 17
        return true;
125
    }
126
127
    /**
128
     * Tests whether or not a given path matches a given pattern.
129
     *
130
     * @param string $pattern The pattern to match against. Must not be <code>null</code>.
131
     * @param string $str The path to match, as a String. Must not be <code>null</code>.
132
     * @param bool $isCaseSensitive Whether or not matching should be performed case sensitively.
133
     *
134
     * @return bool <code>true</code> if the pattern matches against the string,
135
     */
136 147
    public static function matchPath($pattern, $str, $isCaseSensitive = true)
137
    {
138
        // explicitly exclude directory itself
139 147
        if ($str == '' && $pattern == '**/*') {
140 1
            return false;
141
        }
142
143 146
        $rePattern = preg_quote($pattern, '/');
144 146
        $dirSep = preg_quote(DIRECTORY_SEPARATOR, '/');
145 146
        $trailingDirSep = '((' . $dirSep . ')?|(' . $dirSep . ').+)';
146
        $patternReplacements = [
147 146
            $dirSep . '\*\*' . $dirSep => $dirSep . '.*' . $trailingDirSep,
148 146
            $dirSep . '\*\*' => $trailingDirSep,
149 146
            '\*\*' . $dirSep => '(.*' . $dirSep . ')?',
150 146
            '\*\*' => '.*',
151 146
            '\*' => '[^' . $dirSep . ']*',
152 146
            '\?' => '[^' . $dirSep . ']'
153
        ];
154 146
        $rePattern = str_replace(array_keys($patternReplacements), array_values($patternReplacements), $rePattern);
155 146
        $rePattern = '/^' . $rePattern . '$/' . ($isCaseSensitive ? '' : 'i');
156
157 146
        return (bool) preg_match($rePattern, $str);
158
    }
159
160
    /**
161
     * Tests whether or not a string matches against a pattern.
162
     * The pattern may contain two special characters:<br>
163
     * '*' means zero or more characters<br>
164
     * '?' means one and only one character
165
     *
166
     * @param string $pattern The pattern to match against.
167
     *                                Must not be
168
     *                                <code>null</code>.
169
     * @param string $str The string which must be matched against the pattern.
170
     *                                Must not be <code>null</code>.
171
     * @param bool $isCaseSensitive Whether or not matching should be performed
172
     *                                case sensitively.case sensitively.
173
     *
174
     * @return bool <code>true</code> if the string matches against the pattern,
175
     *                           or <code>false</code> otherwise.
176
     */
177 25
    public static function match($pattern, $str, $isCaseSensitive = true)
178
    {
179 25
        $rePattern = preg_quote($pattern, '/');
180 25
        $rePattern = str_replace(["\*", "\?"], ['.*', '.'], $rePattern);
181 25
        $rePattern = '/^' . $rePattern . '$/' . ($isCaseSensitive ? '' : 'i');
182
183 25
        return (bool) preg_match($rePattern, $str);
184
    }
185
186
    /**
187
     * Returns dependency information on these two files. If src has been
188
     * modified later than target, it returns true. If target doesn't exist,
189
     * it likewise returns true. Otherwise, target is newer than src and
190
     * is not out of date, thus the method returns false. It also returns
191
     * false if the src file doesn't even exist, since how could the
192
     * target then be out of date.
193
     *
194
     * @param  File $src the original file
195
     * @param  File $target the file being compared against
196
     * @param  int $granularity the amount in seconds of slack we will give in
197
     *                                determining out of dateness
198
     * @return bool whether   the target is out of date
199
     */
200 3
    public static function isOutOfDate(File $src, File $target, $granularity)
201
    {
202 3
        if (!$src->exists()) {
203 1
            return false;
204
        }
205 2
        if (!$target->exists()) {
206 1
            return true;
207
        }
208 1
        if (($src->lastModified() - $granularity) > $target->lastModified()) {
209
            return true;
210
        }
211
212 1
        return false;
213
    }
214
215
    /**
216
     * @param string $string
217
     * @return string
218
     */
219 1
    public static function removeWhitespace($string)
220
    {
221 1
        return preg_replace(
222 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)+/",
223 1
            '',
224 1
            $string
225
        );
226
    }
227
}
228