Passed
Push — master ( b90d2b...4d6caf )
by Siad
13:52
created

PatternSet::setIncludesFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 12
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
 * The patternset storage component. Carries all necessary data and methods
22
 * for the patternset stuff.
23
 *
24
 * @author  Andreas Aderhold, [email protected]
25
 * @package phing.types
26
 */
27
class PatternSet extends DataType
28
{
29
    private $includeList = [];
30
    private $excludeList = [];
31
    private $includesFileList = [];
32
    private $excludesFileList = [];
33
34
    /**
35
     * Makes this instance in effect a reference to another PatternSet
36
     * instance.
37
     * You must not set another attribute or nest elements inside
38
     * this element if you make it a reference.
39
     *
40
     * @param  Reference $r
41
     * @throws BuildException
42
     */
43 4
    public function setRefid(Reference $r)
44
    {
45 4
        if (!empty($this->includeList) || !empty($this->excludeList)) {
46
            throw $this->tooManyAttributes();
47
        }
48 4
        parent::setRefid($r);
49 4
    }
50
51
    /**
52
     * Add a name entry on the include list
53
     *
54
     * @return PatternSetNameEntry Reference to object
55
     * @throws BuildException
56
     */
57 87
    public function createInclude()
58
    {
59 87
        if ($this->isReference()) {
60 1
            throw $this->noChildrenAllowed();
61
        }
62
63 86
        return $this->addPatternToList($this->includeList);
64
    }
65
66
    /**
67
     * Add a name entry on the include files list
68
     *
69
     * @return PatternSetNameEntry Reference to object
70
     * @throws BuildException
71
     */
72 1
    public function createIncludesFile()
73
    {
74 1
        if ($this->isReference()) {
75 1
            throw $this->noChildrenAllowed();
76
        }
77
78
        return $this->addPatternToList($this->includesFileList);
79
    }
80
81
    /**
82
     * Add a name entry on the exclude list
83
     *
84
     * @return PatternSetNameEntry Reference to object
85
     * @throws BuildException
86
     */
87 3
    public function createExclude()
88
    {
89 3
        if ($this->isReference()) {
90 1
            throw $this->noChildrenAllowed();
91
        }
92
93 2
        return $this->addPatternToList($this->excludeList);
94
    }
95
96
    /**
97
     * add a name entry on the exclude files list
98
     *
99
     * @return PatternSetNameEntry Reference to object
100
     * @throws BuildException
101
     */
102 1
    public function createExcludesFile()
103
    {
104 1
        if ($this->isReference()) {
105 1
            throw $this->noChildrenAllowed();
106
        }
107
108
        return $this->addPatternToList($this->excludesFileList);
109
    }
110
111
    /**
112
     * Sets the set of include patterns. Patterns may be separated by a comma
113
     * or a space.
114
     *
115
     * @param  string $includes the string containing the include patterns
116
     * @return void
117
     * @throws BuildException
118
     */
119 8
    public function setIncludes($includes)
120
    {
121 8
        if ($this->isReference()) {
122
            throw $this->tooManyAttributes();
123
        }
124 8
        if ($includes !== null && strlen($includes) > 0) {
125 8
            $tok = strtok($includes, ", ");
126 8
            while ($tok !== false) {
127 8
                $o = $this->createInclude();
128 8
                $o->setName($tok);
129 8
                $tok = strtok(", ");
130
            }
131
        }
132 8
    }
133
134
    /**
135
     * Sets the set of exclude patterns. Patterns may be separated by a comma
136
     * or a space.
137
     *
138
     * @param  string the string containing the exclude patterns
139
     * @return void
140
     * @throws BuildException
141
     */
142 1
    public function setExcludes($excludes)
143
    {
144 1
        if ($this->isReference()) {
145
            throw $this->tooManyAttributes();
146
        }
147 1
        if ($excludes !== null && strlen($excludes) > 0) {
148 1
            $tok = strtok($excludes, ", ");
149 1
            while ($tok !== false) {
150 1
                $o = $this->createExclude();
151 1
                $o->setName($tok);
152 1
                $tok = strtok(", ");
153
            }
154
        }
155 1
    }
156
157
    /**
158
     * add a name entry to the given list
159
     *
160
     * @param  array List onto which the nameentry should be added
0 ignored issues
show
Bug introduced by
The type List was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
161
     * @return PatternSetNameEntry Reference to the created PsetNameEntry instance
162
     */
163 88
    private function addPatternToList(&$list)
164
    {
165 88
        $num = array_push($list, new PatternSetNameEntry());
166
167 88
        return $list[$num - 1];
168
    }
169
170
    /**
171
     * Sets the name of the file containing the includes patterns.
172
     *
173
     * @param PhingFile $includesFile file to fetch the include patterns from.
174
     *
175
     * @throws BuildException
176
     */
177
    public function setIncludesFile($includesFile)
178
    {
179
        if ($this->isReference()) {
180
            throw $this->tooManyAttributes();
181
        }
182
        if ($includesFile instanceof PhingFile) {
0 ignored issues
show
introduced by
$includesFile is always a sub-type of PhingFile.
Loading history...
183
            $includesFile = $includesFile->getPath();
184
        }
185
        $o = $this->createIncludesFile();
186
        $o->setName($includesFile);
187
    }
188
189
    /**
190
     * Sets the name of the file containing the excludes patterns.
191
     *
192
     * @param  PhingFile $excludesFile file to fetch the exclude patterns from.
193
     * @throws BuildException
194
     */
195
    public function setExcludesFile($excludesFile)
196
    {
197
        if ($this->isReference()) {
198
            throw $this->tooManyAttributes();
199
        }
200
        if ($excludesFile instanceof PhingFile) {
0 ignored issues
show
introduced by
$excludesFile is always a sub-type of PhingFile.
Loading history...
201
            $excludesFile = $excludesFile->getPath();
202
        }
203
        $o = $this->createExcludesFile();
204
        $o->setName($excludesFile);
205
    }
206
207
    /**
208
     * Reads path matching patterns from a file and adds them to the
209
     * includes or excludes list
210
     *
211
     * @param PhingFile $patternfile
212
     * @param $patternlist
213
     * @param Project $p
214
     *
215
     * @throws BuildException
216
     */
217
    private function readPatterns(PhingFile $patternfile, &$patternlist, Project $p)
218
    {
219
        $patternReader = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $patternReader is dead and can be removed.
Loading history...
220
        try {
221
            // Get a FileReader
222
            $patternReader = new BufferedReader(new FileReader($patternfile));
223
224
            // Create one NameEntry in the appropriate pattern list for each
225
            // line in the file.
226
            $line = $patternReader->readLine();
227
            while ($line !== null) {
228
                if (!empty($line)) {
229
                    $line = $p->replaceProperties($line);
230
                    $this->addPatternToList($patternlist)->setName($line);
231
                }
232
                $line = $patternReader->readLine();
233
            }
234
        } catch (IOException $ioe) {
235
            $msg = "An error occurred while reading from pattern file: " . $patternfile->__toString();
236
            if ($patternReader) {
0 ignored issues
show
introduced by
$patternReader is of type BufferedReader, thus it always evaluated to true.
Loading history...
237
                $patternReader->close();
238
            }
239
            throw new BuildException($msg, $ioe);
240
        }
241
242
        $patternReader->close();
243
    }
244
245
    /**
246
     * Adds the patterns of the other instance to this set.
247
     *
248
     * @param $other
249
     * @param Project $p
250
     *
251
     * @throws BuildException
252
     */
253 1
    public function append($other, $p)
254
    {
255 1
        if ($this->isReference()) {
256
            throw new BuildException("Cannot append to a reference");
257
        }
258
259 1
        $incl = $other->getIncludePatterns($p);
260 1
        if ($incl !== null) {
261 1
            foreach ($incl as $incl_name) {
262 1
                $o = $this->createInclude();
263 1
                $o->setName($incl_name);
264
            }
265
        }
266
267 1
        $excl = $other->getExcludePatterns($p);
268 1
        if ($excl !== null) {
269
            foreach ($excl as $excl_name) {
270
                $o = $this->createExclude();
271
                $o->setName($excl_name);
272
            }
273
        }
274 1
    }
275
276
    /**
277
     * Returns the filtered include patterns.
278
     *
279
     * @param Project $p
280
     *
281
     * @throws BuildException
282
     *
283
     * @return array
284
     */
285 107
    public function getIncludePatterns(Project $p)
286
    {
287 107
        if ($this->isReference()) {
288
            $o = $this->getRef($p);
289
290
            return $o->getIncludePatterns($p);
0 ignored issues
show
Bug introduced by
The method getIncludePatterns() does not exist on Reference. ( Ignorable by Annotation )

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

290
            return $o->/** @scrutinizer ignore-call */ getIncludePatterns($p);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
291
        }
292
293 107
        $this->readFiles($p);
294
295 107
        return $this->makeArray($this->includeList, $p);
296
    }
297
298
    /**
299
     * Returns the filtered exclude patterns.
300
     *
301
     * @param Project $p
302
     *
303
     * @throws BuildException
304
     *
305
     * @return array
306
     */
307 107
    public function getExcludePatterns(Project $p)
308
    {
309 107
        if ($this->isReference()) {
310
            $o = $this->getRef($p);
311
312
            return $o->getExcludePatterns($p);
0 ignored issues
show
Bug introduced by
The method getExcludePatterns() does not exist on Reference. ( Ignorable by Annotation )

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

312
            return $o->/** @scrutinizer ignore-call */ getExcludePatterns($p);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
313
        }
314
315 107
        $this->readFiles($p);
316
317 107
        return $this->makeArray($this->excludeList, $p);
318
    }
319
320
    /**
321
     * helper for FileSet.
322
     *
323
     * @return bool
324
     */
325 4
    public function hasPatterns()
326
    {
327 4
        return (bool) count($this->includesFileList) > 0 || count($this->excludesFileList) > 0
328 4
            || count($this->includeList) > 0 || count($this->excludeList) > 0;
329
    }
330
331
    /**
332
     * Performs the check for circular references and returns the
333
     * referenced PatternSet.
334
     *
335
     * @param Project $p
336
     *
337
     * @throws BuildException
338
     *
339
     * @return Reference
340
     */
341
    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

341
    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...
342
    {
343
        $dataTypeName = StringHelper::substring(__CLASS__, strrpos(__CLASS__, '\\') + 1);
344
        return $this->getCheckedRef(__CLASS__, $dataTypeName);
345
    }
346
347
    /**
348
     * Convert a array of PatternSetNameEntry elements into an array of Strings.
349
     *
350
     * @param array $list
351
     * @param Project $p
352
     *
353
     * @return array
354
     */
355 107
    private function makeArray(&$list, Project $p)
356
    {
357 107
        if (count($list) === 0) {
358 107
            return null;
359
        }
360
361 76
        $tmpNames = [];
362 76
        foreach ($list as $ne) {
363 76
            $pattern = (string) $ne->evalName($p);
364 76
            if ($pattern !== null && strlen($pattern) > 0) {
365 76
                $tmpNames[] = $pattern;
366
            }
367
        }
368
369 76
        return $tmpNames;
370
    }
371
372
    /**
373
     * Read includesfile or excludesfile if not already done so.
374
     *
375
     * @param Project $p
376
     *
377
     * @throws BuildException
378
     */
379 107
    private function readFiles(Project $p)
380
    {
381 107
        if (!empty($this->includesFileList)) {
382
            foreach ($this->includesFileList as $ne) {
383
                $fileName = (string) $ne->evalName($p);
384
                if ($fileName !== null) {
385
                    $inclFile = $p->resolveFile($fileName);
386
                    if (!$inclFile->exists()) {
387
                        throw new BuildException("Includesfile " . $inclFile->getAbsolutePath() . " not found.");
388
                    }
389
                    $this->readPatterns($inclFile, $this->includeList, $p);
390
                }
391
            }
392
            $this->includesFileList = [];
393
        }
394
395 107
        if (!empty($this->excludesFileList)) {
396
            foreach ($this->excludesFileList as $ne) {
397
                $fileName = (string) $ne->evalName($p);
398
                if ($fileName !== null) {
399
                    $exclFile = $p->resolveFile($fileName);
400
                    if (!$exclFile->exists()) {
401
                        throw new BuildException("Excludesfile " . $exclFile->getAbsolutePath() . " not found.");
402
                    }
403
                    $this->readPatterns($exclFile, $this->excludeList, $p);
404
                }
405
            }
406
            $this->excludesFileList = [];
407
        }
408 107
    }
409
410
    /**
411
     * @return string
412
     */
413 108
    public function __toString()
414
    {
415
416
        // We can't compile includeList into array because, toString() does
417
        // not know about project:
418
        //
419
        // $includes = $this->makeArray($this->includeList, $this->project);
420
        // $excludes = $this->makeArray($this->excludeList, $this->project);
421
422 108
        if (empty($this->includeList)) {
423 34
            $includes = "empty";
424
        } else {
425 74
            $includes = "";
426 74
            foreach ($this->includeList as $ne) {
427 74
                $includes .= (string) $ne . ",";
428
            }
429 74
            $includes = rtrim($includes, ",");
430
        }
431
432 108
        if (empty($this->excludeList)) {
433 107
            $excludes = "empty";
434
        } else {
435 2
            $excludes = "";
436 2
            foreach ($this->excludeList as $ne) {
437 2
                $excludes .= (string) $ne . ",";
438
            }
439 2
            $excludes = rtrim($excludes, ",");
440
        }
441
442 108
        return "patternSet{ includes: $includes  excludes: $excludes }";
443
    }
444
}
445