Completed
Push — develop ( 373768...b82813 )
by Mike
05:50
created

SpecificationFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
ccs 31
cts 31
cp 1
wmc 13
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A inPath() 0 4 1
A orSpec() 0 4 1
B create() 0 38 9
A notSpec() 0 4 1
A andSpec() 0 4 1
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
     * @var (\phpDocumentor\Path|string)[] $paths
37
     * @var (\phpDocumentor\Path|string)[] $ignore
38
     * @var string[] $extensions
39
     */
40 4
    public function create(array $paths, array $ignore, array $extensions): SpecificationInterface
41
    {
42 4
        $pathSpec = null;
43 4
        foreach ($paths as $path) {
44 3
            if ($pathSpec === null) {
45 3
                $pathSpec = $this->inPath((string) $path);
46 3
                continue;
47
            }
48
49 1
            $pathSpec = $this->orSpec($this->inPath($path), $pathSpec);
0 ignored issues
show
Bug introduced by
It seems like $path defined by $path on line 43 can also be of type object<phpDocumentor\Path>; however, phpDocumentor\Parser\Spe...cationFactory::inPath() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
        }
51
52 4
        $ignoreSpec = null;
53 4
        foreach ($ignore['paths'] ?? [] as $path) {
0 ignored issues
show
Bug introduced by
The expression $ignore['paths'] ?? array() of type object<phpDocumentor\Path>|string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
54 2
            if ($ignoreSpec === null) {
55 2
                $ignoreSpec = $this->inPath((string) $path);
56 2
                continue;
57
            }
58
59 1
            $ignoreSpec = $this->orSpec($this->inPath((string) $path), $ignoreSpec);
60
        }
61
62 4
        if (($ignore['hidden'] ?? false) === true) {
63 1
            $ignoreSpec = $ignoreSpec === null
64 1
                ? new IsHidden()
65 1
                : $this->orSpec(new IsHidden(), $ignoreSpec);
66
        }
67
68 4
        $result = new HasExtension($extensions);
69 4
        if ($ignoreSpec !== null) {
70 3
            $result = $this->andSpec($result, $this->notSpec($ignoreSpec));
71
        }
72 4
        if ($pathSpec !== null) {
73 3
            $result = $this->andSpec($pathSpec, $result);
74
        }
75
76 4
        return $result;
77
    }
78
79 4
    private function inPath(string $path): InPath
80
    {
81 4
        return new InPath(new Path((string) $path));
82
    }
83
84 2
    private function orSpec(SpecificationInterface $or, SpecificationInterface $spec): SpecificationInterface
85
    {
86 2
        return new OrSpecification($spec, $or);
87
    }
88
89 3
    private function notSpec(SpecificationInterface $ignoreSpec): SpecificationInterface
90
    {
91 3
        return new NotSpecification($ignoreSpec);
92
    }
93
94 4
    private function andSpec(SpecificationInterface $spec, SpecificationInterface $spec2): SpecificationInterface
95
    {
96 4
        return new AndSpecification($spec, $spec2);
97
    }
98
}
99