Passed
Push — master ( a862c8...fef60d )
by Siad
05:54
created

PathTokenizer::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
 * A Path tokenizer takes a path and returns the components that make up
22
 * that path.
23
 *
24
 * The path can use path separators of either ':' or ';' and file separators
25
 * of either '/' or '\'.
26
 *
27
 * @author Hans Lellelid <[email protected]> (Phing)
28
 * @author Conor MacNeill (Ant)
29
 * @author Jeff Tulley <[email protected]>  (Ant)
30
 *
31
 * @package phing.util
32
 */
33
class PathTokenizer
34
{
35
    /**
36
     * A array of tokens, created by preg_split().
37
     */
38
    private $tokens = [];
39
40
    /**
41
     * A string which stores any path components which have been read ahead
42
     * due to DOS filesystem compensation.
43
     *
44
     * @var string
45
     */
46
    private $lookahead;
47
48
    /**
49
     * Flag to indicate whether or not we are running on a platform with a
50
     * DOS style filesystem
51
     *
52
     * @var boolean
53
     */
54
    private $dosStyleFilesystem;
55
56
57
    /**
58
     * Constructs a path tokenizer for the specified path.
59
     *
60
     * @param string $path The path to tokenize. Must not be <code>null</code>.
61
     */
62 48
    public function __construct($path)
63
    {
64
65
        // on Windows and Unix, we can ignore delimiters and still have
66
67
        // enough information to tokenize correctly.
68
69 48
        $this->tokens = preg_split("/[;:]/", $path, -1, PREG_SPLIT_NO_EMPTY);
70
71 48
        $this->dosStyleFilesystem = (PATH_SEPARATOR == ';');
72 48
    }
73
74
75
    /**
76
     * Tests if there are more path elements available from this tokenizer's
77
     * path. If this method returns <code>true</code>, then a subsequent call
78
     * to nextToken will successfully return a token.
79
     *
80
     * @return bool <code>true</code> if and only if there is at least one token
81
     *                                in the string after the current position; <code>false</code> otherwise.
82
     */
83 48
    public function hasMoreTokens()
84
    {
85 48
        if ($this->lookahead !== null) {
86
            return true;
87
        }
88
89 48
        return !empty($this->tokens);
90
    }
91
92
    /**
93
     * Returns the next path element from this tokenizer.
94
     *
95
     * @return string the next path element from this tokenizer.
96
     *
97
     * @throws Exception if there are no more elements in this tokenizer's path.
98
     */
99 48
    public function nextToken()
100
    {
101 48
        if ($this->lookahead !== null) {
102
            $token = $this->lookahead;
103
104
            $this->lookahead = null;
105
        } else {
106 48
            $token = trim(array_shift($this->tokens));
0 ignored issues
show
Bug introduced by
It seems like $this->tokens can also be of type boolean; however, parameter $array of array_shift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
            $token = trim(array_shift(/** @scrutinizer ignore-type */ $this->tokens));
Loading history...
107
        }
108
109
110
        if (
111 48
            strlen($token) === 1
112 48
            && Character::isLetter($token[0])
113 48
            && $this->dosStyleFilesystem
114 48
            && !empty($this->tokens)
115
        ) {
116
            // we are on a dos style system so this path could be a drive
117
118
            // spec. We look at the next token
119
120
            $nextToken = trim(array_shift($this->tokens));
121
122
            if (StringHelper::startsWith('\\', $nextToken) || StringHelper::startsWith('/', $nextToken)) {
123
                // we know we are on a DOS style platform and the next path
124
125
                // starts with a slash or backslash, so we know this is a
126
127
                // drive spec
128
129
                $token .= ':' . $nextToken;
130
            } else {
131
                // store the token just read for next time
132
133
                $this->lookahead = $nextToken;
134
            }
135
        }
136
137 48
        return $token;
138
    }
139
140
    /**
141
     * Non StringTokenizer function, that indicates whether the specified path is contained in loaded tokens.
142
     * We can do this easily because in PHP implimentation we're using arrays.
143
     *
144
     * @param string $path path to search for.
145
     *
146
     * @return boolean
147
     */
148
    public function contains($path)
149
    {
150
        return in_array($path, $this->tokens, true);
0 ignored issues
show
Bug introduced by
It seems like $this->tokens can also be of type boolean; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
        return in_array($path, /** @scrutinizer ignore-type */ $this->tokens, true);
Loading history...
151
    }
152
}
153