Passed
Push — master ( 19c397...01dc81 )
by Michiel
12:23
created

FileList::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
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
 * FileList represents an explicitly named list of files. FileLists
22
 * are useful when you want to capture a list of files regardless of
23
 * whether they currently exist.
24
 *
25
 * <filelist
26
 *    id="docfiles"
27
 *   dir="${phing.docs.dir}"
28
 *   files="chapters/Installation.html,chapters/Setup.html"/>
29
 *
30
 * OR
31
 *
32
 * <filelist
33
 *         dir="${doc.src.dir}"
34
 *         listfile="${phing.docs.dir}/PhingGuide.book"/>
35
 *
36
 * (or a mixture of files="" and listfile="" can be used)
37
 *
38
 * @author  Hans Lellelid <[email protected]>
39
 * @package phing.types
40
 */
41
class FileList extends DataType implements IteratorAggregate
42
{
43
44
    // public for "cloning" purposes
45
46
    /**
47
     * Array containing all filenames.
48
     */
49
    public $filenames = [];
50
51
    /**
52
     * Base directory for this file list.
53
     */
54
    public $dir;
55
56
    /**
57
     * @var PhingFile that contains a list of files (one per line).
58
     */
59
    public $listfile;
60
61
    /**
62
     * Construct a new FileList.
63
     *
64
     * @param FileList $filelist
65
     */
66 6
    public function __construct($filelist = null)
67
    {
68 6
        parent::__construct();
69
70 6
        if ($filelist !== null) {
71
            $this->dir = $filelist->dir;
72
            $this->filenames = $filelist->filenames;
73
            $this->listfile = $filelist->listfile;
74
        }
75 6
    }
76
77
    public function getIterator()
78
    {
79
        if ($this->isReference()) {
80
            return $this->getRef($this->getProject())->getIterator();
81
        }
82
83
        return new ArrayIterator($this->filenames);
84
    }
85
86
    /**
87
     * Makes this instance in effect a reference to another FileList
88
     * instance.
89
     *
90
     * @param  Reference $r
91
     * @throws BuildException
92
     */
93
    public function setRefid(Reference $r)
94
    {
95
        if ($this->dir !== null || count($this->filenames) !== 0) {
96
            throw $this->tooManyAttributes();
97
        }
98
        parent::setRefid($r);
99
    }
100
101
    /**
102
     * Base directory for files in list.
103
     *
104
     * @param PhingFile $dir
105
     * @throws IOException
106
     * @throws NullPointerException
107
     */
108 5
    public function setDir(PhingFile $dir)
109
    {
110 5
        if ($this->isReference()) {
111
            throw $this->tooManyAttributes();
112
        }
113 5
        if (!($dir instanceof PhingFile)) {
0 ignored issues
show
introduced by
$dir is always a sub-type of PhingFile.
Loading history...
114
            $dir = new PhingFile($dir);
115
        }
116 5
        $this->dir = $dir;
117 5
    }
118
119
    /**
120
     * Get the basedir for files in list.
121
     *
122
     * @param  Project $p
123
     * @throws BuildException
124
     * @return PhingFile
125
     */
126 3
    public function getDir(Project $p)
127
    {
128 3
        if ($this->isReference()) {
129
            $ref = $this->getRef($p);
130
131
            return $ref->getDir($p);
132
        }
133
134 3
        return $this->dir;
135
    }
136
137
    /**
138
     * Set the array of files in list.
139
     *
140
     * @param  array $filenames
141
     * @throws BuildException
142
     */
143 3
    public function setFiles($filenames)
144
    {
145 3
        if ($this->isReference()) {
146
            throw $this->tooManyAttributes();
147
        }
148 3
        if (!empty($filenames)) {
149 3
            $tok = strtok($filenames, ", \t\n\r");
0 ignored issues
show
Bug introduced by
$filenames of type array is incompatible with the type string expected by parameter $str of strtok(). ( Ignorable by Annotation )

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

149
            $tok = strtok(/** @scrutinizer ignore-type */ $filenames, ", \t\n\r");
Loading history...
150 3
            while ($tok !== false) {
151 3
                $fname = trim($tok);
152 3
                if ($fname !== "") {
153 3
                    $this->filenames[] = $tok;
154
                }
155 3
                $tok = strtok(", \t\n\r");
156
            }
157
        }
158 3
    }
159
160
    /**
161
     * Sets a source "list" file that contains filenames to add -- one per line.
162
     *
163
     * @param string $file
164
     * @throws IOException
165
     * @throws NullPointerException
166
     */
167
    public function setListFile($file)
168
    {
169
        if ($this->isReference()) {
170
            throw $this->tooManyAttributes();
171
        }
172
        if (!($file instanceof PhingFile)) {
0 ignored issues
show
introduced by
$file is never a sub-type of PhingFile.
Loading history...
173
            $file = new PhingFile($file);
174
        }
175
        $this->listfile = $file;
176
    }
177
178
    /**
179
     * Get the source "list" file that contains file names.
180
     *
181
     * @param  Project $p
182
     * @return PhingFile
183
     */
184
    public function getListFile(Project $p)
185
    {
186
        if ($this->isReference()) {
187
            $ref = $this->getRef($p);
188
189
            return $ref->getListFile($p);
190
        }
191
192
        return $this->listfile;
193
    }
194
195
    /**
196
     * Returns the list of files represented by this FileList.
197
     *
198
     * @param Project $p
199
     * @return array
200
     * @throws IOException
201
     * @throws BuildException
202
     */
203 5
    public function getFiles(Project $p)
204
    {
205 5
        if ($this->isReference()) {
206
            $ret = $this->getRef($p);
207
            $ret = $ret->getFiles($p);
208
209
            return $ret;
210
        }
211
212 5
        if ($this->dir === null) {
213 1
            throw new BuildException("No directory specified for filelist.");
214
        }
215
216 4
        if ($this->listfile !== null) {
217
            $this->readListFile($p);
218
        }
219
220 4
        if (empty($this->filenames)) {
221 1
            throw new BuildException("No files specified for filelist.");
222
        }
223
224 3
        return $this->filenames;
225
    }
226
227
    /**
228
     * Performs the check for circular references and returns the
229
     * referenced FileSet.
230
     *
231
     * @param Project $p
232
     *
233
     * @throws BuildException
234
     *
235
     * @return FileList
236
     */
237
    public function getRef(Project $p)
0 ignored issues
show
Unused Code introduced by
The parameter $p is not used and could be removed. ( Ignorable by Annotation )

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

237
    public function getRef(/** @scrutinizer ignore-unused */ Project $p)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
238
    {
239
        $dataTypeName = StringHelper::substring(__CLASS__, strrpos(__CLASS__, '\\') + 1);
240
        return $this->getCheckedRef(__CLASS__, $dataTypeName);
241
    }
242
243
    /**
244
     * Reads file names from a file and adds them to the files array.
245
     *
246
     * @param Project $p
247
     *
248
     * @throws BuildException
249
     * @throws IOException
250
     */
251
    private function readListFile(Project $p)
252
    {
253
        $listReader = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $listReader is dead and can be removed.
Loading history...
254
        try {
255
            // Get a FileReader
256
            $listReader = new BufferedReader(new FileReader($this->listfile));
257
258
            $line = $listReader->readLine();
259
            while ($line !== null) {
260
                if (!empty($line)) {
261
                    $line = $p->replaceProperties($line);
262
                    $this->filenames[] = trim($line);
263
                }
264
                $line = $listReader->readLine();
265
            }
266
        } catch (Exception $e) {
267
            if ($listReader) {
0 ignored issues
show
introduced by
$listReader is of type BufferedReader, thus it always evaluated to true.
Loading history...
268
                $listReader->close();
269
            }
270
            throw new BuildException(
271
                "An error occurred while reading from list file " . $this->listfile->__toString() . ": " . $e->getMessage()
272
            );
273
        }
274
275
        $listReader->close();
276
    }
277
}
278