Completed
Push — master ( a169f8...b546ba )
by Siad
16:03
created

FileList::setDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
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
 * 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 9
    public function __construct($filelist = null)
67
    {
68 9
        parent::__construct();
69
70 9
        if ($filelist !== null) {
71
            $this->dir = $filelist->dir;
72
            $this->filenames = $filelist->filenames;
73
            $this->listfile = $filelist->listfile;
74
        }
75 9
    }
76
77
    public function getIterator()
78
    {
79
        return new ArrayIterator($this->getFiles());
0 ignored issues
show
Bug introduced by
The call to FileList::getFiles() has too few arguments starting with p. ( Ignorable by Annotation )

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

79
        return new ArrayIterator($this->/** @scrutinizer ignore-call */ getFiles());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
80
    }
81
82
    /**
83
     * Makes this instance in effect a reference to another FileList
84
     * instance.
85
     *
86
     * @param  Reference $r
87
     * @throws BuildException
88
     */
89 2
    public function setRefid(Reference $r)
90
    {
91 2
        if ($this->dir !== null || count($this->filenames) !== 0) {
92 2
            throw $this->tooManyAttributes();
93
        }
94
        parent::setRefid($r);
95
    }
96
97
    /**
98
     * Base directory for files in list.
99
     *
100
     * @param PhingFile $dir
101
     * @throws IOException
102
     * @throws NullPointerException
103
     */
104 6
    public function setDir(PhingFile $dir)
105
    {
106 6
        if ($this->isReference()) {
107
            throw $this->tooManyAttributes();
108
        }
109 6
        $this->dir = $dir;
110 6
    }
111
112
    /**
113
     * Get the basedir for files in list.
114
     *
115
     * @param  Project $p
116
     * @throws BuildException
117
     * @return PhingFile
118
     */
119 3
    public function getDir(Project $p)
120
    {
121 3
        if ($this->isReference()) {
122
            $ref = $this->getRef($p);
123
124
            return $ref->getDir($p);
125
        }
126
127 3
        return $this->dir;
128
    }
129
130
    /**
131
     * Set the array of files in list.
132
     *
133
     * @param  array $filenames
134
     * @throws BuildException
135
     */
136 4
    public function setFiles($filenames)
137
    {
138 4
        if ($this->isReference()) {
139
            throw $this->tooManyAttributes();
140
        }
141 4
        if (!empty($filenames)) {
142 4
            $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

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

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