Completed
Push — develop ( 0cc20e...dccdc9 )
by Mike
07:04
created

src/phpDocumentor/Parser/SpecificationFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
50
        }
51
52 4
        $ignoreSpec = null;
53 4
        foreach ($ignore['paths'] ?? [] as $path) {
0 ignored issues
show
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