Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

SpecificationFactory::create()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 3
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 6
rs 8.9297
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Parser;
17
18
use Flyfinder\Path;
19
use Flyfinder\Specification\AndSpecification;
20
use Flyfinder\Specification\HasExtension;
21
use Flyfinder\Specification\InPath;
22
use Flyfinder\Specification\IsHidden;
23
use Flyfinder\Specification\NotSpecification;
24
use Flyfinder\Specification\OrSpecification;
25
use Flyfinder\Specification\SpecificationInterface;
26
use phpDocumentor\Parser\SpecificationFactoryInterface as FactoryInterface;
27
28
/**
29
 * Factory class to build Specification used by FlyFinder when reading files to process.
30
 */
31
final class SpecificationFactory implements FactoryInterface
32
{
33
    /**
34
     * Creates a SpecificationInterface object based on the ignore and extension parameters.
35
     */
36 3
    public function create(array $paths, array $ignore, array $extensions): SpecificationInterface
37
    {
38 3
        $pathSpec = null;
39 3
        foreach ($paths as $path) {
40 3
            $pathSpec = $this->orSpec($this->inPath($path), $pathSpec);
41
        }
42
43 3
        $ignoreSpec = null;
44 3
        if (isset($ignore['paths'])) {
45 2
            foreach ($ignore['paths'] as $path) {
46 1
                $ignoreSpec = $this->orSpec($this->inPath($path), $ignoreSpec);
47
            }
48
        }
49
50 3
        if (isset($ignore['hidden']) && $ignore['hidden'] === true) {
51 1
            $ignoreSpec = $this->orSpec(new IsHidden(), $ignoreSpec);
52
        }
53
54 3
        return $this->andSpec(
55 3
            $pathSpec,
56 3
            $this->andSpec(new HasExtension($extensions), $this->notSpec($ignoreSpec))
57
        );
58
    }
59
60
    /**
61
     * Will return an OrSpecification when $or and $spec are both not null.
62
     *
63
     * @param SpecificationInterface|null $spec
64
     * @return OrSpecification|SpecificationInterface
65
     */
66 3
    private function orSpec(SpecificationInterface $or, SpecificationInterface $spec = null): SpecificationInterface
67
    {
68 3
        if ($spec === null) {
69 3
            return $or;
70
        }
71
72 1
        return new OrSpecification($spec, $or);
73
    }
74
75
    /**
76
     * Creates an InPath specification.
77
     *
78
     * @param string $path
79
     */
80 3
    private function inPath($path): InPath
81
    {
82 3
        return new InPath(new Path((string) $path));
83
    }
84
85 3
    private function notSpec(SpecificationInterface $ignoreSpec = null)
86
    {
87 3
        if ($ignoreSpec === null) {
88 1
            return null;
89
        }
90
91 2
        return new NotSpecification($ignoreSpec);
92
    }
93
94 3
    private function andSpec(SpecificationInterface $spec = null, SpecificationInterface $spec2 = null)
95
    {
96 3
        if ($spec === null) {
97
            return $spec2;
98
        }
99
100 3
        if ($spec2 === null) {
101 1
            return $spec;
102
        }
103
104 3
        return new AndSpecification($spec, $spec2);
105
    }
106
}
107