Completed
Push — master ( bc194a...d90c8b )
by Siad
17:01
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 28
    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 28
        $this->tokens = preg_split("/[;:]/", $path, -1, PREG_SPLIT_NO_EMPTY);
70
71 28
        $this->dosStyleFilesystem = (PATH_SEPARATOR == ';');
72 28
    }
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 28
    public function hasMoreTokens()
84
    {
85 28
        if ($this->lookahead !== null) {
86
            return true;
87
        }
88
89 28
        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 28
    public function nextToken()
100
    {
101 28
        if ($this->lookahead !== null) {
102
            $token = $this->lookahead;
103
104
            $this->lookahead = null;
105
        } else {
106 28
            $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 28
        if (strlen($token) === 1 && Character::isLetter($token[0])
111
            && $this->dosStyleFilesystem
112
            && !empty($this->tokens)
113
        ) {
114
            // we are on a dos style system so this path could be a drive
115
116
            // spec. We look at the next token
117
118
            $nextToken = trim(array_shift($this->tokens));
119
120
            if (StringHelper::startsWith('\\', $nextToken) || StringHelper::startsWith('/', $nextToken)) {
121
                // we know we are on a DOS style platform and the next path
122
123
                // starts with a slash or backslash, so we know this is a
124
125
                // drive spec
126
127
                $token .= ':' . $nextToken;
128
            } else {
129
                // store the token just read for next time
130
131
                $this->lookahead = $nextToken;
132
            }
133
        }
134
135 28
        return $token;
136
    }
137
138
    /**
139
     * Non StringTokenizer function, that indicates whether the specified path is contained in loaded tokens.
140
     * We can do this easily because in PHP implimentation we're using arrays.
141
     *
142
     * @param string $path path to search for.
143
     *
144
     * @return boolean
145
     */
146
    public function contains($path)
147
    {
148
        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

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